Page 1 of 1

Collision: Polygons and Lines

Posted: Sun Feb 16, 2014 8:28 pm
by Mangl
Hi guys.

I'll cut to the chase, I'm not sure how to approach checking if lines intersect with polygons. I would like my game to have enemies with hitboxes made out of polygons (probably mostly rectangles and triangles) and also laser weapons which pass through enemies whilst dealing damage to them.

I figure that there are probably some great libraries already out there for this but I'm not sure which are most suited to this, and I'd kind of like to get a better understanding of how to perform these kind of checks for when I start tackling poly vs poly collision.

Any recommendations or explanations would be much appreciated! :3

Re: Collision: Polygons and Lines

Posted: Sun Feb 16, 2014 9:19 pm
by davisdude
Well, since you mention libraries, here are two I know:
MLib (made by me) and HardOn made by vrld.
I don't know about the latter, but in MLib, here's how you would do it:

Code: Select all

local mlib = require( 'mlib' )

function love.load()
     Line = mlib.shape.new( 1, 30, 40, 50 )
     Polygon = mlib.shape.new( 10, 10, 10, 20, 20, 20, 20, 10 )
end

function love.draw()
     love.graphics.line( Line.x1, Line.y1, Line.x2, Line.y2 )
     love.graphics.polygon( 'fill', Polygon.points )
end

function love.update( dt )
     mlib.shape.checkCollisions()
end
You can always read more on the wiki and stuff.