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];
}
}
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];
}
}