Box collision detection and bouncing

时间秒杀一切 提交于 2019-12-02 10:03:10

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.

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.

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.

jt78

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!