Having problems calculating "bounce-angles"
BlitzMax Forums/BlitzMax Programming/Having problems calculating "bounce-angles"
| ||
This has me stumped so I'm hoping someone can help me out. I need to be able to calculate the new angle of a bullet or a ball when it hits a wall. Imagine Arkanoid (although that's not what I'm making). Then imagine a ball moving down and right towards a wall at 135 degrees - like this \. Essentially both xspeed and yspeed would be 1 in this simple example. The only way I can think of is the "normal" (I think) way of changing the direction of the ball - ie. When it hits that right wall I'd make xspeed=-xspeed to change the direction. The thing is - I'm not interrested in changing the xspeed and yspeed variables directly like this. I'd like some clever way (if there is one) to calculate my new angle, so in this case the angle would change from 135 to 225, and then calculate the speeds depending on the angle. Can anyone help me out? |
| ||
Hi Sokurah well it depends on how do you keep your ball information. There is two different way: - The 'old-school' cartesian way: like you describe, your ball information is Position X,Y and Speed X,Y. Looks simple but much more troublesome. - The trigonometric way: you still have position X,Y but your direction is an angle and speed. The way I would suggest would be storing an angle and speed. To move the ball, you do something like this: ballx :+ cos(angle) * speed bally :+ sin(angle) * speed * Note: you might have to multiply the speed by a delta time if you need to be framerate independant, however this would be a separate discussion. You need to detect if the ball hits a vertical or horizontal wall because the resulting collision is not the same angle. See this small example: Graphics 512,512 ballx# = 256 bally# = 256 speed# = 6 angle# = 30 While Not KeyHit(27) Cls ballx :+ Cos(angle) * speed bally :+ Sin(angle) * speed If bally <=0 Or bally >= 512 Then angle = -angle EndIf If ballx <=0 Or ballx >= 512 Then angle = 180-angle EndIf DrawRect ballx-4, bally-4, 8,8 Flip Wend |
| ||
Hey remz, Thanks for your answer. I've been sick as a dog for over a week now - that's why I haven't replied. Thanks again. I'll look into your suggestions now. |
| ||
This help me too. Thanks! |
| ||
It gets a lot harder if the WALL is at a non 90 degree angle. I solved that but couldn't properly deal with a ball hitting a corner. |
| ||
isn't the angle of incident the same as the angle of reflection when measured from the normal? |
| ||
Yes but it's a bit stinky to work out when the wall is at an angle like I said. |