Two circular objects collide when the distance between their centers is small enough. You can use the following code to check this:
double distanceSquared = pow(ourMidPoint.x - theirMidPoint.x, 2.0) + pow(ourMidPoint.x - theirMidPoint.x, 2.0); bool haveCollided = (distanceSquared <= pow(radius + theirRadius, 2.0));
To check if a collision has occurred between two points in time, you can check for a collision at the beginning of the time interval and at the end of it; however, if objects move very fast, collision detection may fail (I think you ran into this problem for falling objects at the fastest speed at the bottom of the screen).
The following may make collision detection more reliable (although still not ideal). Suppose objects move at a constant speed; then their position is a linear function of time:
our_x(t) = our_x0 + our_vx * t; our_y(t) = our_y0 + our_vy * t; their_x(t) = their_x0 + their_vx * t; their_y(t) = their_y0 + their_vy * t;
Now you can define the (square) distance between them as a quadratic function of time. Find at what time it takes its minimum value (i.e., its derivative is 0); if this time belongs to the current time interval, calculate the minimum value and check it for a collision.
That should be enough to detect collisions almost perfectly; if your application works hard with freely falling objects, you may need to clarify the motion functions that will be quadratic:
our_x(t) = our_x0 + our_v0x * t; our_y(t) = our_y0 + our_v0y * t + g/2 * t^2;
source share