问题
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 speed from a certain location to a certain location. Problem is how to determine if the circles having a collision. I cant use the midPoint of the circle and the vector its moving, cause the circle have a radius. Another problem is I just can't check the final locations of the circles. I need to check if the collide on the way and if it is in the same time. Does anyone have an idea how to calculate that?
回答1:
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.
回答2:
I can think of a couple approaches:
- 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.
- 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.
回答3:
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.
来源:https://stackoverflow.com/questions/34694101/collision-of-two-circles