Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
4 replies to this topic

#1 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

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
  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.

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


Sponsor:

#2 HappyCoder   Members   -  Reputation: 2877

Like
0Likes
Like

Posted 24 October 2012 - 09:14 PM

Are the objects colliding but with some overlap or are they not colliding at all?

#3 Krohm   Crossbones+   -  Reputation: 3245

Like
0Likes
Like

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.

#4 Medo3337   Members   -  Reputation: 680

Like
0Likes
Like

Posted 26 October 2012 - 08:11 AM

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

#5 erwincoumans   Members   -  Reputation: 272

Like
0Likes
Like

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.



PARTNERS