# Thread: Reflecting particle

1. ## Reflecting particle

Hi guys

For my 2 year project I have decided to make a particle simulator and so far so good
But I want the particle to bounce when it hits a side. Now this is a little complex as i need to have a formula that calculates the gradient and direction of the bounced particle.

I hope this makes sense, thanks!

(for example, how does pong get the ball to bounce)

2. Well, change the direction. IE if it hits the right side, it is now travelling in the original y, and the inverse x. You can figure out the other sides using similar logic. So keep some speed, and on update set x += speed * dx and y += speed * dy.

Other then this simpler route you can keep track of the angle of the particle, relative to some axis, and use the trig functions to figure out the new position.

3. Well, change the direction. IE if it hits the right side, it is now travelling in the original y, and the inverse x. You can figure out the other sides using similar logic. So keep some speed, and on update set x += speed * dx and y += speed * dy.
For that you'll need two speed variables speedX and speedY, or perhaps a vector with two components.

But most of the better implementations of pong use something more than that. You could for instance do it using some trig as mentioned above.

If you want to reflect the particle against a plane, then you'll have to use the get the dot product between your vector and the normal and calculate the new direction. Something like -

Code:
`R = V - (2 * N * dot(V, N));`

4. I know this may be asking for a lot but...could you upload an example please please please......
Thanks!

5. Well, for the simplest implementation as I mentioned above it's pretty easy. First you must be able to tell when a particle hits a wall. After that change the direction of movement by taking the inverse of the correct position that you are changing.

You should post the section of code you need help with and ask specific questions. Also there are plenty of google examples in every language of doing a pong implementation.

6. No worries, i figured it out !

Popular pages Recent additions