Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

Stopping 2 Rigid Bodies from Intersecting

Started by Medo Mex Oct 24, 2012 at 3:29 PM 3 replies 1.6k views
Original Post
Medo Mex
Medo Mex
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
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);
}


I am using Bullet Physics.
HappyCoder
HappyCoder
Are the objects colliding but with some overlap or are they not colliding at all?
My current game project Platform RPG
Krohm
Krohm
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.
Previously "Krohm"
Medo Mex
Medo Mex
The boxes are intersecting like there is no physics, but the gravity is working.
erwincoumans
erwincoumans
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.