View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# RigidBody Rotation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Medo3337  Members

Posted 29 November 2012 - 05:40 AM

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 *****
}
}

### #2DgekGD  Members

Posted 29 November 2012 - 08:16 AM

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, 29 November 2012 - 08:18 AM.

### #3BCullis  Members

Posted 29 November 2012 - 10:52 AM

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #4Medo3337  Members

Posted 29 November 2012 - 12:03 PM

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

### #5DgekGD  Members

Posted 29 November 2012 - 02:42 PM

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!

### #6Medo3337  Members

Posted 29 November 2012 - 02:48 PM

@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...


### #7DgekGD  Members

Posted 29 November 2012 - 03:02 PM

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);


### #8BCullis  Members

Posted 29 November 2012 - 03:03 PM

@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, 29 November 2012 - 03:04 PM.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #9Medo3337  Members

Posted 29 November 2012 - 03:21 PM

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, 29 November 2012 - 03:22 PM.

### #10DgekGD  Members

Posted 29 November 2012 - 03:32 PM

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, 29 November 2012 - 03:34 PM.

### #11BCullis  Members

Posted 29 November 2012 - 04:11 PM

A little digging in the bullet docs revealed:
btMatrix3x3::setEulerYPR ( const btScalar & yaw,
const btScalar & pitch,
const btScalar & roll
)

So I'm assuming you could use that to create a btMatrix3x3, and then apply that to a btTransform's origin that can be applied to the btRigidBody.

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

No one is here to just write your code for you. You're explicitly asking people to do that. Try and have a little courtesy, it's a forum for information sharing, not task orders.

Edited by BCullis, 29 November 2012 - 04:13 PM.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

### #12Olof Hedman  Members

Posted 29 November 2012 - 04:29 PM

You got pretty much exactly what you wanted in the first post.
Here its modified with setting yaw, pitch and roll:

btMatrix3x3 orn = m_pRigidBody->getWorldTransform().getBasis(); //get basis of world transformation
btMatrix ypr;
ypr.setEulerYPR(0.2f,0.1f,0.1f);
orn *= ypr //Multiply it by rotation matrix, yaw, pitch, roll
m_pRigidBody->getWorldTransform().setBasis(orn); //set new rotation for the object

Edit:

No one is here to just write your code for you

Oops

Edited by Olof Hedman, 29 November 2012 - 04:31 PM.

### #13Medo3337  Members

Posted 30 November 2012 - 12:16 AM

@Olof Hedman: That was exactly what I was looking for! Thank you!

@BCullis:

No one is here to just write your code for you.

Trust me, writing 5 lines of code is easier to understand and will save a lot of time ;)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.