# Collision Detection Between Two Squares

Printable View

• August 22nd, 2012, 12:15 AM
thegreatzo
Collision Detection Between Two Squares
Heya,

I have a program with predefined objects(squares) playerObject & stillObject; that have methods to get their x,y coordinates called getX() & getY().
playerObject: moves with the arrow keys of the keyboard via an already provided game engine.
stillObject: is set in a location on the screen.
Both objects have measurements of 100x100.

My problem is I'm having trouble defining a collision detection statement, which needs to account for the stillObject changing positions in debugging tests, so it can't be hard-coded.
So far what I have covers collisions between the top and left sides of the stillObject against playerObject by minusing the distance between them and if it's bigger than 0 than the objects have collided and the same for -200, which should be the other side of stillObject(which is what I thought), but it doesn't seem to be working that way for me.

Code :

```if ((((stillObject.getX() + 100) - playerObject.getX()) > 0) && ((stillObject.getY() + 100) - playerObject.getY()) > 0) { collide = true; }   if ((((stillObject.getX() - 100) + playerObject.getX()) < -200) && ((stillObject.getY() - 100) + playerObject.getY()) < -200) { collide = true; }```

Any push in the right direction would be great, thank you.
• August 22nd, 2012, 07:18 AM
Norm
Re: Collision Detection Between Two Squares
The way I debug these kinds of problems is by taking a piece of paper and drawing the shapes all the different ways that there can be a collision and then looking at the x,y & width,height of both shapes at each point of collision to come up with the tests needed to detect it.

The Rectangle class has methods that could be used.

Note on the posted code: You should NOT hard code the values 100 and 200 in the tests. Use variables that can be changed and whose names define what their values are.
• August 22nd, 2012, 07:59 AM
thegreatzo
Re: Collision Detection Between Two Squares
Quote:

Originally Posted by Norm
The way I debug these kinds of problems is by taking a piece of paper and drawing the shapes all the different ways that there can be a collision and then looking at the x,y & width,height of both shapes at each point of collision to come up with the tests needed to detect it.

The Rectangle class has methods that could be used.

Note on the posted code: You should NOT hard code the values 100 and 200 in the tests. Use variables that can be changed and whose names define what their values are.

I tried drawing it out, and this is the best thing I could come up with. I thought using those values wouldn't count as hard coding since I'm calculating them along with the current x,y values( getX() & getY() ), so even if the coordinates of the stillObject changes, it should still work. The measurements of the squares aren't going to change, just their location so in theory these values would still work, right?

The objects are drawn using a custom method, and it's not actually clear how they're drawn so I'm not sure whether it's drawn from the x,y being the top left corner like usual, or from the middle.
• August 22nd, 2012, 08:18 AM
Norm
Re: Collision Detection Between Two Squares
Quote:

measurements of the squares aren't going to change,
In general its better to use variables. There might be a time when they will be changed.
Using a named variable also makes the code more understandable.

Quote:

I'm not sure whether it's drawn from the x,y being the top left corner like usual, or from the middle.
You will have to know which way is used so you know where the shapes are to be able to detect a collision.
• August 22nd, 2012, 08:48 AM
thegreatzo
Re: Collision Detection Between Two Squares
Thanks Norm, I'll find out how the rectangles are drawn then I'll try reworking my code
• August 22nd, 2012, 08:53 AM
Darryl.Burke
Re: Collision Detection Between Two Squares
This thread has been cross posted here:

Although cross posting is allowed, for everyone's benefit, please read:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• August 22nd, 2012, 09:04 AM
thegreatzo
Re: Collision Detection Between Two Squares
Thanks for that, I didn't know there were cross posting rules. Usually I get arrogant posts just telling me I crossed posted and not mentioning any rules. I'll post up a link next time.
• August 22nd, 2012, 09:13 AM
Norm
Re: Collision Detection Between Two Squares
This is a confusing way to compare locations:
(stillObject.getX() - 100) + playerObject.getX()) < -200)
what is the code supposed to be doing?