0
\$\begingroup\$

I want to know how would I tell if a quad touches another quad in my game built in Slick2D. I have a class that I named "Bounds" which has 4 "Point" objects which just store an X and a Y value and a "Bounds" object stores 4 "Point"s. Simply it has 4 2D vectors, one stores the top left, another stores the top right, another stores the bottom right and the other stores the bottom left. I need to be able to check if two quads based of those 4 coordinates are inside each other. Does anyone have an idea of what I should do?

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I cant tell if you just want to do bounding box collision or somthing more complicated with the vectors but if you just want to see if a rectangle is inside another rectangle then check if :

box1.left   < box2.right &&
box1.right  > box2.left &&
box1.top    < box2.bottom && // if your Y starts at bottom of screen and gets higher
box1.bottom > box2.top       // as you go up the screen swap these round. aka the > <

enter image description here

If all that is true you have a collision, You can then check what side of a box is actully hiting by just changing some of that code around.

Like if the left side of one box is more left then the right side and more right the left side of another box then you know the left side of you box is inside another and not your right side.

Hope that helps, if not sorry :S

Edit: now with pic to help work out that code :]

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your help! I'm not sure if everything will work but I voted on your post for the help. \$\endgroup\$ Feb 16, 2013 at 10:14
  • 1
    \$\begingroup\$ This is only correct if the quads are axis aligned rectangles, which from the original question then might not be, (they could be rotated/irregularly shaped for instance). Still, with that caveat, I suspect this is the answer the OP is actually looking for. \$\endgroup\$ Feb 16, 2013 at 10:26
0
\$\begingroup\$

I actually wouldn't assume that the boxes are axis aligned since they are defined using vector2's for each corner. In fact no where does it say that these are rectangles. My solution would be to do a point test on each point from 1 quad in the other and then do the same from the other perspective... using a winding algorithm you can test if a point is inside or outside of another polygon(quad). If 1 or more points is inside then they are colliding.

Here is a link to a discussion about algorithms that determine if a point is inside a polygon(quad)

http://geomalgorithms.com/a03-_inclusion.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .