Search code examples
geometrycollision-detectionphysicscollision

2D moving object collision


I 'm creating a two 2D simulation and I need to determine if 2 moving objects A and B will cross paths . A moves with a constant speed Va and B with Vb.

I'm able to determine the point where the the object's path intersect but I can't figure out if will they actually collide.

I calculated the point of collision using

This formula

and the same for y


Solution

  • Let's consider case of two axis-aligned rectangles. They do intersect, if projections of both to X-axis intersect, and projections of both to Y-axis intersect. enter image description here

    First rectangle coordinates (Ax1,Ay1),(Ax2,Ay2), velocity vector (VAx,VAy)
    Second rectangle coordinates (Bx1,By1),(Bx2,By2), velocity vector (VBx,VBy)

    Time interval when X-projections intersect:

    Ax2+VAx*t1=Bx1+VBx*t1
    t1=(Bx1-Ax2)/(VAx-VBx)
    t2=(Bx2-Ax1)/(VAx-VBx)
    

    Interval is Ix=(t1,t2) (or (t2,t1) if t2 < t1)

    For Y-projections

    u1=(By1-Ay2)/(VAy-VBy)
    u2=(By2-Ay1)/(VAy-VBy)
    

    Interval is Iy=(u1,u2) (or (u2,u1) if u2 < u1)

    Check if these two time ranges Ix and Iy intersect. If they do, objects collide.