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.
Answer
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);
No comments:
Post a Comment