New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot reproduce simulation with same space and actions (impulse) #181
Comments
There are some hidden state in the underlying Chipmunk library which Pymunk uses for the physics calculations. Basically it has to do with caching impulses for resolving contact between steps. So, if you create a new space, add some things and run simulation I think it should behave the same. If you dont rewind all the way to the beginning the cached impulses will be missing, and you will get different behavior. Does this match the behavior you are seeing? |
Thank you for your reply but an issue is that I need to resume not only the position but also velocity. I currently manually set these values using |
I tried to use apply force instead of apply impulse, and the simulation can be reproduced. I think applying force does not change underlying cached impulse in this space, is this correct? |
Both methods will activate (wake from sleep) the body. I should add that under to hood the damped spring works by applying impulses, so if the base apply impulse is unreliable so will damped springs. Some things I can think of:
|
`
create a space with 15 balls connected by springs, and save the space before the step in `import pickle with open('space.pkl', 'rb') as f: print("print state") space.step(0.02) print("print state") loads the I expected the states after the step are the same, but the velocity have difference up to 0.1 which will accumulate to large values in about 1000 time steps. |
Set |
Ok, thanks. This will need more investigation |
I tried to print out impulses on springs and it shows they are 0s for space loaded from the pickle but the impulses in the original engine are not 0s. I wonder if that may cause this issue. |
Ive made some more investigation into this. Good news is that I found one problem which is easy to fix: Bodies and constraints in the space are kept in a set. When pickle / unpickle the set will be in different order (since sets are unordered). I committed a fix for this just now, which will make the order fixed, as long as you are using Python 3.6 or newer (since my fix is using dict instead, and they are ordered starting from Python 3.6.). I think this is a ok tradeoff, since otherwise Pymunk would have to include or depend on a 3rd party library for ordered set. If I also set the |
Small update: To make it fully stable the cached collision data needs to be exported out from Chipmunk. Currently Chipmunk does not provide any API for this, so it needs to be updated to allow the data to be extracted, and then Pymunk can be updated to read/write it when pickling. Im currently in the process of modifing the binding code (Pymunk uses CFFI to talk with the c library) to better allow these kinds of modifications. But I think it will take some time to make work.. So, I will keep this issue open, and take another look at it once the binding code updating is completed. |
After quite some time I have now implemented a fix for this issue with #221 Pickling & unpickling should work as expected with the same result each time. This will be included in the next release of Pymunk. |
I am using Pymunk version 5.6.0 on ubunutu 18.04.
I write a simulation environment with a set of dynamics balls connected by damped springs. I apply a series of actions to these balls in the space and I recorded the actions and velocity changes of these balls. I also store the initial space variable into a pickle. However, when I load the space and apply the same actions to balls in the space, the velocity changes are different. The difference in the L2 norm is about 0.02 for 15 balls. But when I apply actions for 2000 steps, the error will accumulate to 10-20. I am wondering whether it is correct to expect the same change of velocities. Also, I am thinking whether space itself contains all the information I need to reproduce the result.
The text was updated successfully, but these errors were encountered: