Obi Official Forum
How to detect number of particles in collision? - Printable Version

+- Obi Official Forum (https://obi.virtualmethodstudio.com/forum)
+-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html)
+--- Forum: Obi Fluid (https://obi.virtualmethodstudio.com/forum/forum-3.html)
+--- Thread: How to detect number of particles in collision? (/thread-2369.html)



How to detect number of particles in collision? - anulagarwal - 15-07-2020

Hi,

So how do i detect the number of particles currently in contact with a plane/collider? For example, If i my blueprint has 2000 particles, And I make a few fall off the edge, How do i detect the current remaining particles?


Thanks


RE: How to detect number of particles in collision? - josemendez - 16-07-2020

(15-07-2020, 06:50 PM)anulagarwal Wrote: Hi,

So how do i detect the number of particles currently in contact with a plane/collider? For example, If i my blueprint has 2000 particles, And I make a few fall off the edge, How do i detect the current remaining particles?


Thanks

Hi,

Obi simply provides the full contact list each frame, it is your responsibility to filter it any way you need. See:
http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html

In your case, you can simply keep track of the previous contact list, and use list operations (such as union, intersect, except) to determine which contacts have disappeared since the last frame and which ones still exist.


RE: How to detect number of particles in collision? - anulagarwal - 16-07-2020

Iterating through them via foreach every frame causes the game to lag!
Any other solution?

Edit: Figured it out, Thanks!


RE: How to detect number of particles in collision? - anulagarwal - 16-07-2020

Hey,
The number seems to be fluctuating tho.
I cannot get the exact number of particles in contact with the collision, they seem to go up and down every frame.


RE: How to detect number of particles in collision? - josemendez - 16-07-2020

(16-07-2020, 09:59 AM)anulagarwal Wrote: Hey,
The number seems to be fluctuating tho.
I cannot get the exact number of particles in contact with the collision, they seem to go up and down every frame.

Hi,

The amount of contacts should always be correct, as the data structure passed to the collision event is the exact same used by the engine to resolve collisions. Missing/excess collisions would result in incorrect physical behavior.

How are you counting the amount of particles in contact?


RE: How to detect number of particles in collision? - anulagarwal - 16-07-2020

Code:
  ObiSolver solver;
    bool isHit;

    Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;

    List<Oni.Contact> c;
    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)
    {
       
       
        GameManager.instance.SetCompletion(e.contacts.Count);
       
    }
Basically this is attached on the Solver object, and the e.contacts.Count keeps fluctuating for some reason


RE: How to detect number of particles in collision? - josemendez - 17-07-2020

(16-07-2020, 05:37 PM)anulagarwal Wrote:
Code:
  ObiSolver solver;
    bool isHit;

    Obi.ObiSolver.ObiCollisionEventArgs collisionEvent;

    List<Oni.Contact> c;
    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)
    {
      
       
        GameManager.instance.SetCompletion(e.contacts.Count);
      
    }
Basically this is attached on the Solver object, and the e.contacts.Count keeps fluctuating for some reason

contacts.Count will of course fluctuate over multiple frames, as some frames more particles will be in contact that in others depending on the situation. Not all particles will be in contact with all colliders all the time. You need to check for actual contacts by the way, not just speculative ones. For this, you need to make sure the contact distance is negative (or smaller than a small positive value). Check the sample in the docs:
http://obi.virtualmethodstudio.com/tutorials/scriptingcollisions.html


RE: How to detect number of particles in collision? - anulagarwal - 23-07-2020

Hey,
I still cannot figure it out! Triste
Is it possible you could write a code snippet and explain me how it would work?