0
\$\begingroup\$

I am using a Unity Plugin Obi Rope to create rope-based mechanics. I am working on creating an obi rope curve through code and rendering rope on that curve. I am stuck at the moment where I can spawn points of the curve in the correct position but tangents are disturbed due to which the curve is irregular(as shown in the screenshot). enter image description here Following code focuses on generating curve for rope:

Vector3 tangentLS = default;
        for (int i = 0; i < ropeSaveData.ropeCurvePoints.Count; i++)
        {
            // Calculate control point positions and tangent vector:
            Vector3 currentPointPosition =
                generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i]);
            if (i == 0)
            {
                Vector3 nextPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i + 1]);
                tangentLS = (nextPointPosition - currentPointPosition).normalized;
            }
            else if (i < ropeSaveData.ropeCurvePoints.Count - 1)
            {
                Vector3 previousPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i - 1]);
                Vector3 nextPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i + 1]);
                tangentLS = (nextPointPosition - previousPointPosition).normalized;
            }

            // Build the rope path:
            blueprint.path.AddControlPoint(currentPointPosition, -tangentLS, tangentLS, Vector3.up,
                0.1f, 0.1f, 1, filter, Color.white, "start_" + i);
        }

So mainly AddControlPoint is responsible for creating curve for obi rope.

Following is the method signature:

public void AddControlPoint(Vector3 position, Vector3 inTangentVector, Vector3 outTangentVector, Vector3 normal, float mass, float rotationalMass, float thickness, int filter, Color color, string name)

Additional information around this. I tried this with 2 points(vector3) and following code works perfectly fine since this curve is just a line but the above one is not a straight line it's a list of vector3 points.

 // Calculate control point positions and tangent vector:
        Vector3 startPositionLS = generatedRope.transform.InverseTransformPoint(start.position);
        Vector3 endPositionLS = generatedRope.transform.InverseTransformPoint(end.position);
        Vector3 tangentLS = (endPositionLS - startPositionLS).normalized;

        // Create the blueprint: 
        var blueprint = ScriptableObject.CreateInstance<ObiRopeBlueprint>();
        blueprint.resolution = 0.5f;
        // Build the rope path:
        int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 0);
        blueprint.path.AddControlPoint(startPositionLS, -tangentLS, tangentLS, Vector3.up, 0.1f, 0.1f, 1,
            filter, Color.white, "start");
        blueprint.path.AddControlPoint(endPositionLS, -tangentLS, tangentLS, Vector3.up, 0.1f, 0.1f, 1, filter,
            Color.white, "end");

Helping note for readers here that are willing to help:

Tangent length affects the curvature of the path, just line in any spline. Depending on how far away from each other your points are, using normalized tangents (with length 1) may be too much and the path will have a weird shape. Try reducing the length of the tangents, proportionally to the distance between points.

This is something which is useful but I am unable to actually transform this into a code.

\$\endgroup\$
3
  • \$\begingroup\$ Did you try multiplying the tangent vector by a small number, rather than normalizing it? This would give you a tangent vector with length proportional to the distance between the points, as the quotation you cite suggests. \$\endgroup\$
    – DMGregory
    Sep 8 at 15:04
  • \$\begingroup\$ Just did and boom!! It is working now. @DMGregory Thank you for pointing this out. You always help me out :) Thank you again. \$\endgroup\$ Sep 8 at 15:19
  • \$\begingroup\$ Be sure to post your solution as an answer. \$\endgroup\$
    – DMGregory
    Sep 8 at 15:20

1 Answer 1

0
\$\begingroup\$

Thank you @DMGregory for pointing me out in the right direction.

So earlier I was just normalizing the tangents with length 1 which was creating problems since as I mentioned in the question that tangent length affects the curvature of the path so I modified that by reducing the length of the tangents, proportionally to the distance between points.

The value 0.8 is something that worked(I liked the look of it) for me so basically, my thought process behind that is to keep 80% of the distance of 2 points instead of keeping 100% thus retaining the tangent between points.

Now it is working perfectly fine. Here's the updated code:

Vector3 tangentLS = default;
        for (int i = 0; i < ropeSaveData.ropeCurvePoints.Count; i++)
        {
            // Calculate control point positions and tangent vector:
            Vector3 currentPointPosition =
                generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i]);
            if (i == 0)
            {
                Vector3 nextPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i + 1]);
                
                tangentLS = (nextPointPosition - currentPointPosition) * 0.8;
            }
            else if (i < ropeSaveData.ropeCurvePoints.Count - 1)
            {
                Vector3 previousPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i - 1]);
                Vector3 nextPointPosition =
                    generatedRope.transform.InverseTransformPoint(ropeSaveData.ropeCurvePoints[i + 1]);
              
                tangentLS = (nextPointPosition - previousPointPosition) * 0.8;
            }

            // Build the rope path:
            blueprint.path.AddControlPoint(currentPointPosition, -tangentLS, tangentLS, Vector3.up,
                0.1f, 0.1f, 1, filter, Color.white, "start_" + i);
        }
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .