probelms with rotation

llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

probelms with rotation

Post by llefler »

Hello, I am new to bullet Physics SDK, and I am building a project that includes having multiple cubes bounce and rotate off one another. My problem is that when my cubes collide their angular momentum/rotation does not change at all. If I initially give them an angular velocity using setAngularVelocity() they rotate, but still do not change when they collide with another cube. The cubes bounce off each other but just maintain the same rotation/velocity as before the collision. Any suggestions or help would be greatly appreciated. I am sure there is just something small I am missing that causes this problem. Let me know if I should post any of my code, or should include any other details. Thanks!!
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: probelms with rotation

Post by Dr.Shepherd »

I suppose you use the setAngularVelocity in the motorTickCallback, or any other similar callbacks. In this sense, even if your rotation changes after the collision, it will return straight back the the velocity with the callback.

you need to give an torque impulse to it when you initialize the rotation, only once, then make them collide.

Hope this solves your problem.
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

Unfortunately, applying a torque impulse did not fix the problem :(
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

I am calling dynamicsWorld->stepSimulation(1/60.f,10) and then getting the world transform and using that information to rotate/translate, is this the correct order?
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

Thanks for the reply btw Dr.Shepherd. Here is some of my code, maybe this will help see whats going on.

//init
fallRigidBodyCI.m_friction = 0.5f;
fallRigidBodyCI.m_restitution = restitutionDice;
fallRigidBodyCI.m_linearDamping = linDamp;
fallRigidBodyCI.m_angularDamping = angDamp;
fallRigidBodyCI.m_mass = mass;
btVector3 localInertia(0,0,0);
fallRigidBodyCI.m_collisionShape = fallShape;
fallShape->setMargin(1.0f);
fallRigidBody = new btRigidBody(fallRigidBodyCI);
fallRigidBody[i]->applyTorqueImpulse(btVector3(10.0f,10.0f,0)); //Just to try and get it to rotate
dynamicsWorld->addRigidBody(fallRigidBody[i]);

//update and get values
dynamicsWorld->stepSimulation(1/60.f,10);
fallRigidBody[i]->activate(true);
fallRigidBody[i]->getMotionState()->getWorldTransform(trans[i]);
newTrans[i][0] = trans[i].getOrigin().getX()/50;
newTrans[i][1] = trans[i].getOrigin().getY()/50;
newTrans[i][2] = trans[i].getOrigin().getZ()/50;
btVector3 euler;
[self quatToEuler:trans[i].getRotation():euler];
rotA[i][0] = euler.getX();
rotA[i][1] = euler.getY();
rotA[i][2] = euler.getZ();

///Draw cube
glPushMatrix();
glTranslatef(newTrans[i][0], newTrans[i][1], newTrans[i][2]);
glRotatef(rotA[i][0]*(180/PI),1.0f,0.0f,0.0f);
glRotatef(rotA[i][1]*(180/PI),0.0f,1.0f,0.0f);
glRotatef(rotA[i][2]*(180/PI),0.0f,0.0f,1.0f);
[self drawCube:0.1f:0.1f:0.1f];
glPopMatrix();


it won't rotate at all, not when the cubes collide, not when i apply the impulse torque manually.
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: probelms with rotation

Post by Dr.Shepherd »

Is the code above all in the Initial function, or it will run in a periodic way?

did you set up your dynamics world correctly? compare it with the BasicDemo.cpp in Demo/BasicDemo. Maybe you didn't set up correctly so they won't collide.
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

the first "init" section is in the init function, update and draw are called every game loop. And they are colliding, they bounce off one another, they just never are gaining and angular momentum/velocity, like if the two cubes collide just barley, they just bounce off one another without rotating when they should be.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: probelms with rotation

Post by dphil »

This may be a silly question, but... your "angDamp" variable is less than 1, right? Set it to 0, in case it's not already.

Also, I'm not sure you're supposed to be talking directly to the motion state to get that kind of information. Looking at the bullet api, it looks like "getWorldTransform" actually feeds a transform into the physics, rather than simply returning a stored value (ie it's meant to be a getter for the physics side of things, not the user side, I think).

Try:

Code: Select all

trans[i] = fallRigidBody[i]->getCenterOfMassTransform();
instead.

You can also manually check if the rotation of the transform is actually staying 0 or if it is changing values, which could indicate that the physics is working but your graphics aren't properly reflecting that.
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

My angular and linear damping were both 0.1, I changed them both to 0 and that did not fix it. I also changed the transform to:

trans = fallRigidBody->getCenterOfMassTransform();

and that did not fix it unfortunately.

I am printing out the rotations after I get them using

trans.getRotation();

and I convert this to euler coordinates and the values are continuously 0. If I set an angular velocity, the rotation values update correctly, but even when they collide with another object, they bounce off and they just keep the exact same rotation velocity, I am so confused :(
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

I fixed it.

The problem was the order in which i was calculating the local inertia and then passing that information to the rigid body construction info. I will post my new code in the next day or so in case anyone else has this issue.

Thank you so much for your help!!
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: probelms with rotation

Post by Dr.Shepherd »

Yeah, post your code and highlight the place where the error comes from. It will help us learn a lot !

Cheers!
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

Here is the part of the code that now works. Originally I was not calculating the local inertia for every shape, I believe this is the problem that many people run into when their objects will move/fall but will not rotate. Now I am calculating the local inertia for each shape, updating the RigidBodyCI and it works.

//init function
localInertia = btVector3(0,0,0);
fallShape->calculateLocalInertia(mass, localInertia);
fallRigidBodyCI = btRigidBody::btRigidBodyConstructionInfo(mass,fallMotionState,fallShape,localInertia);
fallRigidBodyCI.m_friction = 0.5f;
fallRigidBodyCI.m_restitution = restitutionDice;
fallRigidBodyCI.m_linearDamping = linDamp;
fallRigidBodyCI[i].m_angularDamping = angDamp;
fallRigidBodyCI[i].m_mass = mass;
fallRigidBodyCI[i].m_collisionShape = fallShape[i];
fallShape[i]->setMargin(0.1f);
fallRigidBody[i] = new btRigidBody(fallRigidBodyCI[i]);
dynamicsWorld->addRigidBody(fallRigidBody[i]);


Please let me know if anyone has any other questions! Thanks again for all the help.
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: probelms with rotation

Post by Dr.Shepherd »

Could anyone explain this LocalInertial for me? Why it seems to be set (0, 0, 0) everywhere?

Thanks
User avatar
Dr.Shepherd
Posts: 168
Joined: Tue Jan 04, 2011 11:47 pm

Re: probelms with rotation

Post by Dr.Shepherd »

Code: Select all

        localInertia[i] = btVector3(0,0,0);
        fallShape[i]->calculateLocalInertia(mass, localInertia[i]);
After I read the code, I think the first line only serves as the initialization of the variable "localInertial", and the variable is actually calculated in the next function. Sorry for this silly question.

Besides, I found this interesting thing, :)

Code: Select all

void    btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
00049 {
00050         //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-)
00051         btVector3 halfExtents = getHalfExtentsWithMargin();
00052 
00053         btScalar lx=btScalar(2.)*(halfExtents.x());
00054         btScalar ly=btScalar(2.)*(halfExtents.y());
00055         btScalar lz=btScalar(2.)*(halfExtents.z());
00056 
00057         inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz),
00058                                         mass/(btScalar(12.0)) * (lx*lx + lz*lz),
00059                                         mass/(btScalar(12.0)) * (lx*lx + ly*ly));
00060 
00061 }
llefler
Posts: 17
Joined: Sun Sep 25, 2011 8:13 pm

Re: probelms with rotation

Post by llefler »

Yeah, as you discovered its just for init purposes, and then calculated later. I am sure there is another way I could do all of this, I decided to do it that way though.