Collision: Polygons and Lines

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
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Collision: Polygons and Lines

Post 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
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Collision: Polygons and Lines

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Google [Bot] and 9 guests