Hi guys!
Today I tested my first JME PHYSICS and I have to say: It is so great! I love playing with falling cubes and stuff.
But now to something different: I try to move a simple plane (a box) with keys. I managed to apply a force to it so that it moves. That works fine, but is very confusing.
What I want to achieve is a working application that allows the user to move the plane with 4 keys. As you can see in my code, I want to place a sphere on it to have a first game. The problem is that all forces are applied to the local coordinates of the object, I guess. So the object moves in an unexpectable way when you press a lot of keys. I want to apply the force in world coordinates (I hope I got that right) so the plane only moves in X and Z axis.
Excuse my bad explanation, but I'm not a native english speaker…
Hope someone can help me!
import java.util.logging.Level;
import com.jme.app.SimpleGame;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.action.InputAction;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.scene.state.AlphaState;
import com.jme.scene.state.MaterialState;
import com.jme.util.LoggingSystem;
import com.jmex.physics.DynamicPhysicsNode;
import com.jmex.physics.util.SimplePhysicsGame;
public class PhysicTest extends SimplePhysicsGame {
protected void simpleInitGame() {
// first we will create the floor like in Lesson2
final DynamicPhysicsNode staticNode = getPhysicsSpace().createDynamicNode();
rootNode.attachChild( staticNode );
final Box visualFloor = new Box( "floor", new Vector3f(), 10, 0.25f, 10 );
staticNode.attachChild( visualFloor );
visualFloor.getLocalTranslation().set( 0, 0, 0 );
staticNode.generatePhysicsGeometry();
staticNode.setAffectedByGravity(false);
InputAction left = new KeyInputAction() {
@Override
public void performAction(InputActionEvent arg0) {
// System.out.println("left!");
staticNode.addForce(
new Vector3f(0, 1, 0),
new Vector3f(1, 0, 0));
staticNode.addForce(
new Vector3f(0, -1, 0),
new Vector3f(-1, 0, 0));
}
};
InputAction right = new KeyInputAction() {
@Override
public void performAction(InputActionEvent arg0) {
// System.out.println("right!");
staticNode.addForce(
new Vector3f(0, -1, 0),
new Vector3f(1, 0, 0));
staticNode.addForce(
new Vector3f(0, 1, 0),
new Vector3f(-1, 0, 0));
}
};
InputAction up = new KeyInputAction() {
@Override
public void performAction(InputActionEvent arg0) {
// System.out.println("up!");
staticNode.addForce(
new Vector3f(0, 0, 1),
new Vector3f(0, -1, 0));
staticNode.addForce(
new Vector3f(0, 0, -1),
new Vector3f(0, 1, 0));
}
};
InputAction down = new KeyInputAction() {
@Override
public void performAction(InputActionEvent arg0) {
// System.out.println("down!");
staticNode.addForce(
new Vector3f(0, 0, -1),
new Vector3f(0, -1, 0));
staticNode.addForce(
new Vector3f(0, 0, 1),
new Vector3f(0, 1, 0));
}
};
KeyBindingManager keyboard = KeyBindingManager.getKeyBindingManager();
keyboard.set("left", KeyInput.KEY_H);
input.addAction(left, "left", false);
keyboard.set("right", KeyInput.KEY_K);
input.addAction(right, "right", false);
keyboard.set("up", KeyInput.KEY_U);
input.addAction(up, "up", false);
keyboard.set("down", KeyInput.KEY_J);
input.addAction(down, "down", false);
// DynamicPhysicsNode dynamicNode = createSphere();
// dynamicNode.getLocalTranslation().set( 0, 5, 0 );
pause = false;
}
public static void main( String[] args ) {
LoggingSystem.getLogger().setLevel( Level.WARNING ); // to see the important stuff
PhysicTest l = new PhysicTest();
l.setDialogBehaviour(SimpleGame.FIRSTRUN_OR_NOCONFIGFILE_SHOW_PROPS_DIALOG);
l.start();
}
}