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.
Answer
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.
No comments:
Post a Comment