Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package accel.tests;
- import com.jme3.app.SimpleApplication;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.material.Material;
- import com.jme3.math.ColorRGBA;
- import com.jme3.math.FastMath;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Vector3f;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- /**
- * The teapot above is moved with mult15
- * The teapot belov is moved with jme quaternions.
- *
- *
- * @author leo
- */
- public class OrbitTest extends SimpleApplication {
- public static void main(String[] args) {
- OrbitTest app = new OrbitTest();
- app.setShowSettings(true);
- app.start();
- }
- static float camSpeed = 10f;
- Node node, node15;
- @Override
- public void simpleInitApp() {
- getViewPort().setBackgroundColor(ColorRGBA.LightGray);
- initWireFrame();
- initFastCam();
- float rad = 1000;
- cam.setLocation(new Vector3f(-rad, 1.300249f, 0f));
- node = new Node();
- node15 = new Node();
- Spatial teapott = assetManager.loadModel("Models/Teapot/Teapot.obj");
- Material mat_default = new Material(
- assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
- teapott.setMaterial(mat_default);
- int teapots = 100;
- initalpos = new float[teapots][2];
- for(int i = 0; i < teapots; i++) {
- float x = rad*FastMath.cos(i*FastMath.TWO_PI/teapots);
- float y = rad*FastMath.sin(i*FastMath.TWO_PI/teapots);
- Spatial teapot = teapott.clone(true);
- teapot.setLocalTranslation(x, -1f, y);
- node.attachChild(teapot);
- teapot = teapott.clone(true);
- teapot.setLocalTranslation(x, 0f, y);
- node15.attachChild(teapot);
- initalpos[i][0] = x;
- initalpos[i][1] = y;
- }
- rootNode.attachChild(node);
- rootNode.attachChild(node15);
- }
- Quaternion tmp = new Quaternion();
- float initalpos[][];
- Quaternion node15Rotation = new Quaternion();
- @Override
- public void simpleUpdate(float tpf) {
- Quaternion q = tmp;
- q.fromAngles(0, tpf*0.001f,0);
- node.rotate(q);
- node15Rotation.multLocal(q);
- for(int i = 0; i < node15.getQuantity(); i++) {
- Spatial s = node15.getChild(i);
- Vector3f pos = s.getLocalTranslation();
- pos.set(initalpos[i][0], 0f, initalpos[i][1]);
- mult15(node15Rotation, pos, pos);
- s.setLocalTranslation(pos);
- }
- }
- public static Vector3f mult15( Quaternion q, Vector3f v, Vector3f store) {
- float x = q.getX();
- float y = q.getY();
- float z = q.getZ();
- float w = q.getW();
- float vx = y * v.z - z * v.y;
- float vy = z * v.x - x * v.z;
- float vz = x * v.y - y * v.x;
- vx += vx; vy += vy; vz += vz;
- store.x = v.x + w * vx + y * vz - z * vy;
- store.y = v.y + w * vy + z * vx - x * vz;
- store.z = v.z + w * vz + x * vy - y * vx;
- return store;
- }
- public void initWireFrame() {
- final boolean wire = false;
- flyCam.setDragToRotate(true);
- final mygame.WireProcessor wp = new mygame.WireProcessor(assetManager);
- inputManager.addMapping("f4", new KeyTrigger(KeyInput.KEY_F4));
- inputManager.addListener(new ActionListener() {
- boolean enabled = wire;
- public void onAction(String name, boolean isPressed, float tpf) {
- if(!isPressed) return;
- enabled = !enabled;
- if(enabled) getViewPort().addProcessor(wp);
- else getViewPort().removeProcessor(wp);
- }
- }, "f4");
- if(wire) getViewPort().addProcessor(wp);
- }
- public void initFastCam() {
- String[] mappings = new String[]{
- "FLYCAM_Left",
- "FLYCAM_Right",
- "FLYCAM_Up",
- "FLYCAM_Down",
- "FLYCAM_StrafeLeft",
- "FLYCAM_StrafeRight",
- "FLYCAM_Forward",
- "FLYCAM_Backward",
- "FLYCAM_ZoomIn",
- "FLYCAM_ZoomOut",
- "FLYCAM_RotateDrag",
- "FLYCAM_Rise",
- "FLYCAM_Lower",
- "FLYCAM_InvertY"
- };
- inputManager.removeListener(flyCam);
- inputManager.addListener(new AnalogListener() {
- public void onAnalog(String name, float value, float tpf) {
- flyCam.onAnalog(name, value*camSpeed, tpf);
- }
- }, mappings);
- inputManager.addListener(new ActionListener() {
- public void onAction(String name, boolean isPressed, float tpf) {
- flyCam.onAction(name, isPressed, tpf);
- }
- }, mappings);
- inputManager.addMapping("Pos", new KeyTrigger(KeyInput.KEY_F2));
- inputManager.addMapping("P", new KeyTrigger(KeyInput.KEY_P));
- inputManager.addListener(new ActionListener() {
- public void onAction(String name, boolean isPressed, float tpf) {
- if(!isPressed) return;
- if(name.equals("Pos")) {
- System.out.println("cam at: "+ cam.getLocation());
- }
- }
- }, "Pos", "P");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement