8 Replies - 1383 Views - Last Post: 11 January 2012 - 02:26 PM Rate Topic: -----

#1 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 08:31 PM

Hello DIC, I'm trying to learn how to make 2D games with the default Java graphics library, and I was wondering how one could detect a Rectangle hitting another Rectangle on a specific side.

Like so:

+-----+ +-----+
|     | |     |
|     | |     |
+-----+ +-----+



I'd want to see if they hit on the X-axis, so I could then set the speed of the X-axis to 0.

Is there any way I could do this?

Thank you!
~Crockeo

P.S.
If you need more specific info, please ask.

Is This A Good Question/Topic? 0
  • +

Replies To: Seeing What side a Rectangle Intersected On

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 08:51 PM

View PostCrockeo, on 10 January 2012 - 10:31 PM, said:

Is there any way I could do this?

There are many ways you can do it, really depends of the rest of your code. When you know that 2 rectangles intersect not really difficult to figure out on which side(s)

This post has been edited by pbl: 10 January 2012 - 08:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 08:57 PM

Thought about making them individual identities using OO?

public class rect1{
//getters and setters
}

public class rect2{
// getters and setters
}

# alternative is go for this approach which is probably better :

Collision detection

Have a look at the Entity class.

This post has been edited by floppyspace: 10 January 2012 - 09:01 PM

Was This Post Helpful? -1
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8316
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 09:17 PM

View Postfloppyspace, on 10 January 2012 - 10:57 PM, said:

Thought about making them individual identities using OO?

public class rect1{
//getters and setters
}

public class rect2{
// getters and setters
}

# alternative is go for this approach which is probably better :

What a waste !!!
What differentiate your class rect1 from rect2 ? A rect is a rect.

Rect rect1 = new Rect(0, 0, 100, 10);
Rect rect2 = new Rect(10, 10, 100, 10);

When you will have 100 rectangle you will write classes rect1 to rect100 ?
Was This Post Helpful? 1
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • Joined: 05-May 05

Re: Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 09:40 PM

You could use the Java awt package.

Rectangle2D.Float rect1 = new Rectangle2D.Float(0, 0, 50, 50);          
Rectangle2D.Float rect2 = new Rectangle2D.Float(25, 25, 50, 50);
System.out.println(rect1.intersects(rect2));



That's just an example. That won't help you determine the side at which the intersection occurs. Suppose you have the following:

----
|  |---
---|  |
   ----



To check for a collision on the right side of the upper rectangle.

Line2D.Float rect1RightSide = 
new Line2D.Float((rect1.getX()+rect1.getWidth()), rect1.getY(), (rect1.getX()+rect1.getWidth()), (rect1.getY()+rect1.getHeight()));


Line2D.Float rect2LeftSide = 
new Line2D.Float(rect2.getX(), rect2.getY(), rect2.getX(), (rect2.getY()+rect2.getHeight()));

System.out.println(rect1RightSide.intersectsLine(rect2LeftSide));


Perhaps that's not the most versatile way of doing it, but since the right side of a rectangle can only collide with the left side of another it will always work (as long as the rectangle don't rotate). Apply the same logic for the 3 other cases.

Just one of many ways...
Was This Post Helpful? 0
  • +
  • -

#6 floppyspace  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 254
  • Joined: 04-February 10

Re: Seeing What side a Rectangle Intersected On

Posted 10 January 2012 - 10:30 PM

View Postpbl, on 10 January 2012 - 09:17 PM, said:

View Postfloppyspace, on 10 January 2012 - 10:57 PM, said:

Thought about making them individual identities using OO?

public class rect1{
//getters and setters
}

public class rect2{
// getters and setters
}

# alternative is go for this approach which is probably better :

What a waste !!!
What differentiate your class rect1 from rect2 ? A rect is a rect.

Rect rect1 = new Rect(0, 0, 100, 10);
Rect rect2 = new Rect(10, 10, 100, 10);

When you will have 100 rectangle you will write classes rect1 to rect100 ?


Correct is was a waste but its a simple explanation, however the link I added uses the Rect rec1,rec2 approach which yes is the better approach.
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1910
  • View blog
  • Posts: 3,955
  • Joined: 11-December 07

Re: Seeing What side a Rectangle Intersected On

Posted 11 January 2012 - 03:26 AM

Is it a waste? You could define your rectangle as an array of lines. Then it would be really easy to write a loop that finds all the collisions between edges! It would be a doddle to deal with rotated rectangles too, and then it would be simple to modify the class to represent any polygon.
Was This Post Helpful? 1
  • +
  • -

#8 SwiftStriker00  Icon User is offline

  • Microsoft Insider
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Seeing What side a Rectangle Intersected On

Posted 11 January 2012 - 07:26 AM

This is probably the best book i have read on collision detection ( 2D and 3D) . I think his examples are in C/C++, but that doesn't change algorithms.

Real Time Collision Detection
Was This Post Helpful? 0
  • +
  • -

#9 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Seeing What side a Rectangle Intersected On

Posted 11 January 2012 - 02:26 PM

Thanks for all of the replies, I'll look these over, and maybe sometime soon I'll have a post about my first game :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1