Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Rope saving and restore
#1
Hello!
I have this question, I'm making a game where ropes are tied in knots and hold each other. In the first version I tied all the ropes in the editor using blueprints, which I edited and then saved. In this variant the ropes hold each other quite well, even if you try to move them by static attachments. You don't have to move them much, so they don't usually go through each other.

Then I changed to an editor that runs in runtime, and you can tie the ropes by rearranging the attachments. Then I save positions and velocities in json for particles. I also save the position of attachments.

Then I restore them in the real game. First I load prefab, where the ropes are in the initial position, as in the template. Then I restore from json positions and velocities of particles. And positions of the attachments.
The ropes are restored without problems, they look exactly as they should at the beginning. But then the ropes start to hold each other very badly. They hold a little, but at any attempt to move them they start to pass through each other.

Maybe there's something else I'm forgetting to do. Maybe I need to call some method to rebuild some constraints after position and velocity of partials are restored?

This is my code for saving and restoring ropes it is script on the rope object itself. Tip1 and Tip2 - are static attachments

public JSONObject SaveRopeToJSONObject()
{
    var solver = _rope.solver;
    var particlesIndices = _rope.solverIndices;
    var positions = new List<Vector3>();
    var velocities = new List<Vector3>();
    for (int i = 0; i < particlesIndices.Length; i++)
    {
        positions.Add(solver.positions[particlesIndices[i]]);
        velocities.Add(solver.velocities[particlesIndices[i]]);
    }
   
    var positionsArray = new JSONArray();
    var velocitiesArray = new JSONArray();
    for (int i = 0; i < positions.Count; i++)
    {
        var p = (JSONNode)positions[i];
        positionsArray.Add(p);
        var v = (JSONNode)velocities[i];
        velocitiesArray.Add(v);
    }
   
    var ropeObject = new JSONObject();
    ropeObject.Add("Tip1Pos", (JSONNode)_ropeTip1.transform.position);
    ropeObject.Add("Tip2Pos", (JSONNode)_ropeTip2.transform.position);
    ropeObject.Add("Tip1Rot", (JSONNode)_ropeTip1.transform.rotation.eulerAngles);
    ropeObject.Add("Tip2Rot", (JSONNode)_ropeTip2.transform.rotation.eulerAngles);
    ropeObject.Add("positions", positionsArray);
    ropeObject.Add("velocities", velocitiesArray);

    return ropeObject;
}


public void RestoreRopeFromJSONObject(JSONObject ropeObject)
{
    _ropeTip1.transform.position = (Vector3)ropeObject["Tip1Pos"];
    _ropeTip2.transform.position = (Vector3)ropeObject["Tip2Pos"];
    _ropeTip1.transform.rotation = Quaternion.Euler((Vector3)ropeObject["Tip1Rot"]);
    _ropeTip2.transform.rotation = Quaternion.Euler((Vector3)ropeObject["Tip2Rot"]);

    var ropeParticlesPosition = ropeObject["positions"].AsArray;
    var ropeParticlesVelocities = ropeObject["velocities"].AsArray;
   
    var solver = _rope.solver;
    var particlesIndices = _rope.solverIndices;
    for (int i = 0; i < particlesIndices.Length; i++)
    {
        solver.positions[particlesIndices[i]] = (Vector3)ropeParticlesPosition[i];
        solver.velocities[particlesIndices[i]] = (Vector3)ropeParticlesVelocities[i];
    }
}
Reply
#2
Hi!

Just setting particle positions and velocities should not affect how collision detection between ropes behaves in any way.

When loading the saved positions/velocities on a rope, is this rope (the one in the prefab you load) the same one you used to save the positions? I mean, is every other per-particle property the same (collision filters, mass, etc), and are constraints also the same?

kind regards
Reply
#3
(20-02-2024, 08:53 AM)josemendez Wrote: Hi!

Just setting particle positions and velocities should not affect how collision detection between ropes behaves in any way.

When loading the saved positions/velocities on a rope, is this rope (the one in the prefab you load) the same one you used to save the positions? I mean, is every other per-particle property the same (collision filters, mass, etc), and are constraints also the same?

kind regards

Thanx for the answer!

yes, I created a rope prefab.
And load it in level editor before editing and in real game before loading positions and velocities. Filters, mass, etc are the same.
May be I should wait for blueprint loading before setting particles positions? Because I RestoreRopeFromJSONObject right after instantiating the prefab with ropes (template)
Reply
#4
(20-02-2024, 09:11 AM)Apoll0 Wrote: May be I should wait for blueprint loading before setting particles positions? Because I RestoreRopeFromJSONObject right after instantiating the prefab with ropes (template)

Yes, you should wait for the rope to be loaded into the solver. You can subscribe to the rope's OnBlueprintLoaded event to get notified when this happens. Not waiting will likely lead to out of bounds access exception, since rope.solverIndices won't be initialized yet.
Reply