Rectangle collision with angled rectangles?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Rectangle collision with angled rectangles?
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?
That doesn't really answer the question.arampl wrote:viewtopic.php?f=4&t=80853
This seems like a pretty decent tutorial on the subject: http://www.ragestorm.net/tutorial?id=22
My game called Hat Cat and the Obvious Crimes Against the Fundamental Laws of Physics is out now!
Re: Rectangle collision with angled rectangles?
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?
Checking edges will not work if one rectangle is bigger, and the smaller one is inside it.
Re: Rectangle collision with angled rectangles?
How do I check edges?
Re: Rectangle collision with angled rectangles?
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.
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?
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.Kasperelo wrote:How do I check edges?
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 1 guest