# RigidBody Rotation

This topic is 2210 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Lets say that I have a tank and want this tank to rotate and look at another area, how do I complete the following code to make the tank rotate 'slightly' every time this method is called?

void RotateLeft(float timeElapsed)
{
btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[0];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
btTransform worldTrans;
worldTrans = body->getCenterOfMassTransform();

// ***** Need the rest of the code to rotate the tank *****
}
}

##### Share on other sites
I'm using bullet physics for my game and this is how I rotate character in the game.

 btMatrix3x3 orn = m_pRigidBody->getWorldTransform().getBasis(); //get basis of world transformation orn *= btMatrix3x3(btQuaternion(axis, radians)); //Multiply it by rotation matrix m_pRigidBody->getWorldTransform().setBasis(orn); //set new rotation for the object 

m_pRigidBody is an object of btRigidBody*. To represent rotation all you need is 3x3 matrix (4th row and column are to enable translation). So you get basis of world transform and then multiply it by rotation matrix( created with the help of quaternion, where the axis is axis of rotation and radians is angle in radians). After that all you have to do is to update basis of your world transform.

I hope it helps;) Edited by DgekGD

##### Share on other sites
Just to add, if your trouble is with making it rotate at a fixed (slow?) rate based on elapsed time, you'll just multiply your desired rad/sec turning rate by the elapsed time in sec.

##### Share on other sites
If I want to get Pitch, Yaw, Roll and manipulate it then give it back to the physics engine, how can I do that?

##### Share on other sites
For example, if you use xna math library, you can create XMMATRIX using XMMatrixRotationRollPitchYaw method.

Get current physics object transform:
 XMMATRIX transform = btTransform_to_XMMATRIX(m_pRigidBody->getWorldTransform() ); //get basis of world transformation 

Create rotation you need:
 XMMATRIX rotation = XMMatrixRotationRollPitchYaw(angle, angle, angle); 

//Get final transform
 btTransform finalTransform = XMMATRIX_to_btTansform(transform * rotation); 

 m_pRigidBody->setWorldTransform(finalTransform); 

Here, take a look at conversion methods:
 static btTransform XMMATRIX_to_btTransform( XMMATRIX const & mat ) { // convert from XMMATRIX to btTransform (Bullet Physics) btMatrix3x3 bulletRotation; btVector3 bulletPosition; // copy rotation matrix for ( int row=0; row<3; ++row ) for ( int column=0; column<3; ++column ) bulletRotation[row][column] = mat.m[column][row]; // copy position for ( int column=0; column<3; ++column ) bulletPosition[column] = mat.m[3][column]; return btTransform( bulletRotation, bulletPosition ); } static XMMATRIX btTransform_to_XMMATRIX( XMMATRIX const & trans ) { XMMATRIX returnValue; // convert from btTransform (BulletPhysics) to XMMATRIX btMatrix3x3 const & bulletRotation = trans.getBasis(); btVector3 const & bulletPosition = trans.getOrigin(); // copy rotation matrix for ( int row=0; row<3; ++row ) for ( int column=0; column<3; ++column ) returnValue.m[row][column] = bulletRotation[column][row]; // copy position for ( int column=0; column<3; ++column ) returnValue.m[3][column] = bulletPosition[column]; return returnValue; } 

Take a note, that XMMATRIX is in row-major format and btTransform is column-major, that's why you swap column/row in these methods!

##### Share on other sites
@DgekGD: I'm not using XNA, I'm using DirectX 9 and Bullet Physics.

Here is what I'm looking for:
 float pitch, yaw, roll; // Code to get pitch, yaw, roll from rigidbody here... yaw+=0.1; // Code to apply the new pitch, yaw, roll to the rigidbody here... 

##### Share on other sites
I didn't mean xna framework, just math defined in xnamath. It was just an example, you can use D3DXMATRIX exactly the same way.

Anyway, to apply new pitch, yaw and roll to your body you have to create transformation matrix!
Using:
XMMatrixRotationRollPitchYaw(Pitch, Yaw, Roll); or
D3DXMatrixRotationYawPitchRoll(pResultMatrix, Yaw, Pitch, Roll) or maybe your own math library class.

Then you'll have to convert this class to btTransform(matrix class which Bullet Physics uses for computation);

That's how you would increment yaw:
 yaw+= 0.1; XMMATRIX rotation = XMMatrixRotationRollPitchYaw(pitch, yaw, roll); 

##### Share on other sites

@DgekGD: I'm not using XNA, I'm using DirectX 9 and Bullet Physics.

xnamath is a library for directx, actually, not referring to the XNA framework for .NET.

Anyway, you can use D3DXMatrixRotationYawPitchRoll:
http://msdn.microsof...1(v=vs.85).aspx

Edit: DAH, too slow. Edited by BCullis

##### Share on other sites
I know all of that, the problem I'm having is that I need to get pitch, yaw, roll from bullet physics and set the updated value to bullet physics.

So here is EXACTLY what I mean:
// Add code to get pitch, yaw, roll from btRigidBody
pitch+=0.1f;
yaw+=0.2f;
roll+=0.1f;
// Add code to apply the new pitch, yaw, roll to btRigidBody

If someone could update the two commented lines with the appropriate code, it will be exactly what I want. Edited by Medo3337

##### Share on other sites
What's the problem with creating rotation matrix D3DXMatrixRotationYawPitchRoll(pResult, 0.2, 0.1, 0.1);

Multiplying current transformation matrix with this one will exactly do what you want: Apply
pitch+=0.1f;
yaw+=0.2f;
roll+=0.1f;
to btRigidBody. Edited by DgekGD

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
A4L
14
5. 5

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013737
×