
I am working on learning Bullet, and made a little bowling simulation to test placing rigid bodies, applying forces, and detecting collision. Of course, there's only so much boring bowling one can do when they are in absolute control of physics, so I've added some portals and boost pads and a gravity switch to my bowling example to spice things up.
I'm having a problem with my gravity switch, though. When the ball rolls over the switch, I can detect the collision, change the color of the switch, and toggle gravity, but it seems that only the ball is affected by the change in gravity. I'd like everything in the simulation to respond to the change, including the pins.
Here's the snippet of my code:
Code: Select all
bool btCallback(btManifoldPoint& cp,const btCollisionObjectWrapper* obj1,int id1,int index1,const btCollisionObjectWrapper* obj2,int id2,int index2)
{
if(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->btid == 1){ //if obj1 is a booster, speed up obj2
btVector3 boostdir;
boostdir.setX(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getX());
boostdir.setY(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getY());
boostdir.setZ(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getZ());
((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(boostdir.x()*10,boostdir.y()*10,boostdir.z()*10));
//((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(100,0,0));
WriteLine(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->getCenterOfMassTransform().getOrigin().getX());
//WriteLine("BOOST!");
}
else if(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->btid == 1){ //if obj2 is a booster, speed up obj1
btVector3 boostdir;
boostdir.setX(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getX());
boostdir.setY(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getY());
boostdir.setZ(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->getLinearVelocity().getZ());
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->applyForce(btVector3(100,0,0),boostdir);
//((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(100,0,0));
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(boostdir.x()*10,boostdir.y()*10,boostdir.z()*10));
//WriteLine("BOOST!");
}
else if(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->btid == 2){ //if obj 1 is a portal, tranlate obj2
double radius = ((bulletObject*)obj2->getCollisionObject()->getUserPointer())->model->MaxRadius(); //happens to work because it's a sphere
((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->translate(btVector3(-230+(2*radius),-15,0)); //translate in Bullet coordinates
WriteLine("FWOOSH! ", radius);
}
else if(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->btid == 2){ //if obj 2 is a portal, tranlate obj1
double radius = ((bulletObject*)obj1->getCollisionObject()->getUserPointer())->model->MaxRadius();
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->translate(btVector3(-230+(2*radius),-15,0));
WriteLine("FWOOSH! ", radius);
}
else if(((bulletObject*)obj1->getCollisionObject()->getUserPointer())->btid == 3){ //if obj 1 is a gravity switch, toggle gravity
GravityVector = GravityVector*-1;
world->setGravity(GravityVector);
WriteLine("FLOOP! ", Now());
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->btid = 4; //deactivate gravity switch
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->model->ChangeColor(black);
}
else if(((bulletObject*)obj2->getCollisionObject()->getUserPointer())->btid == 3){ //if obj 2 is a gravity switch, toggle gravity
GravityVector = GravityVector*-1;
world->setGravity(GravityVector);
WriteLine("FLOOP! ", Now());
((bulletObject*)obj2->getCollisionObject()->getUserPointer())->btid = 4; //deactivate gravity switch
((bulletObject*)obj2->getCollisionObject()->getUserPointer())->model->ChangeColor(black);
}
else if(obj1->getCollisionObject()->isStaticObject() || obj2->getCollisionObject()->isStaticObject()) return false; //don't do anything for collisions with normal static bodies
else {
//WriteLine("Collision!");
((bulletObject*)obj1->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(0,20,0)); //apply a vertical force on any colliding objects for now
((bulletObject*)obj2->getCollisionObject()->getUserPointer())->body->applyCentralForce(btVector3(0,20,0)); //apply a vertical force on any colliding objects for now
}
return false;
}
Thanks,
BW