Box collision detection and bouncing

后端 未结 4 1658
一生所求
一生所求 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:12

    A word of wisdom about using jbx's gradient method. If the ball hits near the corners of the box, while dx will be inverted, dy can place the ball above the top boundary.

    0 讨论(0)
  • 2021-01-27 14:12

    Thanks @jbx, I knew there was a simpler way :) However, that doesn't seem to work for the east and west walls and the paddles (if they're on those walls). This seems to work for me on the east and west walls though:

    (180 - (angle + 90)) - 90.
    

    which simplifies to just (180-angle). Hope that helps.

    0 讨论(0)
  • 2021-01-27 14:16

    In this KineticModel, the method collideWalls() uses two-dimensional vector arithmetic to simplify the simulation of an elastic collision between a particle and a flat surface.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题