Bouncy bouncy


Now that you've got falling particles, you will probably be disappointed by the fact that that quickly fall off the bottom of the screen. Infinitely falling particles are of little use to anyone. The least they should do is to bounce when they hit the floor. I will first explain how to make a particle bounce off the floor, then how to bounce off and arbitary surface.


Bouncing off the floor

Assuming the floor is a level plane at some Y height, the particles should simply change Y direction. So a small amendment is needed to the pseudo code:
loop
  for each particle
    erase particle
    position = position + velocity
    velocity = velocity + acceleration
    if position is below floor then velocity = (xv, -yv, zv)
    draw particle
  end of particle loop
end of main loop
Now, if you were to take a ball and drop it on the floor, you would notice that the ball does not bounce back up to the same height you dropped it from. This is because the ball looses energy when it hits the floor, and so the velocity it bounces with is less than that of the impact. Again, this is quite easy to take into account in the algorithm.

First you must decide how bouncy the ball is. The bouncyness can be represented by a number between 0 and 1. 0 being not at all bouncy, and 1 being perfectly bouncy. Call this value e.

loop
  for each particle
    erase particle
    position = position + velocity
    velocity = velocity + acceleration
    if position is below floor then velocity = (xv*e, -yv*e, zv*e)
    draw particle
  end of particle loop
end of main loop
Some approximate values for e:
0.0 - mud
0.1 - squash ball
0.5 - football
0.8 - bouncy ball
0.9 - snooker balls
1.0 - atoms


Bouncing off any surface

It is not too hard to get particles to bounce off any surface, as long as you know the surface normal. The Normal is a vector perpendicular to the surface at the point the particle hit. If the surface is flat, then the normal will be the same anywhere on the surface. If the surface is curved, a sphere for example, then you will have to calculate a normal for the point of impact.
When a particle with velocity v hits a surface with normal vector n, the particle bounces with a vector of r given by:
r = 2 * n * CosVect(n, v) - v

Where CosVect returns the cosine of the angle between two vectors, otherwise known as the Dot Product.


Click here do download a demonstration of bouncing dots