Whats wrong with this code?

Hi,

I am trying to make test program but some thing is wrong with this code.
The character is not standing on ground after adding BetterCharacterControl.
here is code and screen shot.

    Node base = (Node) assetManager.loadModel("Models/Base/Base.j3o");

    // activate physics
    BulletAppState bulletAppState = new BulletAppState();
    stateManager.attach(bulletAppState);
    bulletAppState.setDebugEnabled(true);

    PhysicsSpace physicsSpace = bulletAppState.getPhysicsSpace();
    RigidBodyControl rbc = new RigidBodyControl(0.0f);

    base.addControl(rbc);
    physicsSpace.add(base);

    rootNode.attachChild(base);
    rbc.setPhysicsLocation(base.getWorldTranslation());

    Node model = (Node) assetManager.loadModel("Models/Sinbad/Sinbad.mesh.j3o");
    model.setLocalScale(.50f);

    System.out.println(model.getWorldBound());
    BoundingBox box = (BoundingBox) model.getWorldBound();
    float yExtent = box.getYExtent() + (-box.getCenter().y);
    float xExtent = box.getXExtent();
    model.setLocalTranslation(new Vector3f(0, yExtent+1, 0));
    System.out.println(model.getLocalTranslation());

    BetterCharacterControl physicsCharacter = new BetterCharacterControl(0.9f, yExtent, .8f);
    model.addControl(physicsCharacter);
    physicsSpace.add(model);

    rootNode.attachChild(model);

The second parameter must be the height not half height.
If you change this line

float yExtent = box.getYExtent() + (-box.getCenter().y); 

in

 float yExtent = box.getYExtent();

It may work.

see the system.out statement… here is its output
BoundingBox [Center: (0.0, -0.24999976, 0.0) xExtent: 2.222134 yExtent: 2.2328453 zExtent: 1.174248]
(0.0, 2.482845, 0.0)

the Y centre of the character is not 0. Without adding BetterCharacterControl the model standing on correct location.
now see the result after doubling the height
“BetterCharacterControl physicsCharacter = new BetterCharacterControl(0.9f, yExtent * 2, .8f);”

The problem is that the center of better chatacter control is on the bottom of the rigidbody, while the center of your geometry is in the middle.

Try to add and offset to your spatial using a node, like this

node=new Node()
node.attachChild(yourspatial);
yourspatial.setLocalTranslation(0,height/2,0);

then, attach the rigidbody to the node instead.

Its working fine… thanks… :slight_smile:

That’s one flaw of this Sinbad model the center is at…well the center… :stuck_out_tongue: but usually it’s better to have the center of a model between the feet.

When you export a model just put the root and the root bone on feet :wink: this will solve every similar issue.

thanks… but I am using test models provided with JME… :wink:

I know,just giving you a tip to make models. Test models provided with jme are often old and not always up-to-date with last engine capabilities.