Results 1 to 6 of 6
  1. #1
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Rotated rectangle collision detection

    Hello. I tried to make collision detection for rotated rectangle in java graphics, but now i have problem. When rectangle is rotated, im getting all of rotated rectangle corners x, y positions and then creating four lines. And now for collision detection I was tried to check for intersection with other rectangle and all rotated rectangle side lines, but lines is only 1 px thickness, so if I have 200x200 pixels rotated rectangle and 10x10 pixels rectangle goes inside rotated rectangle, my application says that there is no collision detection, cause 10x10 size rectangle not collides rotated rectangle side lines. So I need to make somehow to check if that 10x10 is inside rotated rectangle. This is how I tried:
    Java Code:
    double minX = 10000, minY = 10000, maxX = 0, maxY = 0;
    		for(int i=0; i<side_lines.length; i++){
    			//Get lowest and highest X
    			if(side_lines[i].getX1() < minX){ minX = side_lines[i].getX1(); }
    			if(side_lines[i].getX2() < minX){ minX = side_lines[i].getX2(); }
    			if(side_lines[i].getX1() > maxX){ maxX = side_lines[i].getX1(); }
    			if(side_lines[i].getX2() > maxX){ maxX = side_lines[i].getX2(); }
    			//Get lowest and highest Y
    			if(side_lines[i].getY1() < minY){ minY = side_lines[i].getY1(); }
    			if(side_lines[i].getY2() < minY){ minY = side_lines[i].getY2(); }
    			if(side_lines[i].getY1() > maxY){ maxY = side_lines[i].getY1(); }
    			if(side_lines[i].getY2() > maxY){ maxY = side_lines[i].getY2(); }
    		}
    		Rectangle temp_rect = new Rectangle((int)minX, (int)minY, (int)(maxX-minX), (int)(maxY-minY));
    		if(temp_rect.intersects(r)){
    			return true;
    		}
    		return false;
    Now this says that there is collision, but even if it will be like that:
    Rotated rectangle collision detection-28ch5zt.png

    So I need help to make code checking if that blue rectangle is inside between black rectangle side lines.
    Sorry for my bad English.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Rotated rectangle collision detection

    How would you calculate whether the rectangles intersect, without using a computer?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Rotated rectangle collision detection

    Moved from Advanced Java. Not an advanced question either.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Rotated rectangle collision detection

    Quote Originally Posted by DarrylBurke View Post
    How would you calculate whether the rectangles intersect, without using a computer?

    db
    Do not know, poor logic at me.. :D
    Sorry for my bad English.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Rotated rectangle collision detection

    Well, you need to understand the mathematics behind it before you even attempt to code it up. So I suggest you focus on that first and come back if/when you have a Java question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Daslee's Avatar
    Daslee is offline Member
    Join Date
    Mar 2012
    Location
    Plunge, Lithuania
    Posts
    36
    Rep Power
    0

    Default Re: Rotated rectangle collision detection

    Quote Originally Posted by DarrylBurke View Post
    Well, you need to understand the mathematics behind it before you even attempt to code it up. So I suggest you focus on that first and come back if/when you have a Java question.

    db
    Finally! I turned on my brains for a while, and finally realized how to check for that intersection. Before some time I was thinking to check if any corner point from rect1 is inside rect2, but I knew only one method how to check if some point is inside some area, it was similarly like contains method in Polygon class. But when I thought about that idea, it looked little bit weird for me...
    But anyway, now to check for intersection I create new Polygon class which has 4 points(all rotated rectangle(rect1) corner points) and then I check if that polygon contains any corner point from other rotated rectangle(rect2). :)
    Last edited by Daslee; 10-25-2012 at 02:57 PM.
    Sorry for my bad English.

Similar Threads

  1. Collision detection
    By mwr1976 in forum Java 2D
    Replies: 2
    Last Post: 04-14-2012, 12:45 PM
  2. Collision Detection
    By Äppelpaj in forum Java 2D
    Replies: 1
    Last Post: 10-13-2011, 03:29 PM
  3. Collision Detection
    By sunde887 in forum Java 2D
    Replies: 2
    Last Post: 10-07-2011, 11:40 PM
  4. Collision Detection?
    By Alerhau in forum New To Java
    Replies: 39
    Last Post: 09-07-2011, 04:55 PM
  5. Really Need help with some collision detection
    By Harwad in forum New To Java
    Replies: 1
    Last Post: 01-23-2011, 12:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •