Collision detection between particles and polygons
A basic example of collision detection in 3D, where moving particles bounce off a static terrain.
A couple things on it which could be of use …
(A) To find out where a moving particle hits a polygon boils down to asking the question, “Where does a ray intersects a plane?” One or two Google searches will give you some math and some code examples… The gist is that the particle plus its velocity vector gives you the “ray” in question; the plane that it does or does not intersect is the one created by the triangle of the polygon you’re testing against.
In this case, with the polygon which makes up the ‘terrain’, the triangles are set at regular intervals on the X/Z plane (in the parlance of Papervision3D, an instance of “Plane3D”), so it was relatively straightforward to find the right triangle to do a “hit test” on. If it’s a more irregularly shaped polygon, you’ll have to iterate thru all the triangles testing for the occurrence of an intersection, or find clever ways of minimizing the number of tests you have to perform on the polygon).
(B) When a collision has been detected, you have to figure out the direction of the bounce. The solution I arrived at comes from asking the question, “How do you rotate a point or vector around an arbitrary axis?” (Again, Google is your friend). In this case, that arbitrary axis is the normal of the triangle that the particle has hit. The velocity vector of the particle is reversed (since, after all, the particle has to “bounce”) and rotated 180 degrees. The result of this is that the angle between the new vector and the triangle normal equals the angle between the original vector and the triangle normal. I’m not sure if this makes sense in terms of real physics but it seems to make intuitive sense to me, and “looks” correct enough… If someone can comment on this, I’d appreciate it.
Source code: Main class file | Full project (Flex Builder 3)
Licensed under a Creative Commons Attribution 3.0 License.

January 6th, 2008 at 6:05 am
Very nice stuff!
We added your website.
January 6th, 2008 at 7:01 am
quite impressive and pretty useful too !
thanks :)
January 6th, 2008 at 11:59 am
REALLY impressive. Very nice. Excellent framerate!! And yes, as far as I can tell from your explanation, the bounce math sounds right to me; abs(input_angle - normal) should equal abs(output_angle - normal).
January 6th, 2008 at 3:36 pm
bookmarked. thanks!
January 6th, 2008 at 8:53 pm
i-think-i-love-u ;D
January 7th, 2008 at 12:09 am
Excellent!
Thanks!
January 7th, 2008 at 12:15 pm
Brilliant!
January 7th, 2008 at 1:10 pm
you are out of this world!
Awesome effect!
February 21st, 2008 at 3:50 am
import com.lee.papervision.LeeCamera3D;
import com.lee.papervision.PlaneHorizontal;
import com.lee.papervision.Util3D;
where i can find those classes?
A.
March 5th, 2008 at 7:24 am
That’s the kind of class that will give birth to many applications. Well done!
June 20th, 2008 at 1:16 pm
FYI the Flex 3 project is easily converted to build in Flashdevelop.
Just create a new AS3 Flashdevelop project file in the “\ParticleCollisionTest\src” folder and add “ParticleCollisionTest\src_pv” to the project classpath.
Be sure to also add an output SWF filename to the project settings.