Rectangle collision with angled rectangles?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Rectangle collision with angled rectangles?

Post 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.
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: Rectangle collision with angled rectangles?

Post by arampl »

User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Rectangle collision with angled rectangles?

Post 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
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Rectangle collision with angled rectangles?

Post 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.
pel
Prole
Posts: 28
Joined: Mon Aug 17, 2015 6:48 am

Re: Rectangle collision with angled rectangles?

Post by pel »

Checking edges will not work if one rectangle is bigger, and the smaller one is inside it.
User avatar
Kasperelo
Party member
Posts: 343
Joined: Fri Apr 13, 2012 1:47 pm
Location: The Milky Way

Re: Rectangle collision with angled rectangles?

Post by Kasperelo »

How do I check edges?
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Rectangle collision with angled rectangles?

Post 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
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Rectangle collision with angled rectangles?

Post 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.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 3 guests