1

I want to implement the following scenario in openGL c++. Suppose there is a ball moving around a surface and there is a boundary line in all the four direction. when the ball hits one of the lines, it reflects to a direction. My question is is there any way to find out or calculate mathematically in which direction, the ball will go after hitting the wall?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Sure, you're looking for `vector reflection`. Have a look at these couple of links: http://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector and http://www.3dkingdoms.com/weekly/weekly.php?a=2 - note that this approach allows you to bounce a ball off a wall in any orientation - not just the simple case of the axis aligned walls of a box. – enhzflep Sep 25 '16 at 03:49

2 Answers2

2

For a perfect ball with no deformation and an isentropic contact, the ball would reflect with the same angle it went in, but 90 degrees passed it. So if your ball comes in at 30 degrees from the wall, it would come off at 30 degrees from the wall, but on the other "side" of the normal to that wall. Ball Reflecting

Cedric
  • 889
  • 1
  • 8
  • 18
1

If you do not want to change the velocity but only the directory then it is easy to calculate. You only need to toggle direction of horizontal or vertical velocity based on whether it hit vertical or horizontal boundary respectively.

Let's say your horizontal and vertical velocity are vx, vy respectively. If the ball is hit on left or right boundary then vx = -vx and if the ball is hit on top or bottom boundary then vy = -vy. This will work regardless of the direction of current velocity, i.e. it does not matter whether the ball is moving left/right, up/down now. After the collision it will just toggle the proper direction.

taskinoor
  • 45,586
  • 12
  • 116
  • 142