Ball to Ball Collision - Detection and Handling

前端 未结 14 1419
逝去的感伤
逝去的感伤 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 05:38

    This KineticModel is an implementation of the cited approach in Java.

    0 讨论(0)
  • 2020-11-22 05:42

    I see it hinted here and there, but you could also do a faster calculation first, like, compare the bounding boxes for overlap, and THEN do a radius-based overlap if that first test passes.

    The addition/difference math is much faster for a bounding box than all the trig for the radius, and most times, the bounding box test will dismiss the possibility of a collision. But if you then re-test with trig, you're getting the accurate results that you're seeking.

    Yes, it's two tests, but it will be faster overall.

    0 讨论(0)
  • 2020-11-22 05:42

    Improving the solution to detect circle with circle collision detection given within the question:

    float dx = circle1.x - circle2.x,
          dy = circle1.y - circle2.y,
           r = circle1.r + circle2.r;
    return (dx * dx + dy * dy <= r * r);
    

    It avoids the unnecessary "if with two returns" and the use of more variables than necessary.

    0 讨论(0)
  • 2020-11-22 05:42

    After some trial and error, I used this document's method for 2D collisions : https://www.vobarian.com/collisions/2dcollisions2.pdf (that OP linked to)

    I applied this within a JavaScript program using p5js, and it works perfectly. I had previously attempted to use trigonometrical equations and while they do work for specific collisions, I could not find one that worked for every collision no matter the angle at the which it happened.

    The method explained in this document uses no trigonometrical functions whatsoever, it's just plain vector operations, I recommend this to anyone trying to implement ball to ball collision, trigonometrical functions in my experience are hard to generalize. I asked a Physicist at my university to show me how to do it and he told me not to bother with trigonometrical functions and showed me a method that is analogous to the one linked in the document.

    NB : My masses are all equal, but this can be generalised to different masses using the equations presented in the document.

    Here's my code for calculating the resulting speed vectors after collision :

    class TBall {
        constructor(x, y, vx, vy) {
            this.r = [x, y];
            this.v = [0, 0];
        }
    }
    
    function collision(ball1, ball2) {
        n = [ (ball1.r)[0] - (ball2.r)[0], (ball1.r)[1] - (ball2.r)[1] ];
        un = [n[0] /  vecNorm(n), n[1] / vecNorm(n) ] ;
        ut = [ -un[1], un[0] ];   
        v1n = dotProd(un, (ball1.v));
        v1t = dotProd(ut, (ball1.v) );
        v2n = dotProd(un, (ball2.v) );
        v2t = dotProd(ut, (ball2.v) );
        v1t_p = v1t; v2t_p = v2t;
        v1n_p = v2n; v2n_p = v1n;
        v1n_pvec = [v1n_p * un[0], v1n_p * un[1] ]; 
        v1t_pvec = [v1t_p * ut[0], v1t_p * ut[1] ]; 
        v2n_pvec = [v2n_p * un[0], v2n_p * un[1] ]; 
        v2t_pvec = [v2t_p * ut[0], v2t_p * ut[1] ];
        ball1.v = vecSum(v1n_pvec, v1t_pvec); ball2.v = vecSum(v2n_pvec, v2t_pvec);
    }

    0 讨论(0)
  • 2020-11-22 05:43

    Well, years ago I made the program like you presented here.
    There is one hidden problem (or many, depends on point of view):

    • If the speed of the ball is too high, you can miss the collision.

    And also, almost in 100% cases your new speeds will be wrong. Well, not speeds, but positions. You have to calculate new speeds precisely in the correct place. Otherwise you just shift balls on some small "error" amount, which is available from the previous discrete step.

    The solution is obvious: you have to split the timestep so, that first you shift to correct place, then collide, then shift for the rest of the time you have.

    0 讨论(0)
  • 2020-11-22 05:47

    A good way of reducing the number of collision checks is to split the screen into different sections. You then only compare each ball to the balls in the same section.

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