View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Stopping 2 Rigid Bodies from Intersecting

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Medo3337  Members

Posted 24 October 2012 - 09:29 AM

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:

///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
}

{
//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);
}
{
//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);
}

I am using Bullet Physics.

Edited by Medo3337, 24 October 2012 - 09:30 AM.

### #2HappyCoder  Members

Posted 24 October 2012 - 09:14 PM

Are the objects colliding but with some overlap or are they not colliding at all?
My current game project Platform RPG

### #3MaxDZ8  Members

Posted 26 October 2012 - 01:03 AM

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 calculateLocalInertia explicitly anymore.

Previously "Krohm"

### #4Medo3337  Members

Posted 26 October 2012 - 08:11 AM

The boxes are intersecting like there is no physics, but the gravity is working.

### #5erwincoumans  Members

Posted 26 October 2012 - 11:06 AM

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.