Search code examples
c++bulletphysics

Bullet | Rigidbody translated correctly but printed position is wrong


I have a very simple problem, but I can't see what I'm doing wrong.

I have a rigidbody starting at pos: 0.0, 3.0, 0.0. I apply a translate, -90 degree rotation, and then another translate. The rigidbody's final position should be 2.0, 1.0, 0.0, but the position that is printed out is still 0.0, 3.0, 0.0.

I perform a collision test by dropping some small cubes above the rigidbody in question. Oddly enough, they stop above 2.0, 1.0, 0.0 showing that the rigidbody was moved correctly.

//Rigidbody in question
btRigidBody *btPhys;

//First transform
btPhys->translate(btVector3(0.0, -2.0, 0.0));

//Perform -90 degree rotation
btMatrix3x3 orn = btPhys->getWorldTransform().getBasis();
orn *= btMatrix3x3(btQuaternion( btVector3(0, 0, 1), btScalar(degreesToRads(-90))));
btPhys->getWorldTransform().setBasis(orn);

//Perform second transform
btPhys->translate(btVector3(2.0, 0.0, 0.0));

//Print out final position
btTransform trans;
btPhys->getMotionState()->getWorldTransform(trans);
float x, y, z;
x = trans.getOrigin().getX();
y = trans.getOrigin().getY();
z = trans.getOrigin().getZ();

printf("\n\nposition: %f %f %f\n\n", x, y, z);

Basically, I'd just like to be able to get the correct position of the rigidbody from this code (2.0, 1.0, 0.0). Thank you!


Solution

  • In your case, if you want to obtain correct position of btRigidBody you should call:

    btPhys->getWorldTransform().getOrigin();
    

    You are calling

    btPhys->getMotionState()->getWorldTransform(trans);
    

    instead, but the MotionState is not yet updated. All MotionStates are updated in simulation step.