my explosion code causes game to crash

Post Reply
PgrAm
Posts: 12
Joined: Sun Apr 15, 2012 3:34 pm

my explosion code causes game to crash

Post by PgrAm »

I wrote some code to create an explosion and I seem to be having some trouble with it. When it is used it causes the game to crash. I assumed this was because I was trying to upcast a btRigidBody from a btCollsionObject that didn't have one. I check to see if the object is static or not but maybe that's not enough. Here is my code:

Code: Select all

void explodeAtPos(const btVector3& pos, float force, float radius)
{
	btGhostObject* explosionRadius = new btPairCachingGhostObject();

	btCollisionShape* explosionshape = new btSphereShape(radius);

	explosionRadius->setCollisionShape(explosionshape);
	explosionRadius->setCollisionFlags(explosionRadius->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);

	dynamicsWorld->addCollisionObject(explosionRadius, btBroadphaseProxy::SensorTrigger, btBroadphaseProxy::AllFilter);

	btTransform explosionTrans;
	explosionTrans.setIdentity();
	explosionTrans.setOrigin(pos);

	explosionRadius->setWorldTransform(explosionTrans);

	for(int i = 0; i < explosionRadius->getOverlappingPairs().size(); i++)
	{
		if(!explosionRadius->getOverlappingPairs().at(i)->isStaticObject())
		{
			btCollisionObject* forcedObject = explosionRadius->getOverlappingPairs().at(i);

			btTransform forcedTrans = forcedObject->getWorldTransform();

			btVector3 forceDirection = forcedTrans.getOrigin() - pos;

			forceDirection.normalize();

			btRigidBody::upcast(forcedObject)->applyCentralImpulse(forceDirection*force); //problem is here, commenting out this line prevents crash
		}
	}

	dynamicsWorld->removeCollisionObject(explosionRadius);
	delete explosionRadius;
	delete explosionshape;
}
the problem seems to be where I upcast to a btRigidBody and apply an impulse to it. If anyone can give any insight on what might be wrong with it I would be infinitely thankful.
Post Reply