Collision of circular objects

I am going to develop the game in my pocket. I have a problem with a collision of two parts. How to find the collision point of two parts. And then, how to find the angle and distance, moving the pieces after the collision. I found a collision point solution in a circle-circle collision . here the solution is described by trigonometry, but I want a solution with vector math. With the help of which the problem of the distance traveled after the collision will also be easily solved.

0
source share
1 answer

You do not need to look for a collision point for the collision calculation itself. To detect a collision event, you just need to compare the distance from the centers, going over the sum of the radii

sqrt(sqr(x2-x1)+sqr(y2-y1))<=r1+r2

or

dx*dx+dy*dy <= (r1+r2)*(r1+r2)

where (x1,y1)and (x2,y2)are the positions of the disks 1 (with mass m1, radius r1and speed (vx1,vy1)) and 2. The differences are always 2 minus 1, dx=x2-x1etc.


You will almost never realize that a collision occurs at a point in time sampling time. With the above formula, the circles are already overlapping. Depending on the time step and overall speeds, this may be negligible or may result in a serious shootout. The following simple calculations assume slow motion, i.e. Very little overlap in the last step.


, , - . , , ,

dx*dvx+dy*dvy < 0

dvx=vx2-vx1 ..

, ( . fooobar.com/questions/1627358/...)

dx = x2-x1; dy = y2-y1;
dist2 = dx*dx + dy*dy;
R = r1+r2;

if ( dist2 <= R*R )
{
    dvx=vx2-vx1; dvy=vy2-vy1;
    dot = dx*dvx + dy*dvy;

    if ( dot < 0 )
    {
        factor = 2/(m1+m2)*dot/dist2;
        vx1 += m2*factor*dx;
        vy1 += m2*factor*dy;
        vx2 -= m1*factor*dx;
        vy2 -= m1*factor*dy;
    }
}
+1

Source: https://habr.com/ru/post/1627360/


All Articles