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 !
How to reset simulation
-
- Posts: 350
- Joined: Sat Jul 04, 2015 10:33 am
- Location: Bern, Switzerland
Re: How to reset simulation
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.
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);
-
- Posts: 11
- Joined: Sun Jan 10, 2016 5:08 pm
Re: How to reset simulation
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 !
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 !
-
- Posts: 350
- Joined: Sat Jul 04, 2015 10:33 am
- Location: Bern, Switzerland
Re: How to reset simulation
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
-
- Posts: 33
- Joined: Fri Oct 26, 2012 1:50 pm
Re: How to reset simulation
Could you post that code, benelot? I think it would be very useful!In case you are doing things with Featherstone Multibody physics, I also have reset code for that.
-
- Posts: 350
- Joined: Sat Jul 04, 2015 10:33 am
- Location: Bern, Switzerland
Re: How to reset simulation
Ok, I just looked into my code and tried to get a quick overview of how it is done:allsey87 wrote:Could you post that code, benelot? I think it would be very useful!
First reset the multibody base position:
Code: Select all
if (mMultiBody) {
mMultiBody->setBasePos(position);
}
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);
}
This is just a rough writeup, tell me if it works.