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). 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.