20-05-2024, 09:29 AM
(This post was last modified: 20-05-2024, 10:02 AM by Harshid123.)
(17-05-2024, 03:47 PM)Harshid123 Wrote: I have a similar setup as the above image, How to know if the rope is not tangled anymore? like in the first image. I have enabled the surface collisions and tried with OnParticleCollision but it has not worked for me. can I ray cast from each particle of the rope a bit upward and check if it has collided with another rope? or is there any other simple workaround for this?
Code:
public IEnumerator CheckIfTangled()
{
yield return new WaitForSeconds(1);
int filter = ObiUtils.MakeFilter(ObiUtils.CollideWithEverything, 1);
for (int i = 0; i < rope.activeParticleCount; ++i)
{
yield return null;
Vector3 pos = rope.GetParticlePosition(rope.solverIndices[i]);
Ray ray = new Ray(pos, Vector3.back * 10f);
if (solver.Raycast(ray, out QueryResult result, filter, 100, 0.5f))
{
int simplexStart = solver.simplexCounts.GetSimplexStartAndSize(result.simplexIndex, out int simplexSize);
for (int j = 0; j < simplexSize; ++j)
{
yield return null;
int particleIndex = solver.simplices[simplexStart + j];
ObiSolver.ParticleInActor pa = solver.particleToActor[particleIndex];
if (pa.actor != rope)
{
// rope is tangled here return.
yield break;
}
}
}
}
// rope is not tangled here.
}