# 2D Collision Detection

• April 2nd, 2010, 10:19 PM
Cuju
2D Collision Detection
I'm trying to create hit boxes on a platform and an oval(I've accomplished this). But my method of doing this is checking if any of the 16 hit boxes on the oval touch any of the 12 hit boxes on the platform. There's so many different combinations and I don't think printing out every combo is the most efficient or easiest way. I know I should use arrays but I don't quite know how to do it.

Code :

```//Oval g.drawOval(x,y,40,40); //Collision boxes for oval //Square 1 g.drawRect(x,y,10,10); g.drawRect(x+10,y,10,10); g.drawRect(x+10,y+10,10,10); g.drawRect(x,y+10,10,10); //Square 2 g.drawRect(x+20,y,10,10); g.drawRect(x+30,y,10,10); g.drawRect(x+20,y+10,10,10); g.drawRect(x+30,y+10,10,10); //Square 3 g.drawRect(x,y+20,10,10); g.drawRect(x+10,y+20,10,10); g.drawRect(x,y+30,10,10); g.drawRect(x+10,y+30,10,10); //Sqaure 4 g.drawRect(x+20,y+20,10,10); g.drawRect(x+30,y+20,10,10); g.drawRect(x+20,y+30,10,10); g.drawRect(x+30,y+30,10,10);   //Platform g.drawRect(px,400,60,20); //Collision boxes for platform //P 1 g.drawRect(px,400,10,10); g.drawRect(px+10,400,10,10); g.drawRect(px,410,10,10); g.drawRect(px+10,410,10,10); //P 2 g.drawRect(px+20,400,10,10); g.drawRect(px+30,400,10,10); g.drawRect(px+20,410,10,10); g.drawRect(px+30,410,10,10); //P 3 g.drawRect(px+40,400,10,10); g.drawRect(px+50,400,10,10); g.drawRect(px+40,410,10,10); g.drawRect(px+50,410,10,10);```

I called each set of mini 4 squares one big square, don't ask why, it just clicks in my brain that why :P

Here's a sample of how I'm determining my collisions which would take forever to check every one if I continue this way!

Code :

```//Collision detection //Square 1 and P 1 if(x==px&&y==400) { alive=true; } if(x+10==px&&y==400) { alive=true; } if(x+10==px&&y+10==400) { alive=true; } if(x==px&&y+10==400) { alive=true; }```

And I know I don't have to draw the actual hit boxes out, but it's a good visual representation
• April 3rd, 2010, 10:39 AM
helloworld922
Re: 2D Collision Detection
Find a mathematical representation that allows you to draw the rectangles using a loop. This will greatly reduce the amount of lines of code you have to write. It doesn't matter terribly which order the boxes are drawn, so you can manipulate that to come up with a shorter solution. Here's a list of the draw commands listed by increasing x-coordinates, then increasing y-coordinates (can you spot the simple pattern?).
Code :

```g.drawRect(x,y,10,10); g.drawRect(x,y+10,10,10); g.drawRect(x,y+20,10,10); g.drawRect(x,y+30,10,10);   g.drawRect(x+10,y,10,10); g.drawRect(x+10,y+10,10,10); g.drawRect(x+10,y+20,10,10); g.drawRect(x+10,y+30,10,10);   g.drawRect(x+20,y,10,10); g.drawRect(x+20,y+10,10,10); g.drawRect(x+20,y+20,10,10); g.drawRect(x+20,y+30,10,10);   g.drawRect(x+30,y,10,10); g.drawRect(x+30,y+10,10,10); g.drawRect(x+30,y+20,10,10); g.drawRect(x+30,y+30,10,10);```

A great way to simplify your life is to "abstract" away the details, i.e. helper methods. A good helper method here would be to check to see if a point is inside of a rectangle. Of course, you will have to write the helper method at some point, but that will only need to be written once :P

Here's a great article on getting 2D collision detection (focused primarily on games): Game dev: Collision Detection