Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  Issue Saving/Loading Particle Positions in play mode using ObiRope v7.0
#9
Alright, I just realized I forgot to add this:

Code:
rope.CopyParticle(rope.activeParticleCount - 1, rope.activeParticleCount);
Now, the ropes load exactly as per the particle positions I saved.

Here’s the final version of the Load method:
Code:
public void Load()
    {
        int particleCount = particlePositions.Length;
        while (rope.activeParticleCount > particleCount)
        {
            rope.elements.RemoveAt(rope.elements.Count - 1);
            rope.DeactivateParticle(rope.activeParticleCount - 1);
        }

        while (rope.activeParticleCount < particleCount)
        {
            rope.elements.Add(new ObiStructuralElement
            {
                particle1 = rope.elements[^1].particle2,
                particle2 = rope.solverIndices[rope.activeParticleCount],
                restLength = rope.interParticleDistance
            });
            rope.CopyParticle(rope.activeParticleCount - 1, rope.activeParticleCount);
            rope.ActivateParticle();
        }

        for (int j = 0; j < rope.elements.Count; ++j)
        {
            int firstParticle = rope.elements[j].particle1;
            rope.solver.positions[firstParticle] = particlePositions[j];
            rope.solver.velocities[firstParticle] = Vector4.zero;
            rope.solver.angularVelocities[firstParticle] = Vector4.zero;
        }

        int lastParticle = rope.elements[^1].particle2;
        rope.solver.positions[lastParticle] = particlePositions[^1];
        rope.solver.velocities[lastParticle] = Vector4.zero;
        rope.solver.angularVelocities[lastParticle] = Vector4.zero;
        rope.RebuildConstraintsFromElements();
        AddConstraints();
    }

However, in the last line of the Load method, within the AddConstraints method, something seems to be going wrong.

I adapted the code from the Adding/Removing Constraints section to my use case, but it seems like colliderB isn’t linking to the last particle, while colliderA is working as intended.


Here’s the AddConstraints() method:
Code:
[SerializeField] private ObiCollider colliderA;
[SerializeField] private ObiCollider colliderB;
private void AddConstraints()
{
    //first particle in the rope is the first particle of the first element:
    int firstParticle = rope.elements[0].particle1;

    // last particle in the rope is the second particle of the last element:
    int lastParticle = rope.elements[^1].particle2;

    // now get their positions (expressed in solver space):
    Vector3 firstPos = rope.solver.positions[firstParticle];
    Vector3 lastPos = rope.solver.positions[lastParticle];

    colliderA.transform.position = firstPos;
    colliderB.transform.position = lastPos;

    // get a hold of the constraint type we want, in this case, pin constraints:
    var pinConstraints = rope.GetConstraintsByType(Oni.ConstraintType.Pin) as ObiConstraints<ObiPinConstraintsBatch>;

    // remove all batches from it, so we start clean:
    pinConstraints.Clear();

    // create a new pin constraints batch
    var batch = new ObiPinConstraintsBatch();

    // Add a couple constraints to it, pinning the first and last particles in the rope:
    batch.AddConstraint(firstParticle, colliderA, Vector3.zero, Quaternion.identity, 0, 0);
    batch.AddConstraint(lastParticle, colliderB, Vector3.zero, Quaternion.identity, 0, 0);


    // set the amount of active constraints in the batch to 2 (the ones we just added).
    batch.activeConstraintCount = 2;

    // append the batch to the pin constraints:
    pinConstraints.AddBatch(batch);

    // this will cause the solver to rebuild pin constraints at the beginning of the next frame:
    rope.SetConstraintsDirty(Oni.ConstraintType.Pin);
}

This script used to work normally in v6. As far as I can tell, the only change is the tearRope parameter:

batch.AddConstraint(firstParticle, colliderA, new Vector3(0, 0, 0), Quaternion.identity, 0, 0, float.PositiveInfinity);

What could be my mistake here?

https://www.youtube.com/watch?v=A-5ZFtkJ89k

Yellow sphere that is visible on the video is colliderA and the Blue one is colliderB



Thanks.
Reply


Messages In This Thread
RE: Issue Saving/Loading Particle Positions in play mode using ObiRope v7.0 - by ozeecode - 29-04-2025, 01:40 PM