4
\$\begingroup\$

I am trying to update the location of the rigid body for a player class, as my player moves around I would like the collision body to also move with the player object (currently represented as a cube). Below is my current update method for when I want to update the xyz coords, but I am pretty sure I am not able to update the origin coords? :

    public void Update(float pX, float pY, float pZ) {
    posX = pX;
    posY = pY;
    posZ = pZ;

    //update the playerCube transform for the rigid body
    cubeTransform.origin.x = posX;
    cubeTransform.origin.y = posY;
    cubeTransform.origin.z = posZ;

    cubeRigidBody.getMotionState().setWorldTransform(cubeTransform);

    processTransformMatrix(cubeTransform);
    }

I do not have rotation updated, as I do not actually want/need the player body to rotate at all currently. However, in the final game this will be put in place.

\$\endgroup\$
3
\$\begingroup\$

You need to make the physics object a kinematic object. See Bullet Physics Manual for more information. Bullet Physics ask every frame the current transformation from kinematic objects. You need your own MotionState implementation for that. Mine is as simple as this:

public class KinematicMotionState extends MotionState {
    private Transform worldTransform;

    public KinematicMotionState() {
        worldTransform = new Transform();
        worldTransform.setIdentity();
    }

    @Override
    public Transform getWorldTransform(Transform out) {
        out.set(worldTransform);
        return out;
    }

    @Override
    public void setWorldTransform(Transform worldTrans) {
        worldTransform.set(worldTrans);
    }
}

In addition you must tell the rigid body that it's a kinematic object:

rigidBody.setCollisionFlags(rigidBody.getCollisionFlags() | CollisionFlags.KINEMATIC_OBJECT);
rigidBody.setActivationState(CollisionObject.DISABLE_DEACTIVATION);

And then updating the transformation of the kinematic object:

Transform transform = new Transform();
transform.setIdentity();
transform.origin.set(x, y, z);
motionState.setWorldTransform(transform);
\$\endgroup\$
2
  • \$\begingroup\$ That is so simple! I had looked through the Bullet Physics manual a bit before posting, but I guess I just didn't know what I was looking for! This was super helpful thank you again msell! \$\endgroup\$ – user22020 Nov 1 '12 at 16:29
  • \$\begingroup\$ Isn't this just setting the position? What happens when something collides? \$\endgroup\$ – joehot200 Apr 19 '15 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy