Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualMedo3337

Posted 24 October 2012 - 09:30 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.

#1Medo3337

Posted 24 October 2012 - 09:29 AM

I have 2 boxes and trying to make them act like physical boxes, instead of intersecting, I 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.

PARTNERS