Outline: Polygon Physics Detection Library

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
TheFriendlyFoe
Prole
Posts: 7
Joined: Fri Mar 18, 2022 11:42 am

Outline: Polygon Physics Detection Library

Post by TheFriendlyFoe »

Simple physics detection library I made. It is composed of polygons and detects if the lines of two polygons intersect. That means if one polygon is fully inside another then it returns false. This can be useful for things like making sure they player is staying in bounds.

Polygons can be made with arrays of points like so.

Code: Select all

local points = {
	{x=20, y=20},
	{x=40, y=20},
	{x=30, y=30},
	{x=30, y=40},
	{x=20, y=30},
}
myPolygon = OL.create.basic(points) --now your polygon is stored in that table
They can be rotated, moved, and scaled.

Code: Select all

OL.angle.rotate(myPolygon, 5) --rotates polygon by 5 degrees
OL.position.move(myPolygon, 10, 10) --moves the polygon by 10x and 10y 
OL.scale.change(myPolygon, 2) --makes the polygon twice as big
Then you can detect collisions with them.

Code: Select all

OL.collision.basic(myPolygon, anotherPolygon) --returns true if touching and false if not.
Everything is explained in the library next to the function.

List of functions:

OL.create.basic
OL.create.advanced

OL.collision.basic
OL.collision.advanced
OL.collision.setCustomTags
OL.collision.setTag

OL.position.set
OL.position.move
OL.position.movePoint

OL.angle.set
OL.angle.rotate
OL.angle.rotatePoint

OL.scale.set
OL.scale.change
OL.scale.changePoint

OL.draw.basic
OL.draw.advanced
OL.draw.setColorPalet
OL.draw.changeColor

Feel free to make it better and release it as your own. I don't really care what you do with this, just credit me in some small form. If anyone makes anything with this, I would love to see it.
Attachments
main.lua
Demo
(1.95 KiB) Downloaded 132 times
outline.lua
The Library
(10.96 KiB) Downloaded 130 times
Last edited by TheFriendlyFoe on Tue May 31, 2022 12:30 pm, edited 2 times in total.
User avatar
GVovkiv
Party member
Posts: 685
Joined: Fri Jan 15, 2021 7:29 am

Re: Outline: Polygon Physics Detection Library

Post by GVovkiv »

It's better to wrap library in module, not in global variable:

Code: Select all

local OL = {}
* library stuff *
return OL
Post Reply

Who is online

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