Simple Collision

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
hey5000
Prole
Posts: 4
Joined: Tue Jan 13, 2009 7:21 am
Location: Georgia

Simple Collision

Post by hey5000 »

First, YES I did read the Wiki snippet for "Poor Man's Collision" (See below). But I need a full example of collision, specifically a player colliding with an object or wall (such as the window boundary's).

Hope I'm not asking too much, thanks! :nyu:

I ALSO hope that I was clear enough for what I need..

Wiki Snippet:

Code: Select all

function colide(x1,y1,width1,height1,x2,y2,width2,height2)
    return not ( 
        (x2 > (x1 + width1 )) or
        (x1 > (x2 + width2 )) or
        (y2 > (y1 + height1)) or
        (y1 > (y2 + height2)) )
end
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: Simple Collision

Post by napco »

Uhm... The function you've written doesn't work with the window boundaries, but it looks fine for object-object collision. An example could be:

Code: Select all

player = {}
player.x = whatdoyouwant
player.y = whatdoyouwant
player.width = 16
player.height = 16
object = {}
object.x = whatdoyouwant
object.y = whatdoyouwant
object.width = 16
object.height = 16

function update(dt)
        if love.keyboard.isDown(love.key_right) then
                if not collide(player.x, player.y, player.width, player.height, object.x, etc...) then player.x = player.x + dt * desired framerate end
        end
end
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Simple Collision

Post by Jasoco »

Here's what I use: (Made it myself with hours of testing)

Code: Select all

function overlap(x1,y1,w1,h1,x2,y2,w2,h2)
	local tl, bl, tr, br = false, false, false, false
	if (x2 >= x1 and x2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tl = true end
	if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2 >= y1 and y2 <= (y1 + h1)) then tr = true end
	if (x2 >= x1 and x2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then bl = true end
	if (x2+w2 >= x1 and x2+w2 <= (x1 + w1)) and (y2+h2 >= y1 and y2+h2 <= (y1 + h1)) then br = true end

	if (x1 >= x2 and x1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tl = true end
	if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1 >= y2 and y1 <= (y2 + h2)) then tr = true end
	if (x1 >= x2 and x1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then bl = true end
	if (x1+w1 >= x2 and x1+w1 <= (x2 + w2)) and (y1+h1 >= y2 and y1+h1 <= (y2 + h2)) then br = true end

	if tl or tr or bl or br then return true else return false end
end
Will return true if any part of a rectangle overlaps any part of another rectangle.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Simple Collision

Post by kikito »

I've done a quick google search and found this: http://silentmatt.com/computers/playgro ... ection.php

According to this page, it is easier if your rectangles are specified as four points (without heights and widths): If we call them A.x1, A.y1, A.x2, A.y2, B.x1, B.y1, B.x2, B.y2, we end up with the following condition:

Code: Select all

 (A.x1 < B.x2 and A.x2 > B.x1 and A.y1 < B.y2 and A.y2 > B.y1) 
If you must have x1, y1, w1, h1 and x2, y2, w2, h2 then it gets a bit uglier, but still intelligible:

Code: Select all

 (x1 < x2+w2 and x1+w1 > x2 and y1 < y2+h2 and y1+h1 > y2)
You might want to change some of the < and > by <= and >= if you want to test if two rectangles are "touching" without "overlapping".

More information on this stackoverflow post: http://stackoverflow.com/questions/3063 ... each-other
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests