18-03-2021, 02:29 PM
This is what I thought too. But still the rope even when not tense , returns value >1. Am I missing something ?
Code:
public class RopeTensity : MonoBehaviour
{
[Header("Obi Rope Fields")]
[SerializeField]
private ObiSolver solver;
[SerializeField]
private ObiRope rope;
[Header("Points to check")][Tooltip("Use these values like a percentage")]
[SerializeField]
private int startPointIndex;
[SerializeField]
private int endPointIndex;
public float strain;
private void Update()
{
if (IsPartStreched())
Debug.Log("PART STRECHED!");
}
public bool IsPartStreched()
{
strain = CalculateLength() / CalculateRestLength();
Debug.Log(rope.elements.Count);
if (strain > 1)
return true;
else
return false;
}
private float CalculateLength()
{
float length = 0;
if (rope!=null)
{
// Iterate trough all distance constraints in order:
for (int i = startPointIndex; i < endPointIndex ; ++i)
length += Vector4.Distance(solver.positions[rope.elements[i].particle1],
solver.positions[rope.elements[i].particle2]);
}
return length;
}
private float CalculateRestLength()
{
float restLength_ = 0;
// Iterate trough all distance elements and accumulate their rest lengths.
for (int i = startPointIndex; i < endPointIndex; ++i)
restLength_ += rope.elements[i].restLength;
return restLength_;
}
}