Page 1 of 1

Rectangle collision with angled rectangles?

Posted: Tue Sep 08, 2015 6:23 pm
by Kasperelo
How do you check collision for two rectangles that are angled / rotated? I need this for a top down shooter game im doing.

Re: Rectangle collision with angled rectangles?

Posted: Tue Sep 08, 2015 6:29 pm
by arampl

Re: Rectangle collision with angled rectangles?

Posted: Wed Sep 09, 2015 5:12 pm
by T-Bone
That doesn't really answer the question.

This seems like a pretty decent tutorial on the subject: http://www.ragestorm.net/tutorial?id=22

Re: Rectangle collision with angled rectangles?

Posted: Wed Sep 09, 2015 6:49 pm
by Ref
Never bothered to determine which is faster but I've just used a simple test to see if any of the edges makes contact - no trig involved.

Re: Rectangle collision with angled rectangles?

Posted: Wed Sep 09, 2015 10:40 pm
by pel
Checking edges will not work if one rectangle is bigger, and the smaller one is inside it.

Re: Rectangle collision with angled rectangles?

Posted: Thu Sep 10, 2015 7:49 pm
by Kasperelo
How do I check edges?

Re: Rectangle collision with angled rectangles?

Posted: Sat Sep 12, 2015 1:04 am
by Ref
Compare each edge of rectangle 1 (x1,y1,x2,y2 )with each edge of rectangle 2 (x3,y3,x4,y4) and see if they intersect.
The following function will return true is the edges overlap.

Code: Select all

function segmentIntersects(x1, y1, x2, y2, x3, y3, x4, y4)
	local d = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1)
	if d == 0 then return false end
	local Ua = ((x4-x3)*(y1-y3)-(y4-y3)*(x1-x3))/d
	local Ub = ((x2-x1)*(y1-y3)-(y2-y1)*(x1-x3))/d
	if Ua >= 0 and Ua <= 1 and Ub >= 0 and Ub <= 1 then
		return true
		end
	return false
	end

Re: Rectangle collision with angled rectangles?

Posted: Sat Sep 12, 2015 8:56 am
by ivan
Kasperelo wrote:How do I check edges?
This is half the test though, like Pel mentioned you ALSO have to check if any of the vertices of rectangle 1 are inside rectangle 2 and vice versa.
For rotated rectangles a better approach is SAT or the separating axis theorem.
Either way, rotated rects are probably way too complicated than what you usually need for a simple 2D shooter.