Advertisement
Guest User

Untitled

a guest
Feb 26th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. package accel.tests;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.input.KeyInput;
  5. import com.jme3.input.controls.ActionListener;
  6. import com.jme3.input.controls.AnalogListener;
  7. import com.jme3.input.controls.KeyTrigger;
  8. import com.jme3.material.Material;
  9. import com.jme3.math.ColorRGBA;
  10. import com.jme3.math.FastMath;
  11. import com.jme3.math.Quaternion;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.scene.Node;
  14. import com.jme3.scene.Spatial;
  15.  
  16. /**
  17.  * The teapot above is moved with mult15
  18.  * The teapot belov is moved with jme quaternions.
  19.  *
  20.  *
  21.  * @author leo
  22.  */
  23. public class OrbitTest extends SimpleApplication {
  24.     public static void main(String[] args) {
  25.         OrbitTest app = new OrbitTest();
  26.         app.setShowSettings(true);
  27.         app.start();
  28.     }
  29.     static float camSpeed = 10f;    
  30.     Node node, node15;
  31.     @Override
  32.     public void simpleInitApp() {
  33.          getViewPort().setBackgroundColor(ColorRGBA.LightGray);
  34.         initWireFrame();
  35.         initFastCam();
  36.         float rad = 1000;
  37.         cam.setLocation(new Vector3f(-rad, 1.300249f, 0f));
  38.         node = new Node();
  39.         node15 = new Node();
  40.        
  41.         Spatial teapott = assetManager.loadModel("Models/Teapot/Teapot.obj");
  42.         Material mat_default = new Material(
  43.                 assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
  44.         teapott.setMaterial(mat_default);
  45.        
  46.         int teapots = 100;
  47.         initalpos = new float[teapots][2];
  48.         for(int i = 0; i < teapots; i++) {
  49.             float x = rad*FastMath.cos(i*FastMath.TWO_PI/teapots);
  50.             float y = rad*FastMath.sin(i*FastMath.TWO_PI/teapots);
  51.             Spatial teapot = teapott.clone(true);
  52.             teapot.setLocalTranslation(x, -1f, y);
  53.             node.attachChild(teapot);
  54.            
  55.             teapot = teapott.clone(true);
  56.             teapot.setLocalTranslation(x, 0f, y);
  57.             node15.attachChild(teapot);
  58.             initalpos[i][0] = x;
  59.             initalpos[i][1] = y;
  60.         }
  61.         rootNode.attachChild(node);
  62.         rootNode.attachChild(node15);
  63.     }
  64.     Quaternion tmp = new Quaternion();
  65.    
  66.     float initalpos[][];
  67.    
  68.     Quaternion node15Rotation = new Quaternion();
  69.     @Override
  70.     public void simpleUpdate(float tpf) {
  71.         Quaternion q = tmp;
  72.         q.fromAngles(0, tpf*0.001f,0);
  73.         node.rotate(q);
  74.        
  75.         node15Rotation.multLocal(q);
  76.         for(int i = 0; i < node15.getQuantity(); i++) {
  77.             Spatial s = node15.getChild(i);
  78.             Vector3f pos = s.getLocalTranslation();
  79.             pos.set(initalpos[i][0], 0f, initalpos[i][1]);
  80.             mult15(node15Rotation, pos, pos);
  81.             s.setLocalTranslation(pos);
  82.         }
  83.     }
  84.     public static Vector3f mult15( Quaternion q, Vector3f v, Vector3f store) {
  85.         float x = q.getX();
  86.         float y = q.getY();
  87.         float z = q.getZ();
  88.         float w = q.getW();
  89.        
  90.         float vx = y * v.z - z * v.y;
  91.         float vy = z * v.x - x * v.z;
  92.         float vz = x * v.y - y * v.x;
  93.         vx += vx; vy += vy; vz += vz;
  94.        
  95.         store.x = v.x + w * vx + y * vz - z * vy;
  96.         store.y = v.y + w * vy + z * vx - x * vz;
  97.         store.z = v.z + w * vz + x * vy - y * vx;
  98.  
  99.         return store;
  100.     }    
  101.    
  102.    
  103.    
  104.    
  105.    
  106.     public void initWireFrame() {
  107.         final boolean wire = false;
  108.         flyCam.setDragToRotate(true);
  109.         final mygame.WireProcessor wp = new mygame.WireProcessor(assetManager);
  110.          inputManager.addMapping("f4", new KeyTrigger(KeyInput.KEY_F4));
  111.          inputManager.addListener(new ActionListener() {
  112.              boolean enabled = wire;  
  113.              public void onAction(String name, boolean isPressed, float tpf) {
  114.                  if(!isPressed) return;
  115.                  enabled = !enabled;  
  116.                  if(enabled)  getViewPort().addProcessor(wp);
  117.                  else  getViewPort().removeProcessor(wp);
  118.                 }
  119.             }, "f4");
  120.          if(wire)  getViewPort().addProcessor(wp);
  121.     }
  122.         public void initFastCam() {
  123.         String[] mappings = new String[]{
  124.             "FLYCAM_Left",
  125.             "FLYCAM_Right",
  126.             "FLYCAM_Up",
  127.             "FLYCAM_Down",
  128.  
  129.             "FLYCAM_StrafeLeft",
  130.             "FLYCAM_StrafeRight",
  131.             "FLYCAM_Forward",
  132.             "FLYCAM_Backward",
  133.  
  134.             "FLYCAM_ZoomIn",
  135.             "FLYCAM_ZoomOut",
  136.             "FLYCAM_RotateDrag",
  137.  
  138.             "FLYCAM_Rise",
  139.            
  140.             "FLYCAM_Lower",
  141.             "FLYCAM_InvertY"
  142.         };
  143.         inputManager.removeListener(flyCam);
  144.         inputManager.addListener(new AnalogListener() {
  145.  
  146.             public void onAnalog(String name, float value, float tpf) {
  147.                 flyCam.onAnalog(name, value*camSpeed, tpf);
  148.             }
  149.         }, mappings);
  150.         inputManager.addListener(new ActionListener() {
  151.  
  152.             public void onAction(String name, boolean isPressed, float tpf) {
  153.                 flyCam.onAction(name, isPressed, tpf);
  154.             }
  155.         }, mappings);
  156.         inputManager.addMapping("Pos", new KeyTrigger(KeyInput.KEY_F2));
  157.         inputManager.addMapping("P", new KeyTrigger(KeyInput.KEY_P));
  158.         inputManager.addListener(new ActionListener() {
  159.             public void onAction(String name, boolean isPressed, float tpf) {
  160.                 if(!isPressed) return;
  161.                 if(name.equals("Pos")) {
  162.                     System.out.println("cam at: "+ cam.getLocation());
  163.                 }
  164.             }
  165.         }, "Pos", "P");
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement