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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: 2D Collision Detection

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Location
    Canada
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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
    Last edited by Cuju; April 2nd, 2010 at 11:27 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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
    Last edited by helloworld922; April 3rd, 2010 at 11:55 AM.

Similar Threads

  1. Canny Edge Detection
    By tiny in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2012, 01:54 PM
  2. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM
  3. bounding box collision
    By beechy34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 5th, 2010, 08:58 AM
  4. How to detect brightest spot among all spots?
    By BharatT in forum Java Theory & Questions
    Replies: 4
    Last Post: February 6th, 2009, 09:12 PM
  5. Comparing hash functions and collision resolutions
    By dansongarcia in forum Collections and Generics
    Replies: 0
    Last Post: November 11th, 2008, 10:50 AM