## Collision detection.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Anxiety
Prole
Posts: 49
Joined: Sat Apr 02, 2011 9:36 am
Location: Finland

### Collision detection.

How can i detect if two images are colliding?

I quess i dont have to say anything else anymore than thank you.
I can't come up with a good signature!

ivan
Party member
Posts: 1560
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Collision detection.

Anxiety wrote:How can i detect if two images are colliding?

I quess i dont have to say anything else anymore than thank you.
There's 2 general approaches that I can think of at the moment.
You create a bitmask for every image where each pixel is either 0 or 1 then you test the two bitmasks for overlapping pixels
As far as I know, this approach is pretty rare except for very specific games
2.Bounding volumes
You associate a bounding volume to each sprite (such as a circle or a rectangle) and test for a collision between the bounding volumes

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

### Re: Collision detection.

Anxiety wrote:How can i detect if two images are colliding?
Check whether the two rectangles that make up the images are intersecting. There is a very simple function for that, but I don't know it by heart, it's on this forum somewhere though.

ivan
Party member
Posts: 1560
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Collision detection.

The easiest version is when your rect is represented by a left, top, right and bottom value.
The code then becomes:

Code: Select all

--         -----------
-- -----------       |
-- |       | |(b2)   |
-- |  (b1) | |       |
-- |       -----------
-- -----------

-- Tests rect (b1) against rect (b2)
-- Returns true if they intersect
function RectVsRect ( r1l, r1t, r1r, r1b, r2l, r2t, r2r, r2b )
if r1r < r2l or r1l > r2r then
return false
end
if r1b < r2t or r1t > r2b then
return false
end
return true
end

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Collision detection.

Robin wrote:
Anxiety wrote:How can i detect if two images are colliding?
Check whether the two rectangles that make up the images are intersecting. There is a very simple function for that, but I don't know it by heart, it's on this forum somewhere though.
It's actually on the wiki: BoundingBox.lua. That's a bit simpler than ivan's implementation
When I write def I mean function.

Lafolie
Inner party member
Posts: 804
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

### Re: Collision detection.

I use this function that has been passed down from thread to thread many times apparently, ha. I don't know the original author.

Code: Select all

function overlap(x1,y1,w1,h1, x2,y2,w2,h2)
return not (x1+w1 < x2  or x2+w2 < x1 or y1+h1 < y2 or y2+h2 < y1)
end
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.

Franc[e]sco
Prole
Posts: 19
Joined: Sun May 08, 2011 9:09 am

### Re: Collision detection.

I have a Rectangle class and I just do ptinrect of all the vertex of rect1 in rect2 and ptinrect of all the vertex of rect2 in rect1 I'm quite sure there are more efficient ways to do it though.

Code: Select all

function Rectangle:ptinrect(pt)
return (pt.x >= self.lt.x and pt.x <= self.rt.x and pt.y >= self.lt.y and pt.y <= self.lb.y)
end

function Rectangle:intersect(rect)
return (
self:ptinrect(rect.lt) or
self:ptinrect(rect.lb) or
self:ptinrect(rect.rt) or
self:ptinrect(rect.rb) or
rect:ptinrect(self.lt) or
rect:ptinrect(self.lb) or
rect:ptinrect(self.rt) or
rect:ptinrect(self.rb)
)
end

### Who is online

Users browsing this forum: No registered users and 8 guests