Page 1 of 1

Collision detection.

Posted: Mon May 16, 2011 12:37 pm
by Anxiety
How can i detect if two images are colliding?

I quess i dont have to say anything else anymore than thank you.

Re: Collision detection.

Posted: Mon May 16, 2011 1:43 pm
by ivan
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.
1.Bitmasking
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

Re: Collision detection.

Posted: Mon May 16, 2011 5:02 pm
by Robin
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.

Re: Collision detection.

Posted: Mon May 16, 2011 5:21 pm
by ivan
Depends on your rect implementation.
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

Re: Collision detection.

Posted: Mon May 16, 2011 5:46 pm
by kikito
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

Re: Collision detection.

Posted: Mon May 16, 2011 6:10 pm
by Lafolie
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

Re: Collision detection.

Posted: Thu May 19, 2011 8:24 am
by Franc[e]sco
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 :awesome: 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