• Advertisement
Sign in to follow this  

Stopping 2 Rigid Bodies from Intersecting

This topic is 1912 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

I have 2 boxes and trying to make them act like physical boxes, instead of intersecting, I want them to push each others, I am using the following code but it doesn't stop them from intersecting:

[CODE]///create a few basic rigid bodies
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), btScalar(1));
//keep track of the shapes, we release memory at exit.
//make sure to re-use collision shapes among rigid bodies whenever possible!
btAlignedObjectArray<btCollisionShape*> collisionShapes;
collisionShapes.push_back(groundShape);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(model[0]->X,model[0]->Y,model[0]->Z));
{
btScalar mass(0.);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
groundShape->calculateLocalInertia(mass,localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//add the body to the dynamics world
dynamicsWorld->addRigidBody(body);
}

{
//create a dynamic rigidbody
//btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
btBoxShape* colShape = new btBoxShape(btVector3(1, 1, 1));
collisionShapes.push_back(colShape);
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
colShape->calculateLocalInertia(mass,localInertia);
startTransform.setOrigin(btVector3(model[1]->X,model[1]->Y,model[1]->Z));

//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
dynamicsWorld->addRigidBody(body);
}
{
//create a dynamic rigidbody
//btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
btBoxShape* colShape = new btBoxShape(btVector3(1, 1, 1));
collisionShapes.push_back(colShape);
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
colShape->calculateLocalInertia(mass,localInertia);
startTransform.setOrigin(btVector3(model[2]->X,model[2]->Y,model[2]->Z));

//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//body->activate(true);
dynamicsWorld->addRigidBody(body);
}[/CODE]

I am using Bullet Physics. Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement
That's odd. Even if they encroach each other, they should de-encroach automatically as soon as you tick the world.
Note the example code in bullet demos is overly verbose. You don't need to call [font=courier new,courier,monospace]calculateLocalInertia[/font] explicitly anymore.

Share this post


Link to post
Share on other sites
Start with an unmodified Bullet demo, such as Bullet/Demos/BasicDemo and make sure the collisions work properly there.
Then compare the setup with your own, so find the problem.

Thanks,
Erwin

Share this post


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

  • Advertisement