Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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 :]

share|improve this answer
    
Thanks for your help! I'm not sure if everything will work but I voted on your post for the help. –  CPP_Person Feb 16 '13 at 10:14
1  
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. –  Darcy Rayner Feb 16 '13 at 10:26
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.