09-08-2017, 12:40 AM
I am going to be cutting ropes, with multiple ropes on one solver. I have a knife which tears the rope at the particle index collided with. The only problem is I don't know how to get which rope is being collided with to cut the right one. I've looked through the classes and couldn't find anything. Right now I have a public field to apply the tear to a rope. Here is the code, which is applied to the knife object.
Code:
using UnityEngine;
using Obi;
public class RopeCutter : MonoBehaviour
{
public ObiSolver solver;
public ObiRope rope;
void OnEnable()
{
solver.OnCollision += Solver_OnCollision;
}
void OnDisable()
{
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision(object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
if (solver.colliderGroup == null) return;
foreach (Oni.Contact c in e.contacts)
{
// make sure this is an actual contact:
if (c.distance < 0.01f)
{
// get the collider:
//Collider collider = solver.colliderGroup.colliders[c.other];
// Cut at particle index
CutRopeAtIndex(rope, c.particle);
}
}
}
void CutRopeAtIndex(ObiRope rope, int index)
{
// remove constraints from solver:
rope.DistanceConstraints.RemoveFromSolver(null);
rope.BendingConstraints.RemoveFromSolver(null);
rope.Tear(index); // tear the fifth distance constraint.
// you could call Tear() as many times as you wish here.
// add constraints to solver
rope.BendingConstraints.AddToSolver(rope);
rope.DistanceConstraints.AddToSolver(rope);
// update active bending constraints:
rope.BendingConstraints.SetActiveConstraints();
// only for cloth: commit changes to the topology:
// UpdateDeformableTriangles();
// upload active particle list to solver:
rope.Solver.UpdateActiveParticles();
}
}