Advertisement
ComputerCraft32

Main

Jul 7th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1. package mygame;
  2.  
  3. import com.jme3.app.SimpleApplication;
  4. import com.jme3.bullet.BulletAppState;
  5. import com.jme3.bullet.PhysicsSpace;
  6. import com.jme3.bullet.PhysicsTickListener;
  7. import com.jme3.bullet.collision.PhysicsCollisionEvent;
  8. import com.jme3.bullet.collision.PhysicsCollisionListener;
  9. import com.jme3.bullet.control.RigidBodyControl;
  10. import com.jme3.collision.CollisionResults;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.controls.ActionListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.material.Material;
  15. import com.jme3.math.Quaternion;
  16. import com.jme3.math.Ray;
  17. import com.jme3.math.Vector3f;
  18. import com.jme3.renderer.RenderManager;
  19. import com.jme3.scene.Geometry;
  20. import com.jme3.scene.Spatial;
  21. import com.jme3.scene.shape.Box;
  22. import java.util.Random;
  23.  
  24.  
  25. /**
  26.  * This is the Main Class of your Game. You should only do initialization here.
  27.  * Move your Logic into AppStates or Controls
  28.  * @author normenhansen
  29.  */
  30. public class Main extends SimpleApplication implements PhysicsTickListener, PhysicsCollisionListener {
  31.     Quaternion quaternion;
  32.     Vector3f loc;
  33.     Random random = new Random();
  34.     Geometry geo;
  35.     float deltaTime;
  36.     RigidBodyControl vehicleControler;
  37.     int i;
  38.     BulletAppState appState;
  39.    
  40.    
  41.     public static void main(String[] args) {
  42.        
  43.         Main app = new Main();
  44.         app.setShowSettings(false);
  45.         app.start();
  46.     }
  47.  
  48.    ActionListener actionListener = new ActionListener() {
  49.        
  50.         @Override
  51.         public void onAction(String name, boolean keyPressed, float tpf) {
  52.           if(keyPressed){
  53.             if ("Up".equals(name))  
  54.                 i = 1;
  55.          if ("Down".equals(name));
  56.                 i = 2;
  57.          if ("Left".equals(name))  
  58.                 i = 3;
  59.          if ("Right".equals(name))
  60.                 i = 4;
  61.          if ("Uper".equals(name))  
  62.                 i = 5;
  63.          if ("Downer".equals(name))
  64.                 i = 6;
  65.        
  66.         }
  67.          }
  68.         };
  69.    
  70.    
  71.    
  72.     @Override
  73.     public void simpleInitApp() {
  74.         flyCam.setMoveSpeed(100);
  75.        
  76.         appState =  new BulletAppState();
  77.         stateManager.attach(appState);
  78.         appState.getPhysicsSpace().addTickListener(this);
  79.          appState.getPhysicsSpace().addCollisionListener(this);
  80.         appState.getPhysicsSpace().setGravity(new Vector3f(0, -9.8f, 0));
  81.        
  82.         Spatial terrain = assetManager.loadModel("Textures/GameScene.j3o");
  83.         terrain.setLocalTranslation(0, -100, 0);
  84.        
  85.        
  86.         RigidBodyControl terrainPhysics = new RigidBodyControl(0);
  87.         terrain.addControl(terrainPhysics);
  88.         appState.getPhysicsSpace().add(terrainPhysics);
  89.        
  90.         rootNode.attachChild(terrain);
  91.        
  92.         Box box = new Box(10, 2f, 10);
  93.         Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  94.         geo = new Geometry("Vehicle", box);
  95.         geo.setMaterial(mat);
  96.         geo.setCullHint(Spatial.CullHint.Never);
  97.        
  98.        
  99.         vehicleControler = new RigidBodyControl(2);
  100.         geo.addControl(vehicleControler);
  101.         appState.getPhysicsSpace().add(vehicleControler);
  102.         appState.getPhysicsSpace().setGravity(new Vector3f(0, -9.8f, 0));
  103.         vehicleControler.setPhysicsLocation(new Vector3f(0, -50, 0));
  104.         //vehicleControler.setFriction(0);
  105.         rootNode.attachChild(geo);
  106.        
  107.         inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_1));
  108.         inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_2));
  109.         inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
  110.         inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
  111.         inputManager.addMapping("Uper", new KeyTrigger(KeyInput.KEY_9));
  112.         inputManager.addMapping("Downer", new KeyTrigger(KeyInput.KEY_0));
  113.        
  114.         inputManager.addListener(actionListener, "Up");
  115.         inputManager.addListener(actionListener, "Down");
  116.         inputManager.addListener(actionListener, "Left");
  117.         inputManager.addListener(actionListener, "Right");
  118.         inputManager.addListener(actionListener, "Uper");
  119.         inputManager.addListener(actionListener, "Downer");
  120.        
  121.        
  122.         quaternion = new Quaternion();
  123.         quaternion.fromAngles(0, 0, 0);
  124.        
  125. //       Vector3f loc = vehicleControler.getPhysicsLocation();
  126. //       vehicleControler.setPhysicsLocation(new Vector3f(loc.x, loc.y + 10, loc.z));
  127.  
  128.     }
  129.  
  130.     @Override
  131.     public void simpleUpdate(float tpf) {
  132.        
  133.     }
  134.  
  135.     @Override
  136.     public void simpleRender(RenderManager rm) {
  137.         //TODO: add render code
  138.     }
  139.  
  140.     @Override
  141.     public void prePhysicsTick(PhysicsSpace space, float tpf) {
  142.         vehicleControler.setPhysicsRotation(quaternion);
  143.         deltaTime += tpf;
  144.        
  145.         switch (i) {
  146.            case 1:
  147.                vehicleControler.setLinearVelocity(new Vector3f(10, 0, 0));
  148.                break;
  149.            case 2:
  150.                vehicleControler.setLinearVelocity(new Vector3f(-10, 0, 0));
  151.                break;
  152.            case 3:
  153.                vehicleControler.setLinearVelocity(new Vector3f(0, 0, -10));
  154.                break;  
  155.            case 4:
  156.                vehicleControler.setLinearVelocity(new Vector3f(0, 0, +10));
  157.                break;  
  158.            case 5:
  159.                vehicleControler.setLinearVelocity(new Vector3f(0, 10, 0));
  160.                break;  
  161.            case 6:
  162.                vehicleControler.setLinearVelocity(new Vector3f(0, -10, 0));
  163.                break;        
  164.                
  165.        }
  166.     //   System.out.println("preTick called! Var i is :" + i);
  167.        
  168.    
  169.   if (deltaTime >= 0.7){
  170. //       vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x - 2 , loc.y - 2, loc.z -2));
  171. //       vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x + 2 , loc.y - 2, loc.z +2));
  172. //       vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x - 2 , loc.y - 2, loc.z +2));
  173. //       vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x + 2 , loc.y - 2, loc.z  - 2));
  174.  //       vehicleControler.applyCentralForce(new Vector3f(0, 525, 0));
  175. //        vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(2, 0, 2));
  176. //        vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(-2, 0, -2));
  177. //        vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(+2, 0, -2));
  178. //         vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(-2, 0, +2));
  179. //         vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(0, 0, 0));
  180.          vehicleControler.setLinearVelocity(new Vector3f(0, 210 * tpf, 0));
  181.         // System.out.println(vehicleControler.getPhysicsLocation());
  182. //         if (loc.distance(new Vector3f(loc.x, loc.y - 5)))
  183. //         vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(2, 0, 2));
  184. //        vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(-2, 0, -2));
  185. //        vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(+2, 0, -2));
  186. //         vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(-2, 0, +2));
  187. //         vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(0, 0, 0));
  188.        
  189.        deltaTime = 0;
  190.          }
  191.        
  192.     }
  193.  
  194.     @Override
  195.     public void physicsTick(PhysicsSpace space, float tpf) {
  196.      loc = vehicleControler.getPhysicsLocation();
  197.         //  System.out.println("phsyicsTick called!");
  198.         //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  199.     }
  200.  
  201.     @Override
  202.     public void collision(PhysicsCollisionEvent event) {
  203.       Vector3f NEGATIVE_Y = new Vector3f(0, -1, 0);
  204.       Vector3f NEGATIVE_X = new Vector3f(-1, 0, 0);
  205.       Vector3f NEGATIVE_Z = new Vector3f(0, 0, -1);
  206.      
  207.         CollisionResults results = new CollisionResults();
  208.       Ray ray = new Ray(geo.getWorldTranslation(), NEGATIVE_Y);
  209.       event.getNodeB().collideWith(ray, results);
  210.      
  211.        Ray ray1 = new Ray(geo.getWorldTranslation(), NEGATIVE_X);
  212.       event.getNodeB().collideWith(ray1, results);
  213.      
  214.        Ray ray2 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_X);
  215.       event.getNodeB().collideWith(ray2, results);
  216.      
  217.        Ray ray3 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_Y);
  218.       event.getNodeB().collideWith(ray3, results);
  219.      
  220.       Ray ray4 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_Z);
  221.       event.getNodeB().collideWith(ray4, results);
  222.      
  223.       Ray ray5 = new Ray(geo.getWorldTranslation(), NEGATIVE_Z);
  224.       event.getNodeB().collideWith(ray5, results);
  225.      
  226.      
  227.      
  228.      
  229.      
  230.       if(results.size() > 0)
  231.              vehicleControler.setLinearVelocity((results.getClosestCollision().getContactNormal()).mult(100));
  232.      
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement