Posts: 10
Threads: 2
Joined: Sep 2017
Reputation:
3
Has anyone been able to successfully save the state of a rope and load it correctly? I was told to pull the position and velocity arrays and serialize those and then set them again and push the data but that doesn't seem to work - the rope goes crazy at the start.
I would love to see an example of saving rope state that also takes into account any potential lengthening from a rope cursor.
Thanks!
Posts: 10
Threads: 2
Joined: Sep 2017
Reputation:
3
So I've made a little progress and I can get the rope to initialize correctly as long as I haven't extended it previously. If I save the state of a rope that was extended with the rope cursor, I cannot successfully restore the state correctly.
Posts: 10
Threads: 2
Joined: Sep 2017
Reputation:
3
09-09-2017, 09:13 PM
(This post was last modified: 09-09-2017, 09:15 PM by ptrick.)
OK so I have made some significant progress with saving the state of a rope that has been extended.
The long and the short of it is this:
1. I have a component that manages extending the Rope using the Rope Cursor. We are using a script which was provided a while ago to check for tension and extend the rope if tension exceeds a threshold.
2. If you do not use the rope cursor, saving and restoring rope state is pretty easy and can be summarized as:
* Pull velocity and position data from solver
* Serialize the positions and velocities of the rope in JSON or whatever
* To load - we attach an event handler to the OnAddedToSolver of the rope and in that event handler, we set the positions and velocities on the rope to the serialized values after Instantiate'ing our rope. We then call PushDataToSolver
3. after much experimentation, I found that in order to be able to easily save changes when the rope has been extended we only have to do one thing, each time we use the ChangeLength method of the cursor, we add that value to a list, we call the list lengthChanges and it's a List<float>. This list gets serialized with your positions and velocities data like usual.
4. When you load this data, the order is important: first - for each entry in your lengthChanges list, call ChangeLength on the rope cursor with that value. This is basically getting your rope back to the length it was when you serialized it. THEN - set your velocities and positions and push to solver.
It really is that easy.
TLDR - keep a list of each time you call ChangeLength on your rope cursor and serialize your lengthChanges, positions, and velocities. When deserializing, first, call ChangeLength for each entry in the lengthChanges list, then set your positions and velocities and push to solver.
NOTE: If you plan on serializing multiple times, it's important that you keep track of the lengthChanges list for multiple loads otherwise, it won't work. One optimization that we have done is during the resize code, if we are shrinking the rope and we are back to the ORIGINAL size of the rope, we clear out the length changes list so that it doesn't grow infinitely.
NOTE: You CANNOT just keep the final length of the rope and call ChangeLength once - this doesn't work for whatever reason, you need to call ChangeLength as many times as you called it originally even though the end length will just be the final length of the rope, the internal state will be correct.