• Advertisement
Sign in to follow this  

RigidBody Rotation

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.
For more information about quaternions I would recommend visiting this link.

I hope it helps;) Edited by DgekGD

Share this post


Link to post
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 this post


Link to post
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);


Finally, apply your transform

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
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.
[/quote]

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

Share this post


Link to post
Share on other sites
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
[/quote]

Oops :) Edited by Olof Hedman

Share this post


Link to post
Share on other sites
@Olof Hedman: That was exactly what I was looking for! Thank you!

@BCullis:
No one is here to just write your code for you.[/quote]
Trust me, writing 5 lines of code is easier to understand and will save a lot of time ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement