(23-06-2020, 01:00 AM)anonzy Wrote: Hey,
I was wondering if there was any way to calculate the current length of a rope between two specified control points.
Is this possible?
Thanks for the help!
Not built-in. However, can be done with a bit of coding.
Ropes are made of particles connected by
elements. Getting the length between any two elements in the rope is quite easy. You can take the CalculateLength() method in ObiRopeBase.cs as reference. It iterates over all elements in the rope, summing the distance between the two particles in each element. So instead of iterating trough them all, we can iterate trough a range of them:
Code:
float length = 0;
if (rope.isLoaded)
{
// Iterate trough all distance constraints in order:
for (int i = firstElementIndex; i < lastElementIndex; ++i)
length += Vector4.Distance(rope.solver.positions[rope.elements[i].particle1], rope.solver.positions[rope.elements[i].particle2]);
}
return length;
Now, you could do some searching to know the element range between two given control points. The rope blueprint generates a ObiParticleGroup for each control point. Say we need to know the length between the second and the third control points: the first particle group will contain the index of the first particle of the first element. The second particle group will contain the index of the second particle of the second element. Hope that makes sense! Here's some code:
Code:
var group1 = rope.blueprint.groups[1]; // second cp
var group2 = rope.blueprint.groups[2]; // third cp
// look for the first element:
int firstElementIndex;
for (int i = 0; i < elements.Count; ++i)
if (elements[i].particle1 == solverIndices[group1.particleIndices[0]])
{
firstElementIndex = i; break;
}
// repeat the same for the second element.
int secondElementIndex;
for (int i = 0; i < elements.Count; ++i)
if (elements[i].particle2 == solverIndices[group2.particleIndices[0]])
{
secondElementIndex = i; break;
}
//Now we have the element range we can pass to the first code snippet, to calculate the length between them.