05-09-2022, 02:10 PM
(05-09-2022, 02:06 PM)josemendez Wrote: That code was written for Obi 3.5, idToParticle no longer exists. You get the collider like you were already doing in your original code:
Code:ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
However the idea of the code is still perfectly valid: use a hash set to determine which particles have already been counted.
I've modified your code to work properly:
Code:[RequireComponent(typeof(ObiSolver))]
public class CollisionCounter : MonoBehaviour {
ObiSolver solver;
[SerializeField] int counter = 0;
HashSet<int> particles = new HashSet<int>();
void Awake(){
solver = GetComponent<Obi.ObiSolver>();
}
void OnEnable () {
solver.OnCollision += Solver_OnCollision;
}
void OnDisable(){
solver.OnCollision -= Solver_OnCollision;
}
void Solver_OnCollision (object sender, Obi.ObiSolver.ObiCollisionEventArgs e)
{
HashSet<int> currentParticles = new HashSet<int>();
var world = ObiColliderWorld.GetInstance();
// just iterate over all contacts in the current frame:
foreach (Oni.Contact contact in e.contacts)
{
// if this one is an actual collision:
if (contact.distance < 0.01)
{
ObiColliderBase col = world.colliderHandles[contact.bodyB].owner;
if (col != null && col.gameObject.layer == 7)
{
currentParticles.Add(contact.bodyA);
}
}
}
particles.ExceptWith(currentParticles);
counter += particles.Count;
particles = currentParticles;
}
}
works perfectly, thank you very much