How to fix circle and rectangle overlap in collision response?

后端 未结 3 980
不思量自难忘°
不思量自难忘° 2020-12-13 05:29

Since in the digital world a real collision almost never happens, we will always have a situation where the \"colliding\" circle overlaps the rectangle.

How

3条回答
  •  悲&欢浪女
    2020-12-13 05:45

    Finding the moment of contact isn't too hard:

    You need the position of the circle and rectangle at the timestep before the collision (B) and the timestep after (A). Calculate the distance from the center of the circle to the line of the rectangle it collides with at times A and B (ie, a common formula for a distance from a point to a line), and then the time of collision is:

    tC = dt*(dB-R)/(dA+dB),
    

    where tC is the time of collision, dt is the timestep, dB is the distance to line before the collision, dA is the distance after the collision, and R is the radius of the circle.

    This assumes everything is locally linear, that is, that your timesteps are reasonably small, and so that the velocity, etc, don't change much in the timestep where you calculate the collision. This is, after all, the point of timesteps: in that with a small enough timestep, non-linear problems are locally linear. In the equation above I take advantage of that: dB-R is the distance from the circle to the line, and dA+dB is the total distance moved, so this question just equates the distance ratio to the time ratio assuming everything is approximately linear within the timestep. (Of course, at the moment of collision the linear approximation isn't its best, but to find the moment of collision, the question is whether it's linear within a timestep up to to moment of collision.)

提交回复
热议问题