# Collision Detection difficulties

• May 20th, 2011, 12:00 AM
Uritomi
Collision Detection difficulties
I am attempting to write a 2D game with collision detection for two moving objects (one player controlled and the other in one direction) using Rectangle2D's intersects() method. When the non-player controlled piece is not moving the method works fine, however when I implement the move() method the intersects() method seems to fail. Is there a known problem with this method and 2 moving objects?

I am trying not to post the code because it is recycled from a tetris game I made in my first Java class (it is really, really messy >.<)

If you need more info let me know, any help would be greatly appreciated,
• May 20th, 2011, 06:59 AM
Norm
Re: Collision Detection difficulties
Can you make a small program that executes and demonstrates the problem?
• May 20th, 2011, 08:01 AM
helloworld922
Re: Collision Detection difficulties
There should not be a problem with the intersects method. Likely, this is what I think is happening:

Computers usually work with discrete time steps. This means that between frames, one (or both) of the rectangles will "magically" jump locations to it's new location. If that location happens to completely jump over the object you're checking collision detection with, then no collision will be registered. There are two ways to fix this:

1. Calculate the area between the two discrete locations and check those areas for intersections. This can be done by taking a cross section based off of the current direction of travel, then projecting that line towards the new direction and orientation. Simply connect the two lines into a quadrilateral, and then check collisions of the quadrilaterals (unfortunately, that means you'll have to write your own collision detection code).
2. Calculate an estimated time to collision based off of the current velocities. If the time is less than one frame, then you know on the next frame a collision has happened (assuming that current velocities don't change until after the discrete time step). This is basically trying to operate on a continuous timeline, though there will still be some discrete behaviors present.
• May 20th, 2011, 10:10 AM
Uritomi
Re: Collision Detection difficulties
Ugh I found my mistake... I was setting the location using a modulated number, but in my intersects() method I forgot to modulate it. As a result, whenever the objects intersected, it was comparing with numbers way out of the playing field.

I think I ran into the discrete time steps problem when making tetris(and never finished) so ill have to try those solutions. The first one seems too complicated tho haha.

Thanks again for the help!