From 70d74963249ba4c154da27c591e30f2865a89f08 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Tue, 10 Mar 2009 17:18:05 +0000 Subject: [PATCH] --- src/sg/SpaceGame.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/sg/SpaceGame.java b/src/sg/SpaceGame.java index 325e116..77901b4 100644 --- a/src/sg/SpaceGame.java +++ b/src/sg/SpaceGame.java @@ -9,9 +9,15 @@ import com.jme.bounding.BoundingBox; import com.jme.scene.shape.Sphere; import com.jme.scene.state.TextureState; import com.jme.image.Texture; +import com.jme.input.InputHandler; +import com.jme.input.KeyInput; +import com.jme.input.action.InputAction; +import com.jme.input.action.InputActionEvent; import com.jme.math.Vector3f; import com.jme.util.TextureManager; import com.jmex.physics.DynamicPhysicsNode; +import com.jmex.physics.PhysicsSpace; +import com.jmex.physics.PhysicsUpdateCallback; import com.jmex.physics.StaticPhysicsNode; import com.jmex.physics.util.SimplePhysicsGame; @@ -19,6 +25,7 @@ public class SpaceGame extends SimplePhysicsGame { private StaticPhysicsNode staticNode; private DynamicPhysicsNode dynamicNode; private Environment environment; + private InputHandler physicsStepInputHandler; public static void main(String[] args) { Logger.getLogger("").setLevel( Level.WARNING ); @@ -43,6 +50,21 @@ public class SpaceGame extends SimplePhysicsGame { //light //TODO + + //controll + physicsStepInputHandler = new InputHandler(); + getPhysicsSpace().addToUpdateCallbacks( new PhysicsUpdateCallback() { + public void beforeStep( PhysicsSpace space, float time ) { + physicsStepInputHandler.update( time ); + } + public void afterStep( PhysicsSpace space, float time ) { + + } + } ); + physicsStepInputHandler.addAction( new MyInputAction( new Vector3f( 700, 0, 0 ) ), + InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_HOME, InputHandler.AXIS_NONE, true ); + physicsStepInputHandler.addAction( new MyInputAction( new Vector3f( -700, 0, 0 ) ), + InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_END, InputHandler.AXIS_NONE, true ); //sphere Sphere s = new Sphere("Sphere", 10, 10, 25); @@ -64,4 +86,18 @@ public class SpaceGame extends SimplePhysicsGame { public void simpleUpdate(){ environment.update(); } + + private class MyInputAction extends InputAction { + private final Vector3f direction; + private final Vector3f appliedForce = new Vector3f(); + + public MyInputAction( Vector3f direction ) { + this.direction = direction; + } + + public void performAction( InputActionEvent evt ) { + appliedForce.set( direction ).multLocal( evt.getTime() ); + dynamicNode.addForce( appliedForce ); + } + } } \ No newline at end of file