Resetting objects in a simulation

Gogo
Posts: 10
Joined: Sun Feb 22, 2009 1:47 pm

Resetting objects in a simulation

Post by Gogo »

Hi,

So.. I've been trying to reset several rigidbodies to their initial (i.e stored) positions during a physical simulation. As per instructions on the forum, I remove the bodies from the world, make the required changes, then add them back to the world. However, for some reason, this doesn't quite seem to work as, although the objects are reset to their initial positions correctly, they do not seem to recognise the WANTS_DEACTIVATION flag that I set before adding back to the world, as the objects immediately start falling.

Here is the relevant code I'm using. You'll also notice that I've added a few seemingly-unneccessary lines in there (including setting the WANTS_DEACTIVATION again after adding to world, etc), in my desperation to get this to work ;)

Code: Select all

	for(int model = 0; model < (6); model++){
		testPhysics.dynamicsWorld->removeRigidBody(scene[model].modelRigidBody);
		scene[model].modelRigidBody->setWorldTransform(scene[model].initialTransformInv->inverse());
		scene[model].modelRigidBody->setInterpolationWorldTransform(scene[model].initialTransformInv->inverse());
		 testPhysics.dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(scene[model].modelRigidBody->getBroadphaseHandle(),testPhysics.dynamicsWorld->getDispatcher());
		
		scene[model].modelRigidBody->getMotionState()->setWorldTransform(scene[model].initialTransformInv->inverse());
		scene[model].modelRigidBody->setCenterOfMassTransform(scene[model].initialTransformInv->inverse());
		scene[model].modelRigidBody->clearForces();
		scene[model].modelRigidBody->setLinearVelocity(btVector3(0,0,0));
		scene[model].modelRigidBody->setAngularVelocity(btVector3(0,0,0));
		scene[model].modelRigidBody->setActivationState(WANTS_DEACTIVATION);
		scene[model].modelRigidBody->wantsSleeping();
		*scene[model].transform = scene[model].initialTransformInv->inverse();
		
	}
	for(int model = 0; model < (6); model++){
		testPhysics.dynamicsWorld->addRigidBody(scene[model].modelRigidBody);
		scene[model].modelRigidBody->setActivationState(WANTS_DEACTIVATION);
		//scene[model].modelRigidBody->setGravity(btVector3(0,0,0));
	}
I really can't see why this isn't working.. especially as I'm fairly sure I've done something like this previously (with an earlier version of Bullet fyi.. although I'm hesitant to actually suggest anything has been broken inbetween versions!). What is also interesting is that, if i set each rigidbodies' local gravity to (0,0,0), all the bodies seem to be drifting very slowly upwards... possibly implying that each rigidbody has a force/impulse applied that I'm unaware of. Although, that still makes no sense, as the bodies always float directly upwards, regardless of their directional movement prior to removing them from the world.

Anyway, I've been stuck trying to fix this for a while now, so I could really appreciate some help!
Gogo
Posts: 10
Joined: Sun Feb 22, 2009 1:47 pm

Re: Resetting objects in a simulation

Post by Gogo »

Aww man, was really hoping somebody had an answer to this question... been pulling my hair over this for a few days now!

Maybe I should try rephrasing my question in simpler terms.... Is there any reason why a rigid body would ignore its sleeping flag? It worked during initialisation, but doesn't work again...

Please..? Someone..? Anyone..? :)

Thanks