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.control.RigidBodyControl;
- 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.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.shape.Box;
- /**
- * 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 {
- 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;
- }
- }
- };
- @Override
- public void simpleInitApp() {
- flyCam.setMoveSpeed(100);
- appState = new BulletAppState();
- stateManager.attach(appState);
- 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(5, 2f, 5);
- Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
- Geometry 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));
- 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.addListener(actionListener, "Up");
- inputManager.addListener(actionListener, "Down");
- inputManager.addListener(actionListener, "Left");
- inputManager.addListener(actionListener, "Right");
- }
- @Override
- public void simpleUpdate(float tpf) {
- //TODO: add update code
- }
- @Override
- public void simpleRender(RenderManager rm) {
- //TODO: add render code
- }
- @Override
- public void prePhysicsTick(PhysicsSpace space, float 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(-10, 0, 0));
- break;
- case 4:
- vehicleControler.setLinearVelocity(new Vector3f(+10, 0, 0));
- break;
- }
- System.out.println("preTick called! Var i is :" + i);
- }
- @Override
- public void physicsTick(PhysicsSpace space, float tpf) {
- System.out.println("phsyicsTick called!");
- // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement