JMonkeyEngine3 Android 旋转 、放大、缩小一个方块 demo 版本3.5.2-stable

 

1. Class,里面是旋转的逻辑,很简陋,可以自己优化

import android.util.Log;
import com.jme3.app.SimpleApplication;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.shape.Box;
import static com.jme3.input.event.TouchEvent.Type.MOVE;
import static com.jme3.input.event.TouchEvent.Type.SCALE_MOVE;

/**
 *  移动端 旋转、缩放的jME3 demo
 * @author uoky
 */
public class HelloJME3 extends SimpleApplication {
	private Geometry geom;

	/**
	 * 初始化3D场景,显示一个方块。
	 */
	@Override
	public void simpleInitApp() {

		// #1 创建一个方块形状的网格
		Mesh box = new Box(1, 1, 1);

		// #2 加载一个感光材质
		Material mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");

		// #3 创建一个几何体,应用刚才和网格和材质。
		geom = new Geometry("Box");
		geom.setMesh(box);
		geom.setMaterial(mat);

		// #4 创建一束定向光,并让它斜向下照射,好使我们能够看清那个方块。
		DirectionalLight sun = new DirectionalLight();
		sun.setDirection(new Vector3f(-1, -2, -3));

		// #5 将方块和光源都添加到场景图中
		rootNode.attachChild(geom);
		rootNode.addLight(sun);
		inputManager.addRawInputListener(new HelloJME3.MyRawInputListener());
	}
	// 原始输入监听器
	class MyRawInputListener implements RawInputListener {

		/**
		 * 键盘输入事件
		 */
		@Override
		public void onKeyEvent(KeyInputEvent evt) {
			Log.v("ceshi","onKeyEvent");
		}

		/**
		 * 鼠标输入事件
		 */
		@Override
		public void onMouseMotionEvent(MouseMotionEvent evt) {
			int x = evt.getX();
			int y = evt.getY();
			// 打印鼠标的坐标
			Log.v("ceshi"," x=" + x + " y=" + y);
		}

		@Override
		public void onMouseButtonEvent(MouseButtonEvent evt) {
			Log.w("ceshi",evt.toString());
			if (evt.isReleased()) {
				int x = evt.getX();
				int y = evt.getY();
				if (x > y) {
					float speed = FastMath.TWO_PI;
					float time = 0.01f;
					geom.rotate(0, -time * speed, 0);
				} else {
					float speed = FastMath.TWO_PI;
					float time = 0.01f;
					geom.rotate(0, time * speed, 0);
				}
			}
			Log.w("ceshi", "onMouseButtonEvent 点击事件 ----------- isPressed:" +
					evt.isPressed() + " isReleased : " + evt.isReleased() + "; X : " + evt.getX() +" Y : " + evt.getY());

		}

		@Override
		public void beginInput() {
			Log.v("ceshi"," beginInput");
		}

		@Override
		public void endInput() {
			Log.v("ceshi"," endInput");
		}

		@Override
		public void onJoyAxisEvent(JoyAxisEvent evt) {
			Log.v("ceshi"," onJoyAxisEvent");
		}

		@Override
		public void onJoyButtonEvent(JoyButtonEvent evt) {
			Log.v("ceshi"," JoyButtonEvent");
		}

		@Override
		public void onTouchEvent(TouchEvent evt) {
			Log.i("ceshi",evt.toString());
			if (evt.getType() == MOVE) {
				// 单指 滑动屏幕 向左右,上下旋转
				float time = 0.01f;
				float speed = FastMath.TWO_PI;
				geom.rotate(Math.abs(evt.getDeltaY()) > 3 ? (evt.getDeltaY() > 0 ? -1 : 1 ) * time * speed : 0,  (evt.getDeltaX() > 0 ? 1 : -1 ) * time * speed, 0);
				Log.i("ceshi", "左边旋转15");
			}
			if (evt.getType() == SCALE_MOVE) {
				// 双指 > 0 放大  < 0 缩小
				geom.scale(evt.getDeltaScaleSpan() > 0 ? 1.02f : 0.98f);
			}
		}
	}

	public static void main(String[] args) {
		// 启动jME3程序
		HelloJME3 app = new HelloJME3();
		app.start();
	}
}

  

2. Activity

import com.jme3.app.AndroidHarness;

public class MainActivity extends AndroidHarness  {
    public MainActivity() {
        this.appClass = "com.uoky.jmtest.jmk.HelloJME3";
    }
}

 

3. build.gradle 的 dependencies

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    def jme3 = [g:'org.jmonkeyengine', v:'3.5.2-stable']
    implementation "$jme3.g:jme3-core:$jme3.v"
    implementation "$jme3.g:jme3-android:$jme3.v"
    implementation "$jme3.g:jme3-android-native:$jme3.v"
}

 

  

 

posted @ 2022-05-23 16:23  人间春风意  阅读(39)  评论(0编辑  收藏  举报