I'm in the process of writing a 2D physics engine in Java, and am trying to figure out the algorithm for the most basic of all physics tasks: Object collision.

Detecting collision between two circles is easy: check if the distance between their centers is less than or equal to the sum of their radii.

However, my problem is detecting collision with a rectangle. The most efficient way I can think of to do it is to check at the midpoints of each line segment and at the corners, but this obviously leaves out the pieces in between, which would cause frequent clipping errors. Given a second object to collide with and the dimensions and locations of both objects, how could I reliably detect if a rectangle collided with another object?

My current idea is this:
Use trigonometry and some point rotation to check if each individual line in the rectangle intersects the other object and is close enough to actually touch the segment. This, however, strikes me as inefficient due to the many equations needed to run the intersection between a 1D and 2D object being done four times over for every collision check.

A final question I have is this: Is it possible to use event-based programming to send an event triggered by something in the program? That is, the event isn't sent by the click of a mouse or the press of a key but by some other action, like two objects colliding.

7 Years
Discussion Span
Last Post by Cheesy74
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.