Guest User

Jmonkey BUG?

a guest
Jul 25th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.13 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.bounding.BoundingBox;
  5. import com.jme3.bullet.BulletAppState;
  6. import com.jme3.bullet.PhysicsSpace;
  7. import com.jme3.bullet.collision.shapes.BoxCollisionShape;
  8. import com.jme3.bullet.collision.shapes.CollisionShape;
  9. import com.jme3.bullet.control.RigidBodyControl;
  10. import com.jme3.bullet.control.VehicleControl;
  11. import com.jme3.bullet.objects.VehicleWheel;
  12. import com.jme3.bullet.util.CollisionShapeFactory;
  13. import com.jme3.font.BitmapText;
  14. import com.jme3.input.KeyInput;
  15. import com.jme3.input.controls.ActionListener;
  16. import com.jme3.input.controls.KeyTrigger;
  17. import com.jme3.light.AmbientLight;
  18. import com.jme3.light.DirectionalLight;
  19. import com.jme3.material.Material;
  20. import com.jme3.math.ColorRGBA;
  21. import com.jme3.math.FastMath;
  22. import com.jme3.math.Matrix3f;
  23. import com.jme3.math.Quaternion;
  24. import com.jme3.math.Vector2f;
  25. import com.jme3.math.Vector3f;
  26. import com.jme3.post.FilterPostProcessor;
  27. import com.jme3.post.ssao.SSAOFilter;
  28. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  29. import com.jme3.scene.CameraNode;
  30. import com.jme3.scene.Geometry;
  31. import com.jme3.scene.Node;
  32. import com.jme3.scene.Spatial;
  33. import com.jme3.scene.control.CameraControl.ControlDirection;
  34. import com.jme3.scene.shape.Box;
  35. import com.jme3.shadow.PssmShadowRenderer;
  36. import com.jme3.util.SkyFactory;
  37. import com.jme3.util.TangentBinormalGenerator;
  38.  
  39. /**
  40.  * Example 12 - how to give objects physical properties so they bounce and fall.
  41.  * @author base code by double1984, updated by zathras
  42.  */
  43. public class Main extends SimpleApplication implements ActionListener {
  44.  
  45.  private BulletAppState bulletAppState;
  46.     private VehicleControl player;
  47.     private VehicleWheel fr, fl, br, bl;
  48.     private Node node_fr, node_fl, node_br, node_bl;
  49.     private float wheelRadius;
  50.     private float steeringValue = 0;
  51.     private float accelerationValue = 0;
  52.     private Node carNode;
  53.     private RigidBodyControl    floor_phy;
  54.      private static  Box    floor;
  55.        private RigidBodyControl    floor1_phy;
  56.      private static  Box    floor1;
  57.     private RigidBodyControl    floor2_phy;
  58.      private static  Box    floor2;
  59.      private BitmapText hudText;
  60.       private static Box kamera;
  61.       private static Geometry vcam;
  62.      private BitmapText RPMText;
  63.      private static Node camera_origin;
  64.      private static Boolean isRotating;
  65.      
  66.        CameraNode camNode;
  67.      
  68.     static {
  69.     /** Initialize the floor geometry */
  70.     floor = new Box(200f, 0.01f, 200f);
  71.     floor.scaleTextureCoordinates(new Vector2f(2f, 20f));
  72.  
  73.    
  74.   }  
  75.      
  76.      
  77.   public static void main(String args[]) {
  78.     Main app = new Main();
  79.     app.start();
  80.   }
  81.    private void setupKeys() {
  82.         inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_A));
  83.         inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_D));
  84.         inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_W));
  85.         inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_S));
  86.         inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
  87.         inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_R));
  88.         inputManager.addListener(this, "Lefts");
  89.         inputManager.addListener(this, "Rights");
  90.         inputManager.addListener(this, "Ups");
  91.         inputManager.addListener(this, "Downs");
  92.         inputManager.addListener(this, "Space");
  93.         inputManager.addListener(this, "Reset");
  94.     }
  95.  
  96.     @Override
  97.     public void simpleInitApp() {
  98.         bulletAppState = new BulletAppState();
  99.         stateManager.attach(bulletAppState);
  100.  rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
  101.  //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
  102.         bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0f, -20f, 0f));
  103.  
  104.              PssmShadowRenderer pssmr = new PssmShadowRenderer(assetManager, 4096, 7);
  105.         pssmr.setDirection(new Vector3f(0.5f, -0.3f, 0.3f).normalizeLocal());
  106.         pssmr.setLambda(0.55f);
  107.         pssmr.setShadowIntensity(0.5f);
  108.         pssmr.setEdgesThickness(10);
  109.         pssmr.setCompareMode(PssmShadowRenderer.CompareMode.Software);
  110.         pssmr.setFilterMode(PssmShadowRenderer.FilterMode.PCFPOISSON);
  111.         viewPort.addProcessor(pssmr);
  112.  
  113.  
  114.      
  115.  
  116.     /** A white, directional light source */
  117.     DirectionalLight sun = new DirectionalLight();
  118.     sun.setDirection((new Vector3f(.5f, -0.3f, 0.5f)).normalizeLocal());
  119.     sun.setColor(ColorRGBA.White);
  120.     rootNode.addLight(sun);
  121.          
  122.    
  123.  
  124.  
  125. AmbientLight al = new AmbientLight();
  126. al.setColor(ColorRGBA.White.mult(2f));
  127. rootNode.addLight(al);
  128.  
  129.        
  130.           /** Ambient occlusion shadows */
  131.  FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
  132.  float constantSSAO = 0.2f;
  133.  SSAOFilter ssaoFilter = new SSAOFilter(12.94f*constantSSAO, 23.92f*constantSSAO, 1.33f*constantSSAO, 0.31f*constantSSAO);
  134.     //ssaoFilter.setUseOnlyAo(true);
  135.  fpp.addFilter(ssaoFilter);
  136.  viewPort.addProcessor(fpp);
  137.  
  138.  
  139.  
  140.  
  141.         setupKeys();
  142.  
  143.         initFloor();
  144.         buildPlayer();
  145.  
  146.        
  147.         initGUI();
  148.        
  149.  
  150.         createVirtualCamera();
  151.         initCamera();
  152.    
  153.     }
  154.     private void initCamera(){
  155. //creating the camera Node
  156.     camNode = new CameraNode("CamNode", cam);
  157.     //Setting the direction to Spatial to camera, this means the camera will copy the movements of the Node
  158.     camNode.setControlDir(ControlDirection.SpatialToCamera);
  159.     //attaching the camNode to the teaNode
  160.     camera_origin.attachChild(camNode);
  161.     //setting the local translation of the cam node to move it away from the teanNode a bit
  162.     camNode.setLocalTranslation(new Vector3f(0, 2.5f, 10));
  163.     camNode.setLocalRotation(new Quaternion().fromAngleAxis(FastMath.DEG_TO_RAD*180, new Vector3f(0,1,0)));
  164.    
  165.     //setting the camNode to look at the teaNode
  166.     //camNode.lookAt(new Vector3f(player.getPhysicsLocation().x, 1.5f, player.getPhysicsLocation().z), Vector3f.UNIT_Y);
  167.  
  168.     //disable the default 1st-person flyCam (don't forget this!!)
  169.     flyCam.setEnabled(false);
  170.     }
  171.         private void initGUI(){
  172.         hudText = new BitmapText(guiFont, false);          
  173.         hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
  174.         hudText.setColor(ColorRGBA.White);                             // font color
  175.         hudText.setText("You can write any string here");             // the text
  176.         hudText.setLocalTranslation(0, settings.getHeight(), 0); // position
  177.         guiNode.attachChild(hudText);
  178.        
  179.          RPMText = new BitmapText(guiFont, false);          
  180.         RPMText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
  181.         RPMText.setColor(ColorRGBA.White);                             // font color
  182.         RPMText.setText(1+". gear "+0+" RPM");             // the text
  183.         RPMText.setLocalTranslation(0, settings.getHeight()-RPMText.getLineHeight(), 0); // position
  184.         guiNode.attachChild(RPMText);
  185.     }
  186.        
  187.     private PhysicsSpace getPhysicsSpace() {
  188.         return bulletAppState.getPhysicsSpace();
  189.     }
  190.  
  191.  
  192.     private Geometry findGeom(Spatial spatial, String name) {
  193.         if (spatial instanceof Node) {
  194.             Node node = (Node) spatial;
  195.             for (int i = 0; i < node.getQuantity(); i++) {
  196.                 Spatial child = node.getChild(i);
  197.                 Geometry result = findGeom(child, name);
  198.                 if (result != null) {
  199.                     return result;
  200.                 }
  201.             }
  202.         } else if (spatial instanceof Geometry) {
  203.             if (spatial.getName().startsWith(name)) {
  204.                 return (Geometry) spatial;
  205.             }
  206.         }
  207.         return null;
  208.     }
  209.   /** Make a solid floor and add it to the scene. */
  210.    
  211.   public void initFloor() {
  212.      Material   floor_mat = new Material(assetManager,
  213.         "Common/MatDefs/Light/Lighting.j3md");
  214.     floor_mat.setTexture("DiffuseMap",
  215.         assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg"));
  216.     floor_mat.setTexture("NormalMap",
  217.         assetManager.loadTexture("Textures/Terrain/Pond/Pond_normal.png"));
  218.     floor_mat.setBoolean("UseMaterialColors",true);    
  219.     floor_mat.setColor("Diffuse",ColorRGBA.White);
  220.     floor_mat.setColor("Specular",ColorRGBA.White);
  221.     floor_mat.setColor("Ambient", new ColorRGBA(.25f,.25f, .25f, 1));
  222.     floor_mat.setFloat("Shininess", 64f);  // [0,128]
  223.    
  224.     // BOX material
  225.            Material   box_mat = new Material(assetManager,
  226.         "Common/MatDefs/Light/Lighting.j3md");
  227.         box_mat.setTexture("DiffuseMap",
  228.             assetManager.loadTexture("Textures/box-diffuse.dds"));
  229.         box_mat.setTexture("NormalMap",
  230.             assetManager.loadTexture("Textures/box-normal.dds"));
  231.         box_mat.setTexture("SpecularMap",
  232.             assetManager.loadTexture("Textures/box-specular"
  233.                 + ".dds"));
  234.         box_mat.setBoolean("UseMaterialColors",true);    
  235.         box_mat.setColor("Diffuse",ColorRGBA.White);
  236.         box_mat.setColor("Specular",ColorRGBA.White);
  237.         box_mat.setColor("Ambient", new ColorRGBA(.25f,.25f, .25f, 1));
  238.         box_mat.setFloat("Shininess", 64f);  // [0,128]
  239.     //END BOX MATERIAL
  240.    
  241.     Geometry floor_geo = new Geometry("Floor", floor);
  242.     floor_geo.setShadowMode(ShadowMode.Receive);
  243.     floor_geo.setMaterial(floor_mat);
  244.     floor_geo.setLocalTranslation(0, -0.1f, 0);
  245.     this.rootNode.attachChild(floor_geo);
  246.     /* Make the floor physical with mass 0.0f! */
  247.     floor_phy = new RigidBodyControl(0.0f);
  248.     floor_geo.addControl(floor_phy);
  249.     bulletAppState.getPhysicsSpace().add(floor_phy);
  250.    
  251.      floor1 = new Box(1f, 0.3f, 10f);
  252.    // floor1.scaleTextureCoordinates(new Vector2f(3, 6));
  253.    
  254.        Geometry floor1_geo = new Geometry("Floor1", floor1);
  255.        floor1_geo.setShadowMode(ShadowMode.CastAndReceive);
  256.     floor1_geo.setMaterial(floor_mat);
  257.     floor1_geo.setLocalTranslation(0, -0.1f, 0);
  258.     this.rootNode.attachChild(floor1_geo);
  259.     /* Make the floor physical with mass 0.0f! */
  260.     floor1_phy = new RigidBodyControl(0.0f);
  261.     floor1_geo.addControl(floor1_phy);
  262.     bulletAppState.getPhysicsSpace().add(floor1_phy);
  263.    
  264.     for (int i=0; i<4; i++){
  265.        
  266.          Box floor4 = new Box(1f, 0.3f, 10f);
  267.          Geometry skok = new Geometry("Floor4", floor4);
  268.         skok.setShadowMode(ShadowMode.CastAndReceive);
  269.         skok.setMaterial(floor_mat);
  270.         skok.setLocalTranslation(-i*6f, -0.05f, 0);
  271.         rootNode.attachChild(skok);
  272.         /* Make the floor physical with mass 0.0f! */
  273.         RigidBodyControl skok_phy = new RigidBodyControl(0.0f);
  274.         skok.addControl(skok_phy);
  275.         bulletAppState.getPhysicsSpace().add(skok_phy);
  276.      
  277.     }
  278.    
  279.     floor2 = new Box(5f, 0.3f, 10f);
  280.     //floor2.scaleTextureCoordinates(new Vector2f(3, 6));
  281.        Geometry floor2_geo = new Geometry("Floor2", floor2);
  282.        floor2_geo.setShadowMode(ShadowMode.CastAndReceive);
  283.     floor2_geo.setMaterial(floor_mat);
  284.     floor2_geo.setLocalTranslation(5f, -0.1f, 0);
  285.     floor2_geo.rotate(0,0 , 0.2f);
  286.     this.rootNode.attachChild(floor2_geo);
  287.     /* Make the floor physical with mass 0.0f! */
  288.     floor2_phy = new RigidBodyControl(0.0f);
  289.     floor2_geo.addControl(floor2_phy);
  290.     bulletAppState.getPhysicsSpace().add(floor2_phy);
  291.    
  292.     for (int i=0; i<2; i++){
  293.          for(int j = 0; j<3;j++){
  294.       Box teapot = new Box(1f, 1f, 1f);
  295.       Geometry teapot_g = new Geometry("pBox", teapot);
  296.       teapot_g.setShadowMode(ShadowMode.CastAndReceive);
  297.       TangentBinormalGenerator.generate(teapot_g.getMesh(), true);
  298.      
  299.         teapot_g.setMaterial(box_mat);
  300.         rootNode.attachChild(teapot_g);
  301.         teapot_g.setLocalTranslation(12f, i*3 +1.2f, j*2 +.1f);
  302.         System.out.print(String.valueOf(j*2));
  303.         teapot_g.setLocalScale(1);
  304.          RigidBodyControl teapot_phy = new RigidBodyControl(1000.0f);
  305.         teapot_g.addControl(teapot_phy);
  306.         bulletAppState.getPhysicsSpace().add(teapot_phy);
  307.          }
  308.     }
  309.    
  310.     Spatial barrelNode = assetManager.loadModel("Models/barrel.obj");
  311.     barrelNode.setShadowMode(ShadowMode.CastAndReceive);
  312.         barrelNode.setLocalTranslation(0,2, -20);
  313.         barrelNode.setLocalScale(0.5f);
  314.         rootNode.attachChild(barrelNode);
  315.           RigidBodyControl barrel_phy = new RigidBodyControl(1000.0f);
  316.         barrelNode.addControl(barrel_phy);
  317.        
  318.         barrel_phy.setFriction(2);
  319.         barrel_phy.setPhysicsRotation(new Quaternion(1f, 0f, 0f, 1f));
  320.         bulletAppState.getPhysicsSpace().add(barrel_phy);
  321.        
  322.        
  323.        
  324.        
  325.        
  326.          Spatial felicia = assetManager.loadModel("Models/felicia.obj");
  327.     felicia.setShadowMode(ShadowMode.CastAndReceive);
  328.         felicia.setLocalTranslation(-4,2, 0);
  329.         felicia.setLocalScale(0.5f);
  330.         rootNode.attachChild(felicia);
  331.        
  332.          RigidBodyControl felicia_phy = new RigidBodyControl(1000.0f);
  333.         felicia.addControl(felicia_phy);
  334.         felicia_phy.setCollisionShape(new BoxCollisionShape(Vector3f.ZERO));
  335.         felicia_phy.setFriction(2);
  336.         bulletAppState.getPhysicsSpace().add(felicia_phy);
  337.        
  338.        
  339.   }
  340.     private void buildPlayer() {
  341.         float stiffness = 60.0f;//200=f1 car
  342.         float compValue = 0.05f; //(lower than damp!)
  343.         float dampValue = 0.3f;
  344.         final float mass = 2500;
  345.        
  346.         //Load model and get chassis Geometry
  347.         carNode = (Node)assetManager.loadModel("Models/Ferrari/Car.scene");
  348.         carNode.setShadowMode(ShadowMode.CastAndReceive);
  349.         Geometry chasis = findGeom(carNode, "Car");
  350.         BoundingBox box = (BoundingBox) chasis.getModelBound();
  351.  
  352.        
  353.         //Create a hull collision shape for the chassis
  354.         CollisionShape carHull = CollisionShapeFactory.createDynamicMeshShape(chasis);
  355.  
  356.         //Create a vehicle control
  357.         player = new VehicleControl(carHull, mass);
  358.         carNode.addControl(player);
  359.  
  360.        // player.setGravity(new Vector3f(0, 100f, 0));        //Setting default values for wheels
  361.         player.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
  362.         player.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
  363.         player.setSuspensionStiffness(stiffness);
  364.         player.setMaxSuspensionForce(100000);
  365.        
  366.      
  367.        
  368.         //Create four wheels and add them at their locations
  369.         //note that our fancy car actually goes backwards..
  370.         Vector3f wheelDirection = new Vector3f(0, -1, 0);
  371.         Vector3f wheelAxle = new Vector3f(-1, 0, 0);
  372.  
  373.         Geometry wheel_fr = findGeom(carNode, "WheelFrontRight");
  374.         wheel_fr.setShadowMode(ShadowMode.CastAndReceive);
  375.         wheel_fr.center();
  376.         box = (BoundingBox) wheel_fr.getModelBound();
  377.         wheelRadius = box.getYExtent();
  378.         float back_wheel_h = (wheelRadius * 1.7f) - 1f;
  379.         float front_wheel_h = (wheelRadius * 1.9f) - 1f;
  380.         player.addWheel(wheel_fr.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
  381.                 wheelDirection, wheelAxle, .5f, wheelRadius, true);
  382.  
  383.         Geometry wheel_fl = findGeom(carNode, "WheelFrontLeft");
  384.         wheel_fl.setShadowMode(ShadowMode.CastAndReceive);
  385.         wheel_fl.center();
  386.         box = (BoundingBox) wheel_fl.getModelBound();
  387.         player.addWheel(wheel_fl.getParent(), box.getCenter().add(0, -front_wheel_h, 0),
  388.                 wheelDirection, wheelAxle, .5f, wheelRadius, true);
  389.  
  390.         Geometry wheel_br = findGeom(carNode, "WheelBackRight");
  391.         wheel_br.setShadowMode(ShadowMode.CastAndReceive);
  392.         wheel_br.center();
  393.         box = (BoundingBox) wheel_br.getModelBound();
  394.         player.addWheel(wheel_br.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
  395.                 wheelDirection, wheelAxle, .5f, wheelRadius, false);
  396.  
  397.         Geometry wheel_bl = findGeom(carNode, "WheelBackLeft");
  398.         wheel_bl.setShadowMode(ShadowMode.CastAndReceive);
  399.         wheel_bl.center();
  400.         box = (BoundingBox) wheel_bl.getModelBound();
  401.         player.addWheel(wheel_bl.getParent(), box.getCenter().add(0, -back_wheel_h, 0),
  402.                 wheelDirection, wheelAxle, .5f, wheelRadius, false);
  403.        
  404.         player.getWheel(0).setFrictionSlip(3f);
  405.         player.getWheel(1).setFrictionSlip(3f);
  406.         player.getWheel(2).setFrictionSlip(2f);
  407.         player.getWheel(3).setFrictionSlip(2f);
  408.        
  409.    
  410.         rootNode.attachChild(carNode);
  411.         getPhysicsSpace().add(player);
  412.     }
  413.  
  414.     public void onAction(String binding, boolean value, float tpf) {
  415.          int power = 20000;
  416.         if (binding.equals("Lefts")) {
  417.             if (value) {
  418.                 steeringValue += .7f;
  419.             } else {
  420.                 steeringValue += -.7f;
  421.             }
  422.             player.steer(steeringValue);
  423.         } else if (binding.equals("Rights")) {
  424.             if (value) {
  425.                 steeringValue += -.7f;
  426.             } else {
  427.                 steeringValue += .7f;
  428.             }
  429.             player.steer(steeringValue);
  430.  
  431.         } //note that our fancy car actually goes backwards..
  432.         else if (binding.equals("Ups")) {
  433.             if (value) {
  434.                 accelerationValue -= power;
  435.             } else {
  436.                 accelerationValue += power;
  437.             }
  438.            
  439.             player.accelerate(2, accelerationValue);
  440.              player.accelerate(3, accelerationValue);
  441.            
  442.             player.setCollisionShape(CollisionShapeFactory.createDynamicMeshShape(findGeom(carNode, "Car")));
  443.         } else if (binding.equals("Downs")) {
  444.             if (value) {
  445.                
  446.                 accelerationValue += power;
  447.                
  448.             } else {
  449.                
  450.                 accelerationValue -= power;
  451.             }
  452.             player.accelerate(2, accelerationValue);
  453.              player.accelerate(3, accelerationValue);
  454.         } else if (binding.equals("Reset")) {
  455.             if (value) {
  456.                 System.out.println("Reset");
  457.                 player.setPhysicsLocation(Vector3f.ZERO);
  458.                 player.setPhysicsRotation(new Matrix3f());
  459.                 player.setLinearVelocity(Vector3f.ZERO);
  460.                 player.setAngularVelocity(Vector3f.ZERO);
  461.                 player.resetSuspension();
  462.             } else {
  463.             }
  464.         }
  465.         else if (binding.equals("Space")) {
  466.         //createBox(carNode.getLocalTranslation(),carNode.getLocalRotation());
  467.              if (value) {
  468.  
  469.                  
  470.                //  camNode.lookAt(carNode.getLocalTranslation(), Vector3f.UNIT_Y);            
  471.                 player.brake(2, 100000f);
  472.                 player.brake(3, 1000000f);
  473.                
  474.             } else {
  475.                player.brake(2, 0f);
  476.                player.brake(3, 0f);
  477.             }
  478.            
  479.         }
  480.     }
  481.     public void createBox(Vector3f pos, Quaternion rot){
  482.          Box b = new Box(1, 1, 1); // create a 1x1x1 box shape
  483.         Geometry geom = new Geometry("Box", b);  // create a cube geometry from the box shape
  484.         Material mat = new Material(assetManager,
  485.           "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
  486.         mat.setColor("Color", ColorRGBA.Blue);   // set color of material to blue
  487.         geom.setMaterial(mat);                   // set the cube geometry 's material
  488.         geom.setLocalTranslation(pos.x,pos.y,pos.z+6f);
  489.         geom.setLocalRotation(rot);
  490.         rootNode.attachChild(geom);              // make the cube geometry appear in the scene
  491.     }
  492.    
  493.       public void createVirtualCamera(){
  494.         kamera =  new Box(.3f, .3f, .3f); // create a 1x1x1 box shape
  495.         vcam = new Geometry("vCamera", kamera);  // create a cube geometry from the box shape
  496.         Material mat = new Material(assetManager,
  497.           "Common/MatDefs/Misc/Unshaded.j3md");  // create a simple material
  498.         mat.setColor("Color", ColorRGBA.Green);   // set color of material to blue
  499.         mat.getAdditionalRenderState().setWireframe(true);
  500.         vcam.setMaterial(mat);                   // set the cube geometry 's material
  501.         camera_origin = new Node("camer_origin");
  502.         rootNode.attachChild(camera_origin);
  503.         camera_origin.attachChild(vcam);              // make the cube geometry appear in the scene
  504.     }
  505.     @Override
  506.     public void simpleUpdate(float tpf) {
  507.                 //camNode.rotateUpTo(new Vector3f(-carNode.getWorldRotation().getX(), 1f, 0f));
  508.  
  509.        
  510.        camera_origin.setLocalTranslation(carNode.getLocalTranslation());
  511.        //camera_origin.setLocalRotation(new Quaternion(0f, carNode.getWorldRotation().getY(), 0f, carNode.getWorldRotation().getW() ));
  512.      
  513.      
  514.         float velocity = FastMath.floor(Math.abs(player.getCurrentVehicleSpeedKmHour()));
  515.  
  516.         if(Math.abs(velocity) < 0.01f){
  517.              //player.setLinearVelocity(Vector3f.ZERO);
  518.             // player.brake(100f);
  519.         }
  520.         hudText.setText(String.valueOf(velocity) + "km/h");
  521.        
  522.         float cam_dir = camera_origin.getLocalRotation().getY();
  523.         float car_dir = carNode.getLocalRotation().getY();
  524.        
  525.         String test= "";
  526.         /*if(FastMath.abs(cam_dir - car_dir) > 0.2f){
  527.             isRotating = Boolean.TRUE;
  528.         }
  529.        
  530.         if(FastMath.abs(cam_dir - car_dir) < 0.01f){
  531.              isRotating = Boolean.FALSE;
  532.         }*/
  533.        
  534.         if(!(FastMath.abs(cam_dir - car_dir) < 0.00001f)){
  535.             //camera_origin.setLocalRotation(new Quaternion(0f, camera_origin.getLocalRotation().getY()-(cam_dir - car_dir)*tpf, 0f, camera_origin.getWorldRotation().getW() ));
  536.             Quaternion cama = camera_origin.getLocalRotation();
  537.             Quaternion cara = carNode.getLocalRotation();
  538.             Quaternion vysledek;
  539.             cama.slerp(cara, tpf*3);
  540.             camera_origin.setLocalRotation(new Quaternion(0f,cama.getY(),0f,cama.getW()).normalizeLocal());
  541.         }
  542.      
  543.         test = String.format("%.4f",FastMath.abs(cam_dir - car_dir));
  544.         RPMText.setText("Vektor kamery("+ String.format("%.4f", cam_dir) +") - vektor aute ("+ String.format("%.4f", car_dir) +"):"+test);
  545.     }
  546. }
Advertisement
Add Comment
Please, Sign In to add comment