Advertisement
Guest User

WorldToLocalWeirdness

a guest
Feb 25th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import com.jme3.app.SimpleApplication;
  2. import com.jme3.material.Material;
  3. import com.jme3.math.ColorRGBA;
  4. import com.jme3.math.FastMath;
  5. import com.jme3.math.Vector3f;
  6. import com.jme3.scene.Geometry;
  7. import com.jme3.scene.Node;
  8. import com.jme3.scene.debug.Arrow;
  9. import com.jme3.scene.debug.Grid;
  10. import com.jme3.scene.shape.Box;
  11. import com.jme3.texture.Texture;
  12.  
  13. public class WorldToLocal extends SimpleApplication {
  14.    
  15.     static Node container, pivot;
  16.     static Material blank, wireframe;
  17.  
  18.     public static void main(String[] args) {
  19.         WorldToLocal app = new WorldToLocal();
  20.         app.start();
  21.     }
  22.  
  23.     @Override
  24.     public void simpleInitApp() {
  25.         blank = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  26.         blank.setColor("Color", ColorRGBA.White);
  27.         wireframe = blank.clone();
  28.         wireframe.getAdditionalRenderState().setWireframe(true);
  29.         flyCam.setMoveSpeed(10);
  30.        
  31.         //something to contain the pivot node, translated and rotated for testing
  32.         container = new Node();
  33.         container.setLocalTranslation(-0.5f, 1, 2);
  34.         container.rotate(0, 45*FastMath.DEG_TO_RAD, -10*FastMath.DEG_TO_RAD);
  35.         rootNode.attachChild(container);
  36.        
  37.         // the node that needs to face the tgt point
  38.         pivot = new Node();
  39.         pivot.setLocalTranslation(container.worldToLocal(Vector3f.ZERO, new Vector3f()));
  40.         container.attachChild(pivot);
  41.        
  42.         // a point to look at
  43.         Geometry tgt = new Geometry("realpoint", new Box(0.1f,0.1f,0.1f));
  44.         tgt.setLocalTranslation(new Vector3f(-5,2,4));
  45.         tgt.setMaterial(blank);
  46.         rootNode.attachChild(tgt);
  47.        
  48.         //making an arrow that faces the z.axis. this geometry shouldn't be rotated but the parent node
  49.         Arrow arrow = new Arrow(Vector3f.UNIT_Z.mult(2));
  50.         arrow.setLineWidth(8);
  51.         Geometry zaxisvector = new Geometry("vector", arrow);
  52.         zaxisvector.setMaterial(blank);
  53.         pivot.attachChild(zaxisvector);
  54.        
  55.         //should work but doesn't
  56.         pivot.lookAt(pivot.worldToLocal(tgt.getWorldTranslation(), new Vector3f()), Vector3f.UNIT_Y);
  57.        
  58.         //works but shouldn't (comment out to test out the previous lookAt)
  59.         pivot.lookAt(pivot.getWorldRotation().mult(pivot.worldToLocal(tgt.getWorldTranslation().add(pivot.getWorldTranslation()),new Vector3f())), Vector3f.UNIT_Y);
  60.        
  61.         //grid for reference 1 box = 1 unit
  62.         rootNode.attachChild(attachGrid(Vector3f.ZERO, 81, ColorRGBA.White));
  63.     }
  64.    
  65.     private Geometry attachGrid(Vector3f pos, int size, ColorRGBA color){
  66.           Geometry g = new Geometry("wireframe grid", new Grid(size, size, 1));
  67.           g.setMaterial(wireframe);
  68.           g.center().move(pos);
  69.           rootNode.attachChild(g);
  70.           return g;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement