OK so I want to view a flat plane from above and have a ball rolling around that can bang into walls. I want to make it bounce off the walls realistically.
I know that I need to make the angle of incidence equal the angle of reflection. Well that's all very good in theory but how to actually implement something like that.
For a start I have to work out how to collide a ball and a wall. I can use BMax pixel perfect collision detection on both but that will only tell me that there was a collision, not a which point on the ball.
So maybe I should test a ring of pixels around the rim of the ball, then when one collides I know where it hit the wall (relative to it's centre). I guess though that more than one point could collide if it was moving fast enough and the code had moved the ball partially into the wall. I expect theres some clever code that could avoid this.
Or I could simply move the ball by it's X speed and if I detect a collision I know that I simply have to reverse the X speed. Also the same for Y speed. Are there any problems with this method?
Certainly this would only work for walls that are Horizontal or Vertical. What if the wall is at an angle, I can't just reverse the X or Y, I need to calculate the angle of incidence and reflection. Sure I can do that if I know the ANGLE of the wall. So should I have each wall as an object which has a field for storing its angle? Sounds sensible but what if I just place a rotated block in the playfield? This has 4 walls, each at different angles. I want to draw it as a single block, but from a collision point of 4 it's 4 walls. How would I deal with that?
Any help is very much appreciated, thanks in advance! :-)
I know that I need to make the angle of incidence equal the angle of reflection. Well that's all very good in theory but how to actually implement something like that.
For a start I have to work out how to collide a ball and a wall. I can use BMax pixel perfect collision detection on both but that will only tell me that there was a collision, not a which point on the ball.
So maybe I should test a ring of pixels around the rim of the ball, then when one collides I know where it hit the wall (relative to it's centre). I guess though that more than one point could collide if it was moving fast enough and the code had moved the ball partially into the wall. I expect theres some clever code that could avoid this.
Or I could simply move the ball by it's X speed and if I detect a collision I know that I simply have to reverse the X speed. Also the same for Y speed. Are there any problems with this method?
Certainly this would only work for walls that are Horizontal or Vertical. What if the wall is at an angle, I can't just reverse the X or Y, I need to calculate the angle of incidence and reflection. Sure I can do that if I know the ANGLE of the wall. So should I have each wall as an object which has a field for storing its angle? Sounds sensible but what if I just place a rotated block in the playfield? This has 4 walls, each at different angles. I want to draw it as a single block, but from a collision point of 4 it's 4 walls. How would I deal with that?
Any help is very much appreciated, thanks in advance! :-)