Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 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.

```//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

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

```//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

2. ## 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?).
```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

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