Collision of Two Circles

后端 未结 3 2046
失恋的感觉
失恋的感觉 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条回答
  • 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.

    0 讨论(0)
  • 2021-01-17 06:22

    I can think of a couple approaches:

    1. Simulate the motion - "move" the robots ahead for a distance computed from their speed and the simulation time interval (short), then determine if they're close enough to collide. Repeat until they collide or reach their destinations.
    2. Instead of using a single line representing the midpoint of the robot/circle, you could use two lines representing the edges of the robot.

    Best of luck.

    0 讨论(0)
  • 2021-01-17 06:32

    There is a simple way of checking if the circles are colliding; if the distance between both circles is smaller than the sum of the radius of both, then it means they are colliding.

    To check if the collision is incoming, check if the distance between both circles is smaller than the sum of the radius of both AND a small value, and if in the next tick this value is even smaller.

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