05-09-2022, 02:06 PM
(This post was last modified: 05-09-2022, 02:09 PM by josemendez.)
(05-09-2022, 01:58 PM)0hsyn1 Wrote: Yes, I found the post you shared while doing research.
"idToCollider" "particle" syntaxes appear as console errors.
That code was written for Obi 3.5, idToParticle no longer exists. In >3.5 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;
}
}