Ball to Ball Collision - Detection and Handling

前端 未结 14 1666
逝去的感伤
逝去的感伤 2020-11-22 05:29

With the help of the Stack Overflow community I\'ve written a pretty basic-but fun physics simulator.

\"alt

14条回答
  •  半阙折子戏
    2020-11-22 06:02

    I implemented this code in JavaScript using the HTML Canvas element, and it produced wonderful simulations at 60 frames per second. I started the simulation off with a collection of a dozen balls at random positions and velocities. I found that at higher velocities, a glancing collision between a small ball and a much larger one caused the small ball to appear to STICK to the edge of the larger ball, and moved up to around 90 degrees around the larger ball before separating. (I wonder if anyone else observed this behavior.)

    Some logging of the calculations showed that the Minimum Translation Distance in these cases was not large enough to prevent the same balls from colliding in the very next time step. I did some experimenting and found that I could solve this problem by scaling up the MTD based on the relative velocities:

    dot_velocity = ball_1.velocity.dot(ball_2.velocity);
    mtd_factor = 1. + 0.5 * Math.abs(dot_velocity * Math.sin(collision_angle));
    mtd.multplyScalar(mtd_factor);
    

    I verified that before and after this fix, the total kinetic energy was conserved for every collision. The 0.5 value in the mtd_factor was the approximately the minumum value found to always cause the balls to separate after a collision.

    Although this fix introduces a small amount of error in the exact physics of the system, the tradeoff is that now very fast balls can be simulated in a browser without decreasing the time step size.

提交回复
热议问题