Peng Yang
Peng Yang

Reputation: 231

How to rotate an object in Bullet Physics

Here I have a problem with object rotation in Bullet. What I want to implement is to rotate an object around global x,y,z axis at the same time. (here global means the axis x,y,z will not be changed during rotation) I have the code below

btQuaternion m_lastRot;
btTransform tranf =  _obj[idx]->mp_btRidObj->getCenterOfMassTransform();
tranf.getBasis().getRotation(m_lastRot);
btQuaternion qx(btVector3(1,0,0),angX);
btQuaternion qy(btVector3(0,1,0),angY);
btQuaternion qz(btVector3(0,0,1),angZ);
tranf.setRotation(qz * qy * qx * m_lastRot);
_obj[idx]->mp_btRidObj->setCenterOfMassTransform(tranf);

But it does not work as I expected. By the way, the code below which rotateing a object around one of x,y,z axis each time works well.

btQuaternion m_lastRot;
btTransform tranf =  _obj[idx]->mp_btRidObj->getCenterOfMassTransform();
tranf.getBasis().getRotation(_obj[idx]->m_lastRot);
btQuaternion qx(btVector3(1,0,0),angX);
btQuaternion qy(btVector3(0,1,0),angY);
btQuaternion qz(btVector3(0,0,1),angZ);
if(x)
tranf.setRotation(qx * m_lastRot);
else if(y)
tranf.setRotation(qy * m_lastRot);
else if(z)
tranf.setRotation(qz * m_lastRot);

_obj[idx]->mp_btRidObj->setCenterOfMassTransform(tranf);

Is there anyone can tell me how to solve this problem?

Upvotes: 1

Views: 2274

Answers (2)

IAmNoone
IAmNoone

Reputation: 1011

I do it like this:

//this is my bullet object currently reading data from:
bulletobject->getMotionState()->getWorldTransform(trans);
btQuaternion rot = trans.getRotation();
myquat.w = rot.w();
myquat.x = rot.x();
myquat.y = rot.z();
myquat.z = rot.y();
//I then apply the quat to my object that I want to move in my graphics application.

you have to remember to get the 'w' also if you do it this way, if not the rotations will be wrong.

Upvotes: 1

new Objekt
new Objekt

Reputation: 414

In Jbullet I belive there is a method called setOrientation(Quat4f r) implemented in Bullet RigidBody that does what you want. I assume it's also in the standard Bullet lib.

Upvotes: 0

Related Questions