How to reset simulation

Post Reply
lcrivell
Posts: 11
Joined: Sun Jan 10, 2016 5:08 pm

How to reset simulation

Post by lcrivell »

Hi,
This sounds a basic question, but I could not find a clear answer.
I am developing a plugin in c++, using bullet libraries.

Once I have run x steps of the simulation, how can I reset the simulation so that the objects get in their original location and state, without removing them and adding them again ?
Thanks !
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to reset simulation

Post by benelot »

My solution to the problem is the following:

What you have to do in order to reset the simulation is the clearing of forces and velocity and reset the objects to its original positions. Maybe I do too much, but this works for me and does everything I want. I save the original position of one base rigidbody and save the relative position for all the others. But you can simply leave out this step and save the absolute positions and orientations. Here is the code you need.

Code: Select all


	mBody->clearForces();
	btVector3 zeroVector(0, 0, 0);
	mBody->setLinearVelocity(zeroVector);
	mBody->setAngularVelocity(zeroVector);

	btVector3 initialRelativePosition;
	initialRelativePosition.setValue(getInitialRelativePosition());

	btQuaternion initialOrientation;
	initialOrientation.setValue(getInitialOrientation());

	initialTransform.setOrigin(position + initialRelativePosition);
	initialTransform.setRotation(initialOrientation);

	mBody->setWorldTransform(initialTransform);
	mMotionState->setWorldTransform(initialTransform);
lcrivell
Posts: 11
Joined: Sun Jan 10, 2016 5:08 pm

Re: How to reset simulation

Post by lcrivell »

Thanks. I am pretty new in bullet, hence my basic questions.
I was not sure if Bullet had a cache of initial positions, but I understand that it is not the case.
Will try this out, but I understand what you want to achieve. Cheers !
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to reset simulation

Post by benelot »

No, it does not have a cache of initial positions. You have to provide it yourself. If you have more questions, just ask! In case you are doing things with Featherstone Multibody physics, I also have reset code for that. Cheers
allsey87
Posts: 33
Joined: Fri Oct 26, 2012 1:50 pm

Re: How to reset simulation

Post by allsey87 »

In case you are doing things with Featherstone Multibody physics, I also have reset code for that.
Could you post that code, benelot? I think it would be very useful!
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: How to reset simulation

Post by benelot »

allsey87 wrote:Could you post that code, benelot? I think it would be very useful!
Ok, I just looked into my code and tried to get a quick overview of how it is done:

First reset the multibody base position:

Code: Select all

	if (mMultiBody) {
		mMultiBody->setBasePos(position);
	}
Then for each MultiBodyLinkCollider, you do:

Code: Select all


        btTransform initialTransform;
	initialTransform.setIdentity();

	btVector3 initialRelativePosition;
	initialRelativePosition.setValue(getInitialRelativeXPosition(),
		getInitialRelativeYPosition(), getInitialRelativeZPosition());

	btQuaternion initialOrientation;
	initialOrientation.setValue(getInitialXOrientation(),
		getInitialYOrientation(), getInitialZOrientation(),
		getInitialWOrientation());

	initialTransform.setOrigin(position + initialRelativePosition);
	initialTransform.setRotation(initialOrientation);

	if (mLink) {
		mLink->setWorldTransform(initialTransform);
	}
where position is the base position and the relative position is the position of this multibodylinkcollider relative to the base. This can be simplified if absolute coordinates are given. You should get the point.

This is just a rough writeup, tell me if it works.
Post Reply