ComputerCraft32

VehicleController

Jul 9th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.11 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package mygame.controls_states;
  7.  
  8. import com.jme3.bullet.BulletAppState;
  9. import com.jme3.bullet.collision.PhysicsCollisionEvent;
  10. import com.jme3.bullet.collision.PhysicsCollisionListener;
  11. import com.jme3.bullet.collision.PhysicsSweepTestResult;
  12. import com.jme3.bullet.collision.shapes.BoxCollisionShape;
  13. import com.jme3.bullet.control.RigidBodyControl;
  14. import com.jme3.collision.CollisionResults;
  15. import com.jme3.input.InputManager;
  16. import com.jme3.input.KeyInput;
  17. import com.jme3.input.controls.AnalogListener;
  18. import com.jme3.input.controls.KeyTrigger;
  19. import com.jme3.math.Ray;
  20. import com.jme3.math.Transform;
  21. import com.jme3.math.Vector3f;
  22. import com.jme3.renderer.RenderManager;
  23. import com.jme3.renderer.ViewPort;
  24. import com.jme3.scene.Geometry;
  25. import com.jme3.scene.Spatial;
  26. import com.jme3.scene.control.AbstractControl;
  27. import com.jme3.scene.shape.Box;
  28. import java.util.List;
  29.  
  30. /**
  31.  *
  32.  
  33.  */
  34. public class VehicleController extends AbstractControl implements PhysicsCollisionListener {
  35.    
  36.     Vector3f movement = null;
  37.     float rotLeft, rotRight;
  38.     float deltaTime;
  39.     public int signal = 0;
  40.     Geometry vehicle;
  41.     InputManager inputManager;
  42.     float forward;
  43.     float backwards;
  44.     float left;
  45.     float right;
  46.     float up;
  47.     float down;
  48.     Spatial terrain;
  49.     int hitpoints = 100;
  50.     RigidBodyControl vehiclePhysics;
  51.     BulletAppState appState;
  52.     BoxCollisionShape shape = new BoxCollisionShape(new Vector3f(10, 2, 10));
  53.  
  54.     public VehicleController(InputManager inputManager, Geometry vehicle, Spatial terrain, RigidBodyControl vehiclePhysics, BulletAppState appState) {
  55.        
  56.        
  57.         this.appState = appState;
  58.         this.vehiclePhysics = vehiclePhysics;
  59.         this.terrain = terrain;
  60.         this.vehicle = vehicle;
  61.         this.inputManager = inputManager;
  62.        
  63. //        Box box = new Box(1, 1, 1);
  64. //        Material mat = new Material("MatDefs/Common/")
  65.        
  66.        
  67.         initKeys();
  68.     }
  69.  
  70.     AnalogListener analogListener = new AnalogListener() {
  71.         @Override
  72.         public void onAnalog(String name, float value, float tpf) {
  73.             switch (name) {
  74.                 case "Forwards":
  75.  
  76.                     forward += 1 ;
  77.                     break;
  78.                 case "Backwards":
  79.  
  80.                     backwards += 1;
  81.                     break;
  82.                 case "Left":
  83.  
  84.                     left += 1;
  85.                     break;
  86.                 case "Right":
  87.  
  88.                     right += 1;
  89.                     break;
  90.                 case "Up":
  91.  
  92.                     up += 1;
  93.                     break;
  94.                 case "Down":
  95.  
  96.                     down += 1;
  97.                     break;
  98.  
  99.                 case "RotRight":
  100.  
  101.                     rotRight += 5;
  102.                     break;
  103.                 case "RotLeft":
  104.  
  105.                     rotLeft += 5;
  106.                     break;
  107.  
  108.             }
  109.         }
  110.  
  111.     };
  112.  
  113.     private void initKeys() {
  114.  
  115.         inputManager.addMapping("Forwards", new KeyTrigger(KeyInput.KEY_1));
  116.         inputManager.addMapping("RotLeft", new KeyTrigger(KeyInput.KEY_2));
  117.         inputManager.addMapping("RotRight", new KeyTrigger(KeyInput.KEY_3));
  118. //        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
  119. //        inputManager.addMapping("Backwards", new KeyTrigger(KeyInput.KEY_2));
  120. //        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
  121. //        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
  122. //        inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_9));
  123. //        inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_0));
  124.  
  125.         inputManager.addListener(analogListener, "Forwards");
  126.         inputManager.addListener(analogListener, "RotRight");
  127.         inputManager.addListener(analogListener, "RotLeft");
  128. //        inputManager.addListener(analogListener, "Backwards");
  129. //        inputManager.addListener(analogListener, "Left");
  130. //        inputManager.addListener(analogListener, "Right");
  131. //        inputManager.addListener(analogListener, "Up");
  132. //        inputManager.addListener(analogListener, "Down");
  133.     }
  134.  
  135.     @Override
  136.     protected void controlUpdate(float tpf) {
  137.         deltaTime += tpf;
  138.  
  139.         if (left > 0) {
  140.             Transform startPosLeft = new Transform(vehicle.getWorldTranslation().add(new Vector3f(-5, 0, 0)));
  141.             Transform endPosLeft = new Transform(vehicle.getWorldTranslation().add(new Vector3f(-5, 0, 0)).add(new Vector3f(-left, 0, 0)));
  142.  
  143.             List<PhysicsSweepTestResult> sweepLeft = appState.getPhysicsSpace().sweepTest(shape, startPosLeft, endPosLeft);
  144.  
  145.             if (sweepLeft.isEmpty() && left > 1) {
  146.                 vehicle.move(-left, 0, 0);
  147.             } else {
  148.                 left = 0;
  149.                 //    right += 0.0006;
  150.             }
  151.  
  152.         }
  153.  
  154.         if (right > 0) {
  155.             Transform startPosRight = new Transform(vehicle.getWorldTranslation().add(new Vector3f(5, 0, 0)));
  156.             Transform endPosRight = new Transform(vehicle.getWorldTranslation().add(new Vector3f(5, 0, 0)).add(new Vector3f(right, 0, 0)));
  157.  
  158.             List<PhysicsSweepTestResult> sweepRight = appState.getPhysicsSpace().sweepTest(shape, startPosRight, endPosRight);
  159.  
  160.             if (sweepRight.isEmpty() && right > 1) {
  161.                 vehicle.move(right, 0, 0);
  162.             } else {
  163.                 right = 0;
  164.                    left += 5 * tpf;
  165.             }
  166.  
  167.         }
  168.  
  169.         if (up > 0) {
  170.             Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 1, 0)));
  171.             Transform endPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 1, 0)).add(new Vector3f(0, up, 0)));
  172.             System.out.println(up);
  173.             List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
  174.  
  175.             if (sweepUp.isEmpty() && up > 1) {
  176.                 vehicle.move(0, up, 0);
  177.             } else {
  178.                 up = 0;
  179.                  down += 5 * tpf;
  180.             }
  181.         }
  182.         if (rotLeft > 0) {
  183. //            Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
  184. //            Transform endPosUp = new Transform(vehicle.getWorldRotation().fromAngles(0, -rotLeft , 0).mult(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5))));
  185.             System.out.println(-rotLeft);
  186. //            List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
  187.            
  188. //            if (sweepUp.isEmpty()) {
  189.                 vehicle.rotate(0, -rotLeft * tpf , 0);
  190.                 movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
  191.             } //else// {
  192.                rotLeft = 0;
  193.              //    rotRight += 2;
  194.             //}
  195.          
  196.            if (rotRight > 0) {
  197. //            Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
  198. //            Transform endPosUp = new Transform(vehicle.getWorldRotation().fromAngles(0, -rotLeft , 0).mult(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5))));
  199.             System.out.println(-rotLeft);
  200. //            List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
  201.            
  202. //            if (sweepUp.isEmpty()) {
  203.                 vehicle.rotate(0, rotRight * tpf , 0);
  204.                 movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
  205.             } //else// {
  206.                rotRight = 0;
  207.             //     rotRight += 2;
  208.             //}
  209.  
  210.        
  211.  
  212.         if (down > 0) {
  213.             Transform startPosDown = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, -1, 0)));
  214.             Transform endPosDown = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, -1, 0)).add(new Vector3f(0, -down, 0)));
  215.  
  216.             List<PhysicsSweepTestResult> sweepDown = appState.getPhysicsSpace().sweepTest(shape, startPosDown, endPosDown);
  217.  
  218.             if (sweepDown.isEmpty()) {
  219.                 vehicle.move(0, -down, 0);
  220.             } else {
  221.                 down = 0;
  222.                 //   up += 0.0006;
  223.             }
  224.         }
  225.        if (forward > 0) {
  226.             Transform startPosForward = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
  227.             Transform endPosForward = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)).add(vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)));
  228.  
  229.             List<PhysicsSweepTestResult> sweepForward = appState.getPhysicsSpace().sweepTest(shape, startPosForward, endPosForward);
  230.            
  231.             if (movement == null)
  232.                movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
  233.            
  234.             if (sweepForward.isEmpty()) {
  235.                
  236.                
  237.                 vehicle.move(movement.x * forward * tpf, movement.y * forward * tpf, movement.z * forward * tpf);
  238.             } else {
  239.                 forward = 0;
  240.                   backwards += 10 * tpf;
  241.             }
  242.  
  243.        }
  244.  
  245.         if (backwards > 0) {
  246.             Transform startPosBackwards = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, -5)));
  247.             Transform endPosBackwards = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, -5)).add(new Vector3f(0, 0, -backwards)));
  248.  
  249.             List<PhysicsSweepTestResult> sweepForward = appState.getPhysicsSpace().sweepTest(shape, startPosBackwards, endPosBackwards);
  250.  
  251.             if (sweepForward.isEmpty()) {
  252.                 vehicle.move(0, 0, -backwards);
  253.             } else {
  254.                 backwards = 0;
  255.                 //  forward += 0.0006;
  256.             }
  257.         }
  258.         if (forward > 0) {
  259.             forward -= 0.4 * tpf;
  260.        }
  261. //
  262.         if (forward < 0) {
  263.             forward = 0;
  264.         }
  265.         if (backwards > 0) {
  266.             backwards -= 5 * tpf;
  267.         }
  268.  
  269.         if (backwards < 0) {
  270.             backwards = 5 * tpf;
  271.         }
  272. //        if (right > 0) {
  273. //            right -= 0.0007;
  274. //        }
  275. //
  276. //        if (right < 0) {
  277. //            right = 0;
  278. //        }
  279.         if (up > 0) {
  280.             up -= 5 * tpf;
  281.         }
  282.  
  283.         if (up < 0) {
  284.             up = 0;
  285.         }
  286. //        if (left > 0) {
  287. //            left -= 0.0007;
  288. //        }
  289. //
  290. //        if (left < 0) {
  291. //            left = 0;
  292. //        }
  293.         if (down > 0) {
  294.             down -= 0.0007;
  295.         }
  296.  
  297.         if (down < 0) {
  298.             down = 0;
  299.         }
  300.        
  301.         if (rotLeft > 0) {
  302.             rotLeft -= 2 * tpf;
  303.         }
  304.  
  305.         if (rotLeft < 0) {
  306.             rotLeft = 0 * tpf;
  307.            
  308.         }
  309.        
  310.           if (rotRight > 0) {
  311.             rotRight -= 2 * tpf;
  312.         }
  313.  
  314.         if (rotRight < 0) {
  315.             rotRight = 0 * tpf;
  316.            
  317.         }
  318.        
  319.        
  320.    if (movement != null) {
  321.        
  322.         if (movement.x != 0) {
  323.             movement.x -= 0.0007;
  324.         }
  325.  
  326.        
  327.         if (movement.y !=  0) {
  328.             movement.y -= 0.0007;
  329.         }
  330.  
  331.         if (movement.z != 0) {
  332.             movement.z -= 0.0007;
  333.         }
  334.  
  335.         System.out.println(movement);
  336.    }      
  337.          
  338.        
  339.        
  340.        
  341.        
  342.        
  343.        
  344.        
  345.        
  346. //System.out.println("Right is!" + right);
  347.         deltaTime = 0;
  348.  
  349.     }
  350.  
  351.     @Override
  352.     protected void controlRender(RenderManager rm, ViewPort vp) {
  353.  
  354.     }
  355.  
  356.     @Override
  357.     public void collision(PhysicsCollisionEvent event) {
  358.        
  359.        
  360.        
  361.        
  362. //        Vector3f
  363. //        
  364. //        CollisionResults results = new CollisionResults();
  365. //        Ray ray = new Ray(vehicle.getWorldTranslation().add(-5, 0, -5), Vector3f.UNIT_Z);
  366. //        terrain.collideWith(ray, results);
  367. //        
  368. //         Ray ray1 = new Ray(vehicle.getWorldTranslation().add(-5, 0, -5), Vector3f.UNIT_Z);
  369.        
  370.     }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment