Box collision detection and bouncing

后端 未结 4 1669
一生所求
一生所求 2021-01-27 14:07

I\'m making pong, and am finding it really difficult to write an algorithm that bounces the ball off the four walls properly (I will deal with scoring later on, because only par

4条回答
  •  故里飘歌
    2021-01-27 14:35

    You can look at this in 2 ways:

    Angles: If you know the angle the ball is colliding at, just perform 180 - angle to find the new angle.

    Gradient: Probably simpler. You must be moving the ball at a certain dY and dX every t milliseconds. so if you hit the wall you can simply play with inverting signs of dY and dX. For example if you hit the right wall, dX becomes -dX while dY continues on its course.

提交回复
热议问题