[SOLVED] Rigid Body Rotation

Post Reply
CireNeikual
Posts: 19
Joined: Thu Jun 28, 2012 5:06 pm

[SOLVED] Rigid Body Rotation

Post by CireNeikual »

Super N00b question: How can I get rigid bodies to rotate? They move around fine, and tumble around, but never rotate! When I print the rotation quaternion, it is always just the initial rotation I gave the body.

Thank you for your time!
Last edited by CireNeikual on Fri Jun 29, 2012 1:28 pm, edited 1 time in total.
jdowner
Posts: 7
Joined: Fri Jan 06, 2012 1:04 pm

Re: Rigid Body Rotation

Post by jdowner »

CireNeikual wrote:Super N00b question: How can I get rigid bodies to rotate? They move around fine, and tumble around, but never rotate! When I print the rotation quaternion, it is always just the initial rotation I gave the body.

Thank you for your time!
There are several ways to get a btRigidBody to spin. Call any of applyTorque, applyAngularImpulse, or setAngularVelocity and you can make the a rigidbody rotate, but make sure that you have initialised the inertia of the rigidbody first.

-Josh
CireNeikual
Posts: 19
Joined: Thu Jun 28, 2012 5:06 pm

Re: Rigid Body Rotation

Post by CireNeikual »

Thanks for the response, but I meant having it rotate when it collides with something, like when a box is tumbling down a slope. It should happen normally without me doing anything, so it must be something with how I retrieve the angles. Right now, if a box falls on the ground at an angle, it will just stay at that angle, no matter what. I set up the body like this:

Code: Select all

m_pCollisionShape = new btBoxShape(bt(m_pModel->GetAABB().GetHalfDims()));

	m_pMotionState = new btDefaultMotionState(btTransform(btQuaternion(0.2f, 0.6f, 0.1f, 1.0f).normalized(), bt(m_aabb.GetCenter())));

	const float mass = 1.0f;

	m_pCollisionShape->calculateLocalInertia(mass, btVector3(0.0f, 0.0f, 0.0f));

	btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, m_pMotionState, m_pCollisionShape, btVector3(0.0f, 0.0f, 0.0f));

	m_pRigidBody = new btRigidBody(rigidBodyCI);

	m_pPhysicsWorld->m_pDynamicsWorld->addRigidBody(m_pRigidBody);

	m_pRigidBody->setAngularVelocity(btVector3(0.2f, 0.5f, 0.2f));
And here is how I retrieve the angles:

Code: Select all

btTransform transform;
	m_pMotionState->getWorldTransform(transform);
	
	// Update box model
	m_pModel->SetPosition(cons(transform.getOrigin()));

	m_pModel->SetRotation(cons(transform.getRotation()));
When I print transform.getRotation(), the values are always just what I initialized it to, it never changes over time when the boxes tumble around. transform.getOrigin() updates just fine.

EDIT: Actually, it seems that it is not how I am retrieving the rotations, but the simulation just doesn't have any! All rigid bodies have infinite angular momentum or something, since I can do ridiculous stuff like this:

Image
marios
Posts: 52
Joined: Mon Jul 19, 2010 3:11 am
Contact:

Re: Rigid Body Rotation

Post by marios »

Code: Select all

 
m_pCollisionShape->calculateLocalInertia(mass, btVector3(0.0f, 0.0f, 0.0f));

btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, m_pMotionState, m_pCollisionShape, btVector3(0.0f, 0.0f, 0.0f));
in this lines you got a bug, basicly you are passing zero vector as inertia and it should be vector calculated in first line as reference. Try that:

Code: Select all

btVector3 inertia;
m_pCollisionShape->calculateLocalInertia(mass, intertia);

btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(mass, m_pMotionState, m_pCollisionShape,inertia);
CireNeikual
Posts: 19
Joined: Thu Jun 28, 2012 5:06 pm

Re: Rigid Body Rotation

Post by CireNeikual »

Aha, I see how it works now :oops: ! Thanks for the help!
Post Reply