Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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);
share|improve this answer
    
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! –  Kenneth Bray Nov 1 '12 at 16:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.