Howto reset/clean an object from the physics world?

gagou7
Posts: 2
Joined: Tue Jun 28, 2011 9:48 pm

Howto reset/clean an object from the physics world?

Post by gagou7 »

Hi all,

Sorry for my bad english, I'm French.

I search a lot and found nothing that can help me. I have a world (btDiscreteDynamicsWorld) and I would like to add a ball with an origin and a impulse that represent a ball toss. This is okay, but I would like to reset the world sometimes because an evenement from another part of my project announce a second ball toss.

When I want a new ball in my world, I re-use the ball before. So I remove rigidbody from the world and re-add it after a ->clearForces() on the world and rigidbody. But, If I do that before the ball going down to the floor, the gravity of the second ball looks like increased. If I reset the world like this when the ball hit the floor, the second ball is sent anywhere.

My question is: How can I can reset completely an object from the world for being re-added without "force memory"? I know, I can delete and add another object "Ball" but I want a solution without instantiating a new object.

Thank you.
User avatar
dphil
Posts: 237
Joined: Tue Jun 29, 2010 10:27 pm

Re: Howto reset/clean an object from the physics world?

Post by dphil »

Clearing forces just clears the forces :) . The body can still have a pre-existing velocity. So try something like this:

Code: Select all

world->removeRigidBody(ball);

// reset ball
ball->clearForces();
btVector3 zeroVector(0,0,0);
ball->setLinearVelocity(zeroVector);
ball->setAngularVeclocity(zeroVector);
ball->setWorldTransform(startingTransform); // reset ball position

world->addRigidBody(ball);
Come to think of it, you probably don't even need to remove and re-add the ball. That is, you could probably remove the "removeRigidBody" and "addRigidBody" lines above, and it would still work perfectly fine.
gagou7
Posts: 2
Joined: Tue Jun 28, 2011 9:48 pm

Re: Howto reset/clean an object from the physics world?

Post by gagou7 »

Oh !!! it works !!! Thank you very much !!! :D

I searched, I searched and never found anything... Thank you !!!

[EDIT]
don't forget "mBody->setActivationState(DISABLE_DEACTIVATION);" or the object (ball in my project) will not be replaced after the end of simulation.