Search Unity

Question How to check if point is inside particle system (2D)?

Discussion in 'General Graphics' started by JiriAtanasovsky, Jul 29, 2021.

  1. JiriAtanasovsky

    JiriAtanasovsky

    Joined:
    Jan 18, 2021
    Posts:
    9
    Hello, I would someone have a suggestion how to check if a point in world coordinates is inside particle system?

    F.e. if I have particle system like on picture I would like to check if a point is inside or outside of orange area.

    upload_2021-7-29_14-55-57.png

    Basically, I have a precalculated bullet trajectory (array of Vector2 points) and I want to check if each point is inside of this "smoke" effect or not...

    How would you approach this?
    Thank you for any suggestion.
     
  2. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Probably I’d check the renderer.bounds first, to reject bullet points quickly, but if the point is inside the bounds, you’d need to check how close it is to each particle via GetParticles (or a burst compiled C# job would be much faster)

    Depending on the number of tests you need to do, it may be faster to build a 3D Voxel grid of the smoke and then you can very quickly determine if the Voxel the bullet point is inside is “occupied” by the smoke or not.
     
  3. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,285
    Another very different idea - if you can make a box collider from your bullet trajectory, the particle trigger module could give you a script callback if the collider touches the smoke.
     
  4. JiriAtanasovsky

    JiriAtanasovsky

    Joined:
    Jan 18, 2021
    Posts:
    9
    Thank you for these suggestions. It set me on right track.

    What I did is that I added PolygonCollider2D to the smoke. I have it updating its shape based on position of particles (most left one, right one etc). Now I can just raycast for this "smoke collider".
     
    richardkettlewell likes this.