Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Climbing on rope and fix to move only on x, y
#7
(05-10-2021, 09:00 AM)josemendez Wrote: Ropes simply store a list of elements, and a list of particles. Elements are guaranteed to appear in the list in the same order they do in the rope, particles don't. Each element contains the solver indices of the 2 particles at its ends. This is all the info you can work with.

So finding the nth neighbor of a particle is very simple: you first need to find the first element that references it, and then just count "n" elements up/down of it. This can be done in linear time.

I wrote this method off the top of my head, haven't tested it but should work unless morning coffee hasn't kicked in yet. Returns the solver index of the nth neighbor particle. Takes a rope, actor index and neighbor offset as parameters:

Code:
int GetParticleNeighborIndex(ObiRopeBase rope, int particleIndex, int offset)
{
        int solverIndex = rope.solverIndices[particleIndex];
        int elementCount = rope.elements.Count;

        // find element for the current particle:
        int e = 0;
        for (; e < elementCount; ++e)
            if (rope.elements[e].particle1 == solverIndex) break;

        // return solver index of nth neighbor (accounting for special cases)
        if (e == elementCount - 1 && offset > 0)
            return rope.elements[e].particle2;
        else
        {
            int n = Mathf.Clamp(e + offset, 0, elementCount - 1);
            return rope.elements[n].particle1;
        }
    }

Also, keep in mind you could cache your current element to avoid having to iterate trough all elements every time you ask for a neighbor. If you know where in the rope you currently are, you can just go up and down from there in constant time.


This code is working if I have only 1 rope in the solver, but if I put 2 ropes, I get this:

IndexOutOfRangeException: Index was outside the bounds of the array.


var solverIndex = _rope.solverIndices[particleIndex];
_MyScripts.CreateObiRope.GetParticleNeighborIndex (System.Int32 particleIndex, System.Int32 offset) (at Assets/_MyScripts/CreateObiRope.cs:332)
_MyScripts.CreateObiRope.StayOnRope () (at Assets/_MyScripts/CreateObiRope.cs:246)
_MyScripts.CreateObiRope.Climb () (at Assets/_MyScripts/CreateObiRope.cs:224)
_MyScripts.CollideObiRope.Update () (at Assets/_MyScripts/CollideObiRope.cs:76)
Reply


Messages In This Thread
RE: Climbing on rope and fix to move only on x, y - by lacasrac - 05-10-2021, 01:22 PM