Rotating boxes

bizack
Posts: 5
Joined: Thu Nov 06, 2008 10:25 pm

Rotating boxes

Post by bizack »

I'm trying to put together a simple demo, where a few boxes fall from the sky, stacking on to one another. I'd like the boxes to rotate after they've 'settled.' My first attempt is to simply get a box to rotate (I want a fixed rotation... so 0-90 degrees for example, over a fixed period of time).

I'm not sure how to approach this... my naive approach is to get the objects rotation, update it, and set it. But I'd rather apply torque, I think?

Here's where the boxes are created:

Code: Select all

btCollisionShape* boxShape = new btBoxShape(btVector3(1, 1, 1));
btTransform boxTransform;
btScalar boxMass(1.0);
btVector3 boxInertia(0, 0, 0);
			
for (int i = 0; i < 4; ++i) {
	boxTransform.setIdentity();
	boxTransform.setOrigin(btVector3(0, 12*i, 0));

	btDefaultMotionState* boxMotionState = new btDefaultMotionState(boxTransform);
	btRigidBody::btRigidBodyConstructionInfo boxInfo(boxMass, boxMotionState, boxShape, boxInertia);
	btRigidBody* box = new btRigidBody(boxInfo);
	m_dynamicsWorld->addRigidBody(box);
}
and in my overridden renderme method, I have:

Code: Select all

// ground is 0, first box is 1
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[1];
btRigidBody* body = btRigidBody::upcast(obj);
body->applyTorque(btVector3(0, 1, 0));
body->activate(true);
This isn't working, and I'm not sure why. I'm basically not seeing how animation is done using Bullet.

Thanks.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Rotating boxes

Post by Erwin Coumans »

You need to calculate the boxInertia for dynamic objects (mass > 0). It seems a common mistake. Where did you get the information of setting the inertia to zero?

Code: Select all

boxShape->calculateLocalInertia(mass,boxInertia);
An easier way is to take the startTransform and the desired transform, and directly set the body linear and angular velocities:

Code: Select all

btVector3 linVel;
btVector3 angVel;
btTransformUtil::calculateVelocity(startTransform,desiredTransform,timeStep,linVel,angVel);
Hope this helps,
Erwin
bizack
Posts: 5
Joined: Thu Nov 06, 2008 10:25 pm

Re: Rotating boxes

Post by bizack »

What you said makes sense (my inertia was 0 because I wanted a quick and dirty way to stop the boxes from bouncing off of one another... I'd like them to stack on one another and not fall). So.... I've reduced this to the simple case of one box, and made the changes as you suggested:

Code: Select all

btCollisionShape* boxShape = new btBoxShape(btVector3(1, 1, 1));
btTransform boxTransform;
btScalar boxMass(1.0);
btVector3 boxInertia;
boxShape->calculateLocalInertia(boxMass, boxInertia);
				
btTransform begTransform;
begTransform.setIdentity();
begTransform.setOrigin(btVector3(0, 0, 0));
btTransform endTransform;
endTransform.setIdentity();
endTransform.setOrigin(btVector3(0, 0, 0));
endTransform.setRotation(btQuaternion(btVector3(0, 1, 0), btScalar(90)));

btVector3 linVel;
btVector3 angVel;

btTransformUtil::calculateVelocity(begTransform, endTransform, btScalar(10), linVel, angVel);

boxTransform.setIdentity();
boxTransform.setOrigin(btVector3(0, 0, 0));

btDefaultMotionState* boxMotionState = new btDefaultMotionState(boxTransform);
btRigidBody::btRigidBodyConstructionInfo boxInfo(boxMass, boxMotionState, boxShape, boxInertia);
btRigidBody* box = new btRigidBody(boxInfo);
box->setLinearVelocity(linVel);
box->setAngularVelocity(angVel);

m_dynamicsWorld->addRigidBody(box);
I still have an inanimate box. Thanks for your help.
bizack
Posts: 5
Joined: Thu Nov 06, 2008 10:25 pm

Re: Rotating boxes

Post by bizack »

So I'm a little confused. Everything seems to be set up, and I lowered the friction of the box to a very low value (this was stopping the box from rotating... how does one calculate the correct friction values of sliding surfaces?). Anyway, in my renderme method, I apply a torque impulse, which works, but keeps applying the impulse. So... my goal is to apply just enough torque to rotate the box 90 degrees in x number of seconds. I've set the body's linear and angular velocity, but this doesn't seem to change anything.

Thanks.
bizack
Posts: 5
Joined: Thu Nov 06, 2008 10:25 pm

Re: Rotating boxes

Post by bizack »

Okay, I found an alternative (and most likely correct) solution using constraints. Would be great if there was more (or any) documentation on constraints...

Thanks.