Hey, I’m quite new on jme3, and I was working through the beginner tutorials. I am at the “HelloCollision” tutorial right now and I have this problem now: Although I have created the physicsengine with the BulletAppState, my controls, e. g my player control, do not fall down. What do I wrong?
Here is my Code:
> package de.tuxchan;
>
> import com.jme3.animation.AnimChannel;
> import com.jme3.animation.AnimControl;
> import com.jme3.animation.AnimEventListener;
> import com.jme3.app.SimpleApplication;
> import com.jme3.bullet.BulletAppState;
> import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
> import com.jme3.bullet.collision.shapes.CollisionShape;
> import com.jme3.bullet.control.BetterCharacterControl;
> import com.jme3.bullet.control.CharacterControl;
> import com.jme3.bullet.control.RigidBodyControl;
> import com.jme3.bullet.util.CollisionShapeFactory;
> import com.jme3.font.BitmapText;
> import com.jme3.input.KeyInput;
> import com.jme3.input.controls.ActionListener;
> import com.jme3.input.controls.AnalogListener;
> import com.jme3.input.controls.KeyTrigger;
> import com.jme3.light.AmbientLight;
> import com.jme3.math.ColorRGBA;
> import com.jme3.math.Vector3f;
> import com.jme3.scene.Geometry;
> import com.jme3.scene.Node;
> import com.jme3.scene.Spatial;
>
> public class TerrainTest extends SimpleApplication implements AnimEventListener, ActionListener, AnalogListener {
>
> // For physicsengine
> private BulletAppState bulletAppState;
> private RigidBodyControl terrainControl;
> private CollisionShape terrainShape;
> private Spatial terrain;
> private CharacterControl player;
> private CapsuleCollisionShape playerShape;
> private boolean forward = false, backward = false, left = false, right = false;
>
> private Vector3f camDir = new Vector3f();
> private Vector3f camLeft = new Vector3f();
> private Vector3f walkDirection = new Vector3f();
>
> private BitmapText camLoc;
>
> @Override
> public void simpleInitApp() {
> bulletAppState = new BulletAppState();
> stateManager.attach(bulletAppState);
>
> bulletAppState.setDebugEnabled(true);
>
> flyCam.setMoveSpeed(100);
>
> terrain = assetManager.loadModel("Scenes/Terrain.j3o");
> terrain.setLocalTranslation(Vector3f.ZERO);
> terrainShape = CollisionShapeFactory.createMeshShape(terrain);
> terrainControl = new RigidBodyControl(terrainShape, 0);
> terrain.addControl(terrainControl);
> terrain.setLocalTranslation(Vector3f.ZERO);
> rootNode.attachChild(terrain);
>
> playerShape = new CapsuleCollisionShape(1.5f, 6, 1);
> player = new CharacterControl(playerShape, 0.5f);
> player.setFallSpeed(20);
> player.setGravity(new Vector3f(0, -30f, 0));
> player.setJumpSpeed(20);
> player.setPhysicsLocation(new Vector3f(0, 30, 0));
>
> cam.setLocation(player.getPhysicsLocation().add(0, 5, 0));
>
> AmbientLight al = new AmbientLight();
> al.setColor(ColorRGBA.White);
> rootNode.addLight(al);
>
> bulletAppState.getPhysicsSpace().add(terrain);
> bulletAppState.getPhysicsSpace().add(player);
>
> initKeys();
> initBitmapText();
> }
>
> public void initBitmapText() {
>
> guiNode.detachAllChildren();
> guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
> camLoc = new BitmapText(guiFont, false);
> camLoc.setSize(20);
> camLoc.setText("(x|y|z) : (" + cam.getLocation().x + "|" + cam.getLocation().y + "|" + cam.getLocation().z + ")");
> camLoc.setLocalTranslation(settings.getWidth()/2 - camLoc.getLineWidth()/2, settings.getHeight(), 0);
> guiNode.attachChild(camLoc);
>
> }
>
> public void initKeys() {
> inputManager.addMapping("forward", new KeyTrigger(KeyInput.KEY_W));
> inputManager.addMapping("backward", new KeyTrigger(KeyInput.KEY_S));
> inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_A));
> inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_D));
> inputManager.addMapping("jump", new KeyTrigger(KeyInput.KEY_SPACE));
>
> inputManager.addListener(this, "forward", "backward", "left", "right", "jump");
> }
>
> @Override
> public void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
> // unused
> }
>
> @Override
> public void onAnimCycleDone(AnimControl control, AnimChannel channel, String animName) {
>
> }
>
> @Override
> public void onAction(String name, boolean keyPressed, float tpf) {
>
> }
>
> @Override
> public void onAnalog(String name, float value, float tpf) {
> if(name.equals("forward")) {
> forward = true;
> }
> else {
> forward = false;
> }
>
> if(name.equals("backward")) {
> backward = true;
> }
> else {
> backward = false;
> }
>
> if(name.equals("left")) {
> left = true;
> }
> else {
> left = false;
> }
>
> if(name.equals("right")) {
> right = true;
> }
> else {
> right = false;
> }
>
> if(name.equals("jump")) {
> player.jump(new Vector3f(0, 20, 0));
> cam.setLocation(player.getPhysicsLocation().add(0, 5, 0));
> }
> }
>
> @Override
> public void simpleUpdate(float tpf) {
> camLoc.setText("(x|y|z) : (" + cam.getLocation().x + "|" + cam.getLocation().y + "|" + cam.getLocation().z + ")");
>
> camDir = cam.getDirection().multLocal(2.3f);
> camLeft = cam.getDirection().multLocal(2.3f);
> walkDirection.set(player.getPhysicsLocation());
>
> if(forward) {
> walkDirection = camDir;
> player.setWalkDirection(walkDirection);
> }
> if(backward) {
> walkDirection = camDir.negate();
> player.setWalkDirection(walkDirection);
> }
> if(left) {
> walkDirection = camLeft;
> player.setWalkDirection(walkDirection);
> }
> if(right) {
> walkDirection = camLeft.negate();
> player.setWalkDirection(walkDirection);
> }
>
> cam.setLocation(player.getPhysicsLocation().add(0, 5, 0));
> }
>
> public static void main (String[] args) {
> TerrainTest app = new TerrainTest();
> app.start();
> }
> }