1

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?

Peng Yang
  • 201
  • 1
  • 4
  • 11

2 Answers2

1

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.

IAmNoone
  • 1,011
  • 1
  • 8
  • 25
  • Although I haven't tried your method but I have a question that it seems your "myquat"is just from the quaternion of the object, have you changed the value of "myquat" before you apply it to your object? – Peng Yang Dec 11 '14 at 17:14
0

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.

new Objekt
  • 414
  • 3
  • 8
  • Thanks for your reply.I have tried to find a function named setOrientation but I have not found it at least in rigidbody class.Instead, I have found an alternative way to solve the problem.Thank you all the same. – Peng Yang Dec 11 '14 at 17:20
  • Go ahead and add your solution as an anwser to the question for anyone else who happens upoun this while searching then. Sorry I wasn't of much help and good luck with your project. – new Objekt Dec 11 '14 at 18:03