I am making a simulation that involves particles dynamically added to the stage (lots of circles). When these circles collide (which are actually movieclips) they need to rebound (momentum switched). I have no problem with the physics but do not know how to efficiently check for collisions. It boils down to being able to 1) Check if a particle is colliding and 2) Getting which other particle (object) is involved in the collision?
I would ideally like particles to be able to check for their own collisions and adjust their own momentum, not just some meaty Render() function running on every frame. How could this be done?
Thanks in advance.
For efficient collision detection, you will want to use something called a binary tree. Essentially, instead of checking every single pixel of every single circle against every single pixel of every single circle, you first check if it is even reasonable that two circles are touching. This is done by repeatedly dividing the map into quarters. If two circles aren’t even in the same quadrant of the screen, they won’t be touching.
Implementation, however, is difficult, and unless you’re purposefully giving yourself a challenge, use a library so you don’t reinvent the wheel. You can use Box2D, a physics engine, or Flixel, a game engine.