Sinking Character

Hi, i basically took the “Hello Collision” Code from the tutorials, exchanged the sceneModel with a large box and thats it.



Problem is: the player sinks into the box pops out again (very fast though). Do you know how to fix it. Heres the code, you can just paste and run it on your machine to see the strange behaviour.

[java]

import com.jme3.app.SimpleApplication;

import com.jme3.asset.plugins.ZipLocator;

import com.jme3.bullet.BulletAppState;

import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.bullet.control.CharacterControl;

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.light.AmbientLight;

import com.jme3.light.DirectionalLight;

import com.jme3.material.Material;

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;

import com.jme3.scene.shape.Box;



public class PlatformDebug2 extends SimpleApplication

implements ActionListener {

private Spatial sceneModel;



//instead of a scene take a box

Box box;

Geometry g_box;

Material m_box;

Node n_box;

CollisionShape c_box;

RigidBodyControl r_box;



private BulletAppState bulletAppState;

private RigidBodyControl landscape;

private CharacterControl player;

private Vector3f walkDirection = new Vector3f();

private boolean left = false, right = false, up = false, down = false;

public static void main(String[] args) {

PlatformDebug2 app = new PlatformDebug2();

app.start();

}

public void simpleInitApp() {

/** Set up Physics /

bulletAppState = new BulletAppState();

stateManager.attach(bulletAppState);

bulletAppState.getPhysicsSpace().enableDebug(assetManager);

// We re-use the flyby camera for rotation, while positioning is handled by physics

viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

flyCam.setMoveSpeed(100);

setUpKeys();

setUpLight();



//init player

CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);

player = new CharacterControl(capsuleShape, 0.05f);

player.setJumpSpeed(20);

player.setFallSpeed(30);

player.setGravity(30);

player.setPhysicsLocation(new Vector3f(0, 10, 0));

bulletAppState.getPhysicsSpace().add(player);



//add the box

box = new Box(Vector3f.ZERO, 30, 30, 30);

g_box = new Geometry(“floor”, box);

m_box = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

m_box.setColor(“Color”, ColorRGBA.Green);

g_box.setMaterial(m_box);

n_box = new Node();

n_box.attachChild(g_box);

c_box = CollisionShapeFactory.createBoxShape(g_box);

r_box = new RigidBodyControl(c_box, 0);

n_box.addControl(r_box);

r_box.setPhysicsLocation(new Vector3f(0, 0, 0));

bulletAppState.getPhysicsSpace().add(r_box);

rootNode.attachChild(n_box);

}

private void setUpLight() {

// We add light so we see the scene

AmbientLight al = new AmbientLight();

al.setColor(ColorRGBA.White.mult(1.3f));

rootNode.addLight(al);

DirectionalLight dl = new DirectionalLight();

dl.setColor(ColorRGBA.White);

dl.setDirection(new Vector3f(2.8f, -2.8f, -2.8f).normalizeLocal());

rootNode.addLight(dl);

}

/
* We over-write some navigational key mappings here, so we can

  • add physics-controlled walking and jumping: /

    private void setUpKeys() {

    inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));

    inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));

    inputManager.addMapping("Up", new KeyTrigger(KeyInput.KEY_W));

    inputManager.addMapping("Down", new KeyTrigger(KeyInput.KEY_S));

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

    inputManager.addListener(this, "Left");

    inputManager.addListener(this, "Right");

    inputManager.addListener(this, "Up");

    inputManager.addListener(this, "Down");

    inputManager.addListener(this, "Jump");

    }

    /
    * These are our custom actions triggered by key presses.
  • We do not walk yet, we just keep track of the direction the user pressed. */

    public void onAction(String binding, boolean value, float tpf) {

    if (binding.equals("Left")) {

    left = value;

    } else if (binding.equals("Right")) {

    right = value;

    } else if (binding.equals("Up")) {

    up = value;

    } else if (binding.equals("Down")) {

    down = value;

    } else if (binding.equals("Jump")) {

    player.jump();

    }

    }

    /**
  • This is the main event loop–walking happens here.
  • We check in which direction the player is walking by interpreting
  • the camera direction forward (camDir) and to the side (camLeft).
  • The setWalkDirection() command is what lets a physics-controlled player walk.
  • We also make sure here that the camera moves with player.

    */

    @Override

    public void simpleUpdate(float tpf) {

    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);

    Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);

    walkDirection.set(0, 0, 0);

    if (left) { walkDirection.addLocal(camLeft); }

    if (right) { walkDirection.addLocal(camLeft.negate()); }

    if (up) { walkDirection.addLocal(camDir); }

    if (down) { walkDirection.addLocal(camDir.negate()); }

    player.setWalkDirection(walkDirection);

    cam.setLocation(player.getPhysicsLocation());

    }

    }

    [/java]



    Now you maybe want to tell me to chop the big box into smaller ones. I have tried this already and the problem got even worse. Thanks for constructive solicitousness.



    EDIT:

    exchanged

    [java]

    c_box = CollisionShapeFactory.createBoxShape(g_box);

    [/java]

    with

    [java]

    c_box = CollisionShapeFactory.createMeshShape(g_box);

    [/java]

    and the strange behaviour stopped.

Just don’t use huge boxes. Oh and read the manual, it contains a lot of random details that are hard to tell via forum.