Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help  extanding the rope from its end towards a diractino using the RopeCurser
#7
Hi,

Please find attached a modified version of the grappling hook that extends over time:

.cs   ExtendableGrapplingHook.cs (Size: 7.79 KB / Downloads: 10)

To change how fast the rope shoots out of the character, change the public "hookShootSpeed" variable. It's expressed in meters/second.

Besides the boilerplate code to create the actor, blueprint, set materials, etc. the code is very simple and I have commented it. The important bit is the extension code, which I will paste here:

Code:
        // set masses to zero, as we're going to override positions while we extend the rope:
        for (int i = 0; i < rope.activeParticleCount; ++i)
            solver.invMasses[rope.solverIndices[i]] = 0;

        float currentLength = 0;

        // while the last particle hasn't reached the hit, extend the rope:
        while (true)
        {
            // calculate rope origin in solver space:
            Vector3 origin = solver.transform.InverseTransformPoint(rope.transform.position);

            // update direction and distance to hook point:
            Vector3 direction = hookAttachment.point - origin;
            float distance = direction.magnitude;
            direction.Normalize();

            // increase length:
            currentLength += hookShootSpeed * Time.deltaTime;

            // if we have reached the desired length, break the loop:
            if (currentLength >= distance)
            {
                cursor.ChangeLength(distance);
                break;
            }

            // change rope length (clamp to distance between rope origin and hook to avoid overshoot)
            cursor.ChangeLength(Mathf.Min(distance, currentLength));

            // iterate over all particles in sequential order, placing them in a straight line while
            // respecting element length:
            float length = 0;
            for (int i = 0; i < rope.elements.Count; ++i)
            {
                solver.positions[rope.elements[i].particle1] = origin + direction * length;
                solver.positions[rope.elements[i].particle2] = origin + direction * (length + rope.elements[i].restLength);
                length += rope.elements[i].restLength;
            }

            // wait one frame:
            yield return null;
        }

        // restore masses so that the simulation takes over now that the rope is in place:
        for (int i = 0; i < rope.activeParticleCount; ++i)
            solver.invMasses[rope.solverIndices[i]] = 10; // 1/0.1 = 10

These are the steps taken:

1) disable simulation for all particles by giving them infinite mass.
in a coroutine loop
{
2) calculate the vector from the character to the point on the wall
3) increase rope length, if it is long enough exit loop.
4) place particles in a straight line.
5) wait until next frame
}
6) re-enable simulation by giving non-infinite mass to particles.

Here's a video of it:


let me know if I can be of further help. cheers,
Reply


Messages In This Thread
RE: extanding the rope from its end towards a diractino using the RopeCurser - by josemendez - 13-01-2022, 02:29 PM