Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame.controls_states;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.collision.PhysicsCollisionEvent;
- import com.jme3.bullet.collision.PhysicsCollisionListener;
- import com.jme3.bullet.collision.PhysicsSweepTestResult;
- import com.jme3.bullet.collision.shapes.BoxCollisionShape;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.collision.CollisionResults;
- import com.jme3.input.InputManager;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.math.Ray;
- import com.jme3.math.Transform;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.AbstractControl;
- import com.jme3.scene.shape.Box;
- import java.util.List;
- /**
- *
- */
- public class VehicleController extends AbstractControl implements PhysicsCollisionListener {
- Vector3f movement = null;
- float rotLeft, rotRight;
- float deltaTime;
- public int signal = 0;
- Geometry vehicle;
- InputManager inputManager;
- float forward;
- float backwards;
- float left;
- float right;
- float up;
- float down;
- Spatial terrain;
- int hitpoints = 100;
- RigidBodyControl vehiclePhysics;
- BulletAppState appState;
- BoxCollisionShape shape = new BoxCollisionShape(new Vector3f(10, 2, 10));
- public VehicleController(InputManager inputManager, Geometry vehicle, Spatial terrain, RigidBodyControl vehiclePhysics, BulletAppState appState) {
- this.appState = appState;
- this.vehiclePhysics = vehiclePhysics;
- this.terrain = terrain;
- this.vehicle = vehicle;
- this.inputManager = inputManager;
- // Box box = new Box(1, 1, 1);
- // Material mat = new Material("MatDefs/Common/")
- initKeys();
- }
- AnalogListener analogListener = new AnalogListener() {
- @Override
- public void onAnalog(String name, float value, float tpf) {
- switch (name) {
- case "Forwards":
- forward += 1 ;
- break;
- case "Backwards":
- backwards += 1;
- break;
- case "Left":
- left += 1;
- break;
- case "Right":
- right += 1;
- break;
- case "Up":
- up += 1;
- break;
- case "Down":
- down += 1;
- break;
- case "RotRight":
- rotRight += 5;
- break;
- case "RotLeft":
- rotLeft += 5;
- break;
- }
- }
- };
- private void initKeys() {
- inputManager.addMapping("Forwards", new KeyTrigger(KeyInput.KEY_1));
- inputManager.addMapping("RotLeft", new KeyTrigger(KeyInput.KEY_2));
- inputManager.addMapping("RotRight", new KeyTrigger(KeyInput.KEY_3));
- // inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
- // inputManager.addMapping("Backwards", new KeyTrigger(KeyInput.KEY_2));
- // inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
- // inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
- // inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_9));
- // inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_0));
- inputManager.addListener(analogListener, "Forwards");
- inputManager.addListener(analogListener, "RotRight");
- inputManager.addListener(analogListener, "RotLeft");
- // inputManager.addListener(analogListener, "Backwards");
- // inputManager.addListener(analogListener, "Left");
- // inputManager.addListener(analogListener, "Right");
- // inputManager.addListener(analogListener, "Up");
- // inputManager.addListener(analogListener, "Down");
- }
- @Override
- protected void controlUpdate(float tpf) {
- deltaTime += tpf;
- if (left > 0) {
- Transform startPosLeft = new Transform(vehicle.getWorldTranslation().add(new Vector3f(-5, 0, 0)));
- Transform endPosLeft = new Transform(vehicle.getWorldTranslation().add(new Vector3f(-5, 0, 0)).add(new Vector3f(-left, 0, 0)));
- List<PhysicsSweepTestResult> sweepLeft = appState.getPhysicsSpace().sweepTest(shape, startPosLeft, endPosLeft);
- if (sweepLeft.isEmpty() && left > 1) {
- vehicle.move(-left, 0, 0);
- } else {
- left = 0;
- // right += 0.0006;
- }
- }
- if (right > 0) {
- Transform startPosRight = new Transform(vehicle.getWorldTranslation().add(new Vector3f(5, 0, 0)));
- Transform endPosRight = new Transform(vehicle.getWorldTranslation().add(new Vector3f(5, 0, 0)).add(new Vector3f(right, 0, 0)));
- List<PhysicsSweepTestResult> sweepRight = appState.getPhysicsSpace().sweepTest(shape, startPosRight, endPosRight);
- if (sweepRight.isEmpty() && right > 1) {
- vehicle.move(right, 0, 0);
- } else {
- right = 0;
- left += 5 * tpf;
- }
- }
- if (up > 0) {
- Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 1, 0)));
- Transform endPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 1, 0)).add(new Vector3f(0, up, 0)));
- System.out.println(up);
- List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
- if (sweepUp.isEmpty() && up > 1) {
- vehicle.move(0, up, 0);
- } else {
- up = 0;
- down += 5 * tpf;
- }
- }
- if (rotLeft > 0) {
- // Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
- // Transform endPosUp = new Transform(vehicle.getWorldRotation().fromAngles(0, -rotLeft , 0).mult(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5))));
- System.out.println(-rotLeft);
- // List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
- // if (sweepUp.isEmpty()) {
- vehicle.rotate(0, -rotLeft * tpf , 0);
- movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
- } //else// {
- rotLeft = 0;
- // rotRight += 2;
- //}
- if (rotRight > 0) {
- // Transform startPosUp = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
- // Transform endPosUp = new Transform(vehicle.getWorldRotation().fromAngles(0, -rotLeft , 0).mult(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5))));
- System.out.println(-rotLeft);
- // List<PhysicsSweepTestResult> sweepUp = appState.getPhysicsSpace().sweepTest(shape, startPosUp, endPosUp);
- // if (sweepUp.isEmpty()) {
- vehicle.rotate(0, rotRight * tpf , 0);
- movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
- } //else// {
- rotRight = 0;
- // rotRight += 2;
- //}
- if (down > 0) {
- Transform startPosDown = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, -1, 0)));
- Transform endPosDown = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, -1, 0)).add(new Vector3f(0, -down, 0)));
- List<PhysicsSweepTestResult> sweepDown = appState.getPhysicsSpace().sweepTest(shape, startPosDown, endPosDown);
- if (sweepDown.isEmpty()) {
- vehicle.move(0, -down, 0);
- } else {
- down = 0;
- // up += 0.0006;
- }
- }
- if (forward > 0) {
- Transform startPosForward = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)));
- Transform endPosForward = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, 5)).add(vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)));
- List<PhysicsSweepTestResult> sweepForward = appState.getPhysicsSpace().sweepTest(shape, startPosForward, endPosForward);
- if (movement == null)
- movement = (vehicle.getWorldRotation().mult(Vector3f.UNIT_Z)).mult(1);
- if (sweepForward.isEmpty()) {
- vehicle.move(movement.x * forward * tpf, movement.y * forward * tpf, movement.z * forward * tpf);
- } else {
- forward = 0;
- backwards += 10 * tpf;
- }
- }
- if (backwards > 0) {
- Transform startPosBackwards = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, -5)));
- Transform endPosBackwards = new Transform(vehicle.getWorldTranslation().add(new Vector3f(0, 0, -5)).add(new Vector3f(0, 0, -backwards)));
- List<PhysicsSweepTestResult> sweepForward = appState.getPhysicsSpace().sweepTest(shape, startPosBackwards, endPosBackwards);
- if (sweepForward.isEmpty()) {
- vehicle.move(0, 0, -backwards);
- } else {
- backwards = 0;
- // forward += 0.0006;
- }
- }
- if (forward > 0) {
- forward -= 0.4 * tpf;
- }
- //
- if (forward < 0) {
- forward = 0;
- }
- if (backwards > 0) {
- backwards -= 5 * tpf;
- }
- if (backwards < 0) {
- backwards = 5 * tpf;
- }
- // if (right > 0) {
- // right -= 0.0007;
- // }
- //
- // if (right < 0) {
- // right = 0;
- // }
- if (up > 0) {
- up -= 5 * tpf;
- }
- if (up < 0) {
- up = 0;
- }
- // if (left > 0) {
- // left -= 0.0007;
- // }
- //
- // if (left < 0) {
- // left = 0;
- // }
- if (down > 0) {
- down -= 0.0007;
- }
- if (down < 0) {
- down = 0;
- }
- if (rotLeft > 0) {
- rotLeft -= 2 * tpf;
- }
- if (rotLeft < 0) {
- rotLeft = 0 * tpf;
- }
- if (rotRight > 0) {
- rotRight -= 2 * tpf;
- }
- if (rotRight < 0) {
- rotRight = 0 * tpf;
- }
- if (movement != null) {
- if (movement.x != 0) {
- movement.x -= 0.0007;
- }
- if (movement.y != 0) {
- movement.y -= 0.0007;
- }
- if (movement.z != 0) {
- movement.z -= 0.0007;
- }
- System.out.println(movement);
- }
- //System.out.println("Right is!" + right);
- deltaTime = 0;
- }
- @Override
- protected void controlRender(RenderManager rm, ViewPort vp) {
- }
- @Override
- public void collision(PhysicsCollisionEvent event) {
- // Vector3f
- //
- // CollisionResults results = new CollisionResults();
- // Ray ray = new Ray(vehicle.getWorldTranslation().add(-5, 0, -5), Vector3f.UNIT_Z);
- // terrain.collideWith(ray, results);
- //
- // Ray ray1 = new Ray(vehicle.getWorldTranslation().add(-5, 0, -5), Vector3f.UNIT_Z);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment