[SOLVED] Character doesn't jump

package com.waystudio;

import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.math.Vector3f;

public class MMMDCCXI extends SimpleApplication
        implements ActionListener {

    private BetterCharacterControl character;

    public static void main(String[] args) {
        MMMDCCXI app = new MMMDCCXI();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        initPhysics();
        initInput();
    }

    private void initPhysics() {
        BulletAppState bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);

        bulletAppState.setDebugEnabled(true);

        PhysicsSpace space = bulletAppState.getPhysicsSpace();

        character = new BetterCharacterControl(1f, 3f, 1f);
        space.add(character);

        PhysicsRigidBody floor = new PhysicsRigidBody(new BoxCollisionShape(10f, 0.1f, 10f), 0f);
        floor.setPhysicsLocation(new Vector3f(0, -10f, 0));
        space.add(floor);
    }

     private void initInput() {
        inputManager.addListener(this, "Jump");

        inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
     }

    @Override
    public void onAction(String name, boolean isPressed, float tpf) {
        if (name.equals("Jump")) {
            if (character.isOnGround()) {
                character.jump();
            }
        }
    }
}
1 Like

I tried this in the debugger and found that isOnGround() returns false when it should return true. I continue to investigate.

Edit: I found the issue. BetterCharacterControl is a physics control; in order to function properly, it must be added to the scene graph somewhere, but it isn’t. As a result, its update() method never gets invoked. As a result, its location field is never updated.

Adding the following code at the end of initPhysics() solves your issue:

        Node characterNode = new Node();
        characterNode.addControl(character);
        rootNode.attachChild(characterNode);
3 Likes