# How to calculate the direction a ball must bounce

I have a tile engine I have made. I can detect collision between the ball(the player) and the tiles. When a collision is detected all the colliding tiles are stored inside a list. My question now is, how do I figure how should I change the velocity of the ball using the collided tiles? I tried to figure out a way to calculate the direction the ball is hitting but I am lost.

• Even just I have explained this one a few times before. Although this is a pointsauce goldmine, please search before you ask in the future. – Anko Apr 23 '12 at 14:46
• Possible dupe: gamedev.stackexchange.com/questions/4253/… ? – Tetrad Apr 23 '12 at 15:25
• I did look it up, guess I was just using the wrong key terms – Ziamor Apr 26 '12 at 16:02

## 2 Answers

The ball's new velocity will be it's incoming velocity reflected about the collision normal. Assuming you already have the normal, n, you can reflect the vector v about n using the formula:

reflect = -v + 2*(v*n)*n


Where v*n is the dot product.

To further get into the pseudo-physics, you might want to add in some damping to the collision response in order to simulate energy lost to heat and sound:

reflect *= 0.8


This value, 0.8, is often referred to as the coefficient of restitution, in case you want to do more research. It can really be anything you want, and is more representative of how bouncy the ball in question is.

A final heads up: things will get more complicated if your ball has angular velocity.

If you already have some basic physics knowledge, you just need the normal of the surface collided by the ball and the original speed.

If S is the original speed, N is the normal and Aplha is the angle of those 2 vectors, then if Cos(Alpha) => 0 the ball vector is moving away from the surface and you should let it go.

Otherwise, adding a speed of S*-Cos(Alpha) to the ball in a direction parallel with the surface's Normal will get you a simple rigid bounceback effect.

If it's parallel (Cos == zero) it's your choice to apply any resistance, same goes for any coefficient due to the tiles being elastic or whatever effect you want to add.

Edit: If the ball is colliding with more than 1 surface, just calculate the resulting collision force for each surface, combine them together and you'll get the resulting force.