Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mygame;
- import com.jme3.app.SimpleApplication;
- import com.jme3.bullet.BulletAppState;
- import com.jme3.bullet.PhysicsSpace;
- import com.jme3.bullet.PhysicsTickListener;
- import com.jme3.bullet.collision.PhysicsCollisionEvent;
- import com.jme3.bullet.collision.PhysicsCollisionListener;
- import com.jme3.bullet.control.RigidBodyControl;
- import com.jme3.collision.CollisionResults;
- import com.jme3.input.KeyInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.material.Material;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Ray;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.shape.Box;
- import java.util.Random;
- /**
- * This is the Main Class of your Game. You should only do initialization here.
- * Move your Logic into AppStates or Controls
- * @author normenhansen
- */
- public class Main extends SimpleApplication implements PhysicsTickListener, PhysicsCollisionListener {
- Quaternion quaternion;
- Vector3f loc;
- Random random = new Random();
- Geometry geo;
- float deltaTime;
- RigidBodyControl vehicleControler;
- int i;
- BulletAppState appState;
- public static void main(String[] args) {
- Main app = new Main();
- app.setShowSettings(false);
- app.start();
- }
- ActionListener actionListener = new ActionListener() {
- @Override
- public void onAction(String name, boolean keyPressed, float tpf) {
- if(keyPressed){
- if ("Up".equals(name))
- i = 1;
- if ("Down".equals(name));
- i = 2;
- if ("Left".equals(name))
- i = 3;
- if ("Right".equals(name))
- i = 4;
- if ("Uper".equals(name))
- i = 5;
- if ("Downer".equals(name))
- i = 6;
- }
- }
- };
- @Override
- public void simpleInitApp() {
- flyCam.setMoveSpeed(100);
- appState = new BulletAppState();
- stateManager.attach(appState);
- appState.getPhysicsSpace().addTickListener(this);
- appState.getPhysicsSpace().addCollisionListener(this);
- appState.getPhysicsSpace().setGravity(new Vector3f(0, -9.8f, 0));
- Spatial terrain = assetManager.loadModel("Textures/GameScene.j3o");
- terrain.setLocalTranslation(0, -100, 0);
- RigidBodyControl terrainPhysics = new RigidBodyControl(0);
- terrain.addControl(terrainPhysics);
- appState.getPhysicsSpace().add(terrainPhysics);
- rootNode.attachChild(terrain);
- Box box = new Box(10, 2f, 10);
- Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- geo = new Geometry("Vehicle", box);
- geo.setMaterial(mat);
- geo.setCullHint(Spatial.CullHint.Never);
- vehicleControler = new RigidBodyControl(2);
- geo.addControl(vehicleControler);
- appState.getPhysicsSpace().add(vehicleControler);
- appState.getPhysicsSpace().setGravity(new Vector3f(0, -9.8f, 0));
- vehicleControler.setPhysicsLocation(new Vector3f(0, -50, 0));
- //vehicleControler.setFriction(0);
- rootNode.attachChild(geo);
- inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_1));
- inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_2));
- inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_3));
- inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_4));
- inputManager.addMapping("Uper", new KeyTrigger(KeyInput.KEY_9));
- inputManager.addMapping("Downer", new KeyTrigger(KeyInput.KEY_0));
- inputManager.addListener(actionListener, "Up");
- inputManager.addListener(actionListener, "Down");
- inputManager.addListener(actionListener, "Left");
- inputManager.addListener(actionListener, "Right");
- inputManager.addListener(actionListener, "Uper");
- inputManager.addListener(actionListener, "Downer");
- quaternion = new Quaternion();
- quaternion.fromAngles(0, 0, 0);
- // Vector3f loc = vehicleControler.getPhysicsLocation();
- // vehicleControler.setPhysicsLocation(new Vector3f(loc.x, loc.y + 10, loc.z));
- }
- @Override
- public void simpleUpdate(float tpf) {
- }
- @Override
- public void simpleRender(RenderManager rm) {
- //TODO: add render code
- }
- @Override
- public void prePhysicsTick(PhysicsSpace space, float tpf) {
- vehicleControler.setPhysicsRotation(quaternion);
- deltaTime += tpf;
- switch (i) {
- case 1:
- vehicleControler.setLinearVelocity(new Vector3f(10, 0, 0));
- break;
- case 2:
- vehicleControler.setLinearVelocity(new Vector3f(-10, 0, 0));
- break;
- case 3:
- vehicleControler.setLinearVelocity(new Vector3f(0, 0, -10));
- break;
- case 4:
- vehicleControler.setLinearVelocity(new Vector3f(0, 0, +10));
- break;
- case 5:
- vehicleControler.setLinearVelocity(new Vector3f(0, 10, 0));
- break;
- case 6:
- vehicleControler.setLinearVelocity(new Vector3f(0, -10, 0));
- break;
- }
- // System.out.println("preTick called! Var i is :" + i);
- if (deltaTime >= 0.7){
- // vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x - 2 , loc.y - 2, loc.z -2));
- // vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x + 2 , loc.y - 2, loc.z +2));
- // vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x - 2 , loc.y - 2, loc.z +2));
- // vehicleControler.applyImpulse(new Vector3f(0, 3, 0), new Vector3f(loc.x + 2 , loc.y - 2, loc.z - 2));
- // vehicleControler.applyCentralForce(new Vector3f(0, 525, 0));
- // vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(2, 0, 2));
- // vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(-2, 0, -2));
- // vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(+2, 0, -2));
- // vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(-2, 0, +2));
- // vehicleControler.applyImpulse(new Vector3f(0, 200 * tpf, 0), new Vector3f(0, 0, 0));
- vehicleControler.setLinearVelocity(new Vector3f(0, 210 * tpf, 0));
- // System.out.println(vehicleControler.getPhysicsLocation());
- // if (loc.distance(new Vector3f(loc.x, loc.y - 5)))
- // vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(2, 0, 2));
- // vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(-2, 0, -2));
- // vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(+2, 0, -2));
- // vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(-2, 0, +2));
- // vehicleControler.applyImpulse(new Vector3f(0, -0.25f, 0), new Vector3f(0, 0, 0));
- deltaTime = 0;
- }
- }
- @Override
- public void physicsTick(PhysicsSpace space, float tpf) {
- loc = vehicleControler.getPhysicsLocation();
- // System.out.println("phsyicsTick called!");
- // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- @Override
- public void collision(PhysicsCollisionEvent event) {
- Vector3f NEGATIVE_Y = new Vector3f(0, -1, 0);
- Vector3f NEGATIVE_X = new Vector3f(-1, 0, 0);
- Vector3f NEGATIVE_Z = new Vector3f(0, 0, -1);
- CollisionResults results = new CollisionResults();
- Ray ray = new Ray(geo.getWorldTranslation(), NEGATIVE_Y);
- event.getNodeB().collideWith(ray, results);
- Ray ray1 = new Ray(geo.getWorldTranslation(), NEGATIVE_X);
- event.getNodeB().collideWith(ray1, results);
- Ray ray2 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_X);
- event.getNodeB().collideWith(ray2, results);
- Ray ray3 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_Y);
- event.getNodeB().collideWith(ray3, results);
- Ray ray4 = new Ray(geo.getWorldTranslation(), Vector3f.UNIT_Z);
- event.getNodeB().collideWith(ray4, results);
- Ray ray5 = new Ray(geo.getWorldTranslation(), NEGATIVE_Z);
- event.getNodeB().collideWith(ray5, results);
- if(results.size() > 0)
- vehicleControler.setLinearVelocity((results.getClosestCollision().getContactNormal()).mult(100));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement