Collision of Two Circles

后端 未结 3 2045
失恋的感觉
失恋的感觉 2021-01-17 05:57

Hey i am writing a program in java (but i think thats not that relevant for this question), where two robots (circles) are driving arround. A robot is driving with a certain

3条回答
  •  -上瘾入骨i
    2021-01-17 06:15

    Let's P1 = (x1, y1) and P2 = (x2, y2) are starting coordinates, V1 = (vx1, vy1) and V2 = (vx2, vy2) are velocities, R1 and R2 are circle radia. Circles collide, if center-center distance is smaller than R1 + R2 (or squared distance is smaller then RR=(R1+R2)^2)

    We can find coordinates of centers, function of distance versus time, and determine whether distance ever becomes small enough.

    Simple approach - using Galileo's principle, working in coordinate system, linked with the first object. In that system it stays in the zero point, and the second object is moving with starting point (x2-x1, y2-y1) and velocity (vx2-vx1, vy2-vy1). Coordinates of the second object through the time:

    X = (x2-x1) + (vx2-vx1) * t = dx + vx * t
    Y = (y2-y1) + (vy2-vy1) * t = dy + vy * t
    

    Difference between squared distance and RR is zero when collision occurs

    D^2 - RR = X*X + Y*Y - RR = 
    dx^2 + 2*dx*vx * t + vx^2 * t^2 + dy^2 + 2*dy*vy * t + vy^2 * t^2 - RR = 
    (vx^2+vy^2) * t^2 + 2*(dx*vx+dy*vy) * t + (dx^2+dy^2-RR) = 0
    

    Solve last quadratic equation against t. If proper (positive, the smallest positive) root exists, then collision occurs in this moment.

提交回复
热议问题