I have written some code which creates some cubes where one has a non-zero initial velocity which puts on a collision course with another of the objects. They collide and respond successfully. However, they do not have any rotation after the collision, which doesn't seem right. The normal at the point of collision is not in the direction of the center of mass -- which, if I understand collision physics right, is a necessary condition for having no rotation resulting from the collision.
Am I forgetting to do something / doing something wrong?
My full code is here:
https://bitbucket.org/master_latch/bull ... ?at=master
It's no the prettiest code but right now I'm just trying to get things to work.
Why no rotation after collision?
-
- Posts: 3
- Joined: Sun Dec 07, 2014 11:41 pm
Re: Why no rotation after collision?
I finally figured it out. I spent practically an entire day just trying to get an answer to this one question.
Two things are necessary:
Either step on its own accomplishes nothing. I had seen code online where people did step 1, but nowhere did I notice step 2. It felt so good to finally see the boxes rotate after spending so long trying to figure this out.
Two things are necessary:
- You need to invoke calculateLocalInertia on the shape object. e.g.:
Code: Select all
myShape.calculateLocalInertia(mass, localInertia);
- You need to pass this localInertia vector into the constructor for the btRigidBodyConstructionInfo object. e.g.:
Code: Select all
btRigidBody::btRigidBodyConstructionInfo info0 ( mass, &myMotionState, &myShape, localInertia);
Either step on its own accomplishes nothing. I had seen code online where people did step 1, but nowhere did I notice step 2. It felt so good to finally see the boxes rotate after spending so long trying to figure this out.
-
- Posts: 3
- Joined: Sun Dec 07, 2014 11:41 pm
Re: Why no rotation after collision?
Actually, after reading the first sentence on the Wiki page for moment of inertia I think I know what this thing is:
I looked into the function and the first line is:
Then the subroutine invokes itself recursively and adds up all the inertia's of each part. Talk about confusing... it seems like it should have just returned a vector!!
The components of the vector probably represent the inertia for each axis of rotation. But then if that's the case, it seems like calculateLocalInertia should *output* a vector, not receive one as input...Moment of inertia is the mass property of a rigid body that determines the torque needed for a desired angular acceleration about an axis of rotation. Moment of inertia depends on the shape of the body and may be different around different axes of rotation.
I looked into the function and the first line is:
Code: Select all
inertia.setValue(0.f,0.f,0.f);
-
- Posts: 149
- Joined: Fri May 24, 2013 6:08 am
Re: Why no rotation after collision?
There's a wealth of implementation examples that ship with the library. There are also examples in the wiki. It all shows the cut and dried process of adding a body to the world. I don't know where else you are getting examples, but the user manual and demos are really the place to start.master_latch wrote:Either step on its own accomplishes nothing. I had seen code online where people did step 1, but nowhere did I notice step 2. It felt so good to finally see the boxes rotate after spending so long trying to figure this out.
It does. It calculates the inertia based on your mass and writes it into the btVector3 you pass by reference. You then use this to create your body.master_latch wrote:it seems like calculateLocalInertia should *output* a vector, not receive one as input...
There is no "the function" for calculating inertia. All collision shapes ultimately inherit from btCollisionShape where calculateLocalInertia is a pure virtual function. Meaning every shape has to implement its own calculateLocalInertia.master_latch wrote:I looked into the function and the first line is:
Code:
inertia.setValue(0.f,0.f,0.f);
Then the subroutine invokes itself recursively and adds up all the inertia's of each part. Talk about confusing... it seems like it should have just returned a vector!!
-
- Posts: 5
- Joined: Fri Aug 14, 2015 11:50 am
Re: Why no rotation after collision?
Thank you, master_latch.
Your solution to the problem worked for me after one year. I appreciate this.
Your solution to the problem worked for me after one year. I appreciate this.