Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Rope saving and restore by Json
#11
(25-06-2024, 09:58 AM)josemendez Wrote: You'll need to store blueprint control points as well anyway, so the start/end points of each rope will be stored in your JSON. At this point you can probably just have one scene in your game and store the entire level setup in JSON.


Sure! let me know if you need further help Sonrisa

Hi, I'm back, sorry to bother you again. After some attempts, I have made some significant progress, which is really very exciting.  Guiño
But now I have encountered some new problems. I have uploaded the latest results and problem pictures in the attachment.


Following your advice, I have now saved the length and position of the particles, as well as the ControlPoints in the blueprint, while inTangent and outTangent are Vector.zero, so I didn't save it in order to save space. When instantiating the rope, the originally stored controlPoints are restored through the blueprint's AddControlPoints() method, and after the blueprint is fully loaded, the stored positions are reset to the particles, which allows for the restoration of the shape of the rope for the vast majority of the time as it was before it was saved. So now the generated rope is really similar to what it was before it was saved. But there is still some loss of detail, which directly affects the difficulty of the game in the actual gameplay.


Here is the code to save controlPoints.
Code:
JSONObject GetRopeControlPoints()
    {
        var rope = gameObject.GetComponent<ObiRope>();
        var pointsArray = JSONObject.emptyArray;
        //var inTangentArray = JSONObject.emptyArray;
        //var outTangentArray = JSONObject.emptyArray;
        foreach (var obiWingedPoint in rope.ropeBlueprint.path.m_Points.data)
        {
            pointsArray.Add((JSONNode)obiWingedPoint.position);
            //inTangentArray.Add((JSONNode)obiWingedPoint.inTangent);
            //outTangentArray.Add((JSONNode)obiWingedPoint.outTangent);
        }
        var jsonObject = JSONObject.emptyObject;
        jsonObject.AddField("points", pointsArray);
        //jsonObject.AddField("inTangent", inTangentArray);
        //jsonObject.AddField("outTangent", outTangentArray);
        return jsonObject;
    }


Here is the code when instantiating the rope.
Code:
        ObiRopeBlueprint blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
        blueprint.path.Clear();
        blueprint.resolution = 1;
       
        int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
       
        int posIndex = 0;
        foreach (var obiWingedPoint in controlPoints)
        {
            blueprint.path.AddControlPoint(obiWingedPoint.position, obiWingedPoint.inTangent, obiWingedPoint.outTangent,
                Vector3.up, ropeMass, 0.1f, ropeThickness, filter, Color.yellow, $"pos_{posIndex}");
            posIndex++;
        }
     
        blueprint.path.FlushEvents();
        blueprint.GenerateImmediate();     
   
        ObiParticleAttachment attachment1 = rpObject.AddComponent<ObiParticleAttachment>();
        ObiParticleAttachment attachment2 = rpObject.AddComponent<ObiParticleAttachment>();
     
        rope.ropeBlueprint = blueprint;
       
        attachment1.target = transformA;
        attachment2.target = transformB;
        attachment1.particleGroup = blueprint.groups[0];
        attachment2.particleGroup = blueprint.groups.Last();

        // Parent the actor under a solver to start the simulation:
        rpObject.transform.SetParent(obiSolverObject.transform);
       
        return rpObject;


The images in the attachment should sufficiently illustrate the situation of the problem. Here, I will provide a brief description as well, just in case the images cannot be viewed:  After the restoration, the shape of the rope is really close to it was saved. However, some areas where the ropes are twisted with each other are lost, regardless of the complexity of the twists. As can be seen in image 6, even simple windings can sometimes be lost, even though they had clearly occurred by the first frame.
From image 5, it can be observed that the particles extracted from rope.solverIndices are not contiguous in position, which I believe to be normal according to the Obi documentation. However, I am not certain if this is the cause of the loss of detail.

可以看到从rope.solverIndices中取出的particle,在位置上并不是连续在一起的,根据obi的资料后,我觉得这应该是正常的。但是不确定是否这是导致细节丢失的原因。
The order of the particles in rope.elements is coherent. I have tried to save and restore these particles, but the problem persists.

In the level, there are pre-placed Obi solvers with various parameters and constraints that have been adjusted. The instantiated rope has been saved as a prefab, and the parameters have also been adjusted. Therefore, the parameters and constraints applied when restoring the rope using the data should be the same as before the data was saved. Thus, I did not store the constraints in the JSON, and no operations were performed during restoration.

Is there any step that I have missed?


Attached Files Thumbnail(s)
           
Reply


Messages In This Thread
Rope saving and restore by Json - by tapLucas - 22-06-2024, 03:32 PM
RE: Rope saving and restore by Json - by tapLucas - 22-06-2024, 06:37 PM
RE: Rope saving and restore by Json - by tapLucas - 25-06-2024, 09:02 AM
RE: Rope saving and restore by Json - by tapLucas - 25-06-2024, 09:31 AM
RE: Rope saving and restore by Json - by tapLucas - 25-06-2024, 09:55 AM
RE: Rope saving and restore by Json - by tapLucas - 25-06-2024, 10:26 AM
RE: Rope saving and restore by Json - by tapLucas - 27-06-2024, 11:54 AM
RE: Rope saving and restore by Json - by tapLucas - 27-06-2024, 12:59 PM
RE: Rope saving and restore by Json - by tapLucas - 11-07-2024, 07:21 AM
RE: Rope saving and restore by Json - by tapLucas - 17-08-2024, 09:02 AM
RE: Rope saving and restore by Json - by tapLucas - 17-08-2024, 10:37 AM
RE: Rope saving and restore by Json - by tapLucas - 17-08-2024, 11:10 AM