parent
7a72c42d28
commit
8fdbc3d0df
@ -0,0 +1,8 @@ |
|||||||
|
{ |
||||||
|
"runtime.special": { |
||||||
|
"love.filesystem.load": "loadfile" |
||||||
|
}, |
||||||
|
"workspace.library": [ |
||||||
|
"${3rd}/love2d/library" |
||||||
|
] |
||||||
|
} |
@ -0,0 +1,22 @@ |
|||||||
|
function drawFPS() |
||||||
|
love.graphics.setColor(1, 1, 1) -- RGB values for white are (1, 1, 1) |
||||||
|
--love.graphics.setFont(DebugFont) |
||||||
|
love.graphics.print("FPS: " .. love.timer.getFPS(), 1550, 10) |
||||||
|
end |
||||||
|
|
||||||
|
function DrawGame() |
||||||
|
-- WindField |
||||||
|
if DebugFlag then |
||||||
|
World:draw() |
||||||
|
drawFPS() |
||||||
|
end |
||||||
|
|
||||||
|
for _, v in ipairs(Bullets1) do |
||||||
|
v:draw() |
||||||
|
end |
||||||
|
for _, v in ipairs(Bullets2) do |
||||||
|
v:draw() |
||||||
|
end |
||||||
|
UserPlayer1:draw() |
||||||
|
UserPlayer2:draw() |
||||||
|
end |
@ -0,0 +1,13 @@ |
|||||||
|
function KeyPressed(key) |
||||||
|
if key == "escape" then |
||||||
|
love.event.quit() |
||||||
|
end |
||||||
|
|
||||||
|
if key == "b" then |
||||||
|
DebugFlag = not DebugFlag |
||||||
|
end |
||||||
|
|
||||||
|
if key == "r" then |
||||||
|
love.load() |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,50 @@ |
|||||||
|
function UpdateGame(dt) |
||||||
|
local max = math.max |
||||||
|
KeyPressTime1 = max(0, KeyPressTime1 - dt) |
||||||
|
if KeyPressTime1 <= 0 then |
||||||
|
EnableKeyPress1 = true |
||||||
|
end |
||||||
|
|
||||||
|
KeyPressTime2 = max(0, KeyPressTime2 - dt) |
||||||
|
if KeyPressTime2 <= 0 then |
||||||
|
EnableKeyPress2 = true |
||||||
|
end |
||||||
|
for i, v in ipairs(Bullets1) do |
||||||
|
v:update(dt) |
||||||
|
if v.y < 0 then --top of screen |
||||||
|
table.remove(Bullets1, i) |
||||||
|
v.collider:destroy() |
||||||
|
elseif v.y > love.graphics.getHeight() then --bottom of screen |
||||||
|
table.remove(Bullets1, i) |
||||||
|
v.collider:destroy() |
||||||
|
end |
||||||
|
if v.collider:enter("Player2") then |
||||||
|
print("Player 2 hit") |
||||||
|
table.remove(Bullets1, i) |
||||||
|
v.collider:destroy() |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
for i, v in ipairs(Bullets2) do |
||||||
|
v:update(dt) |
||||||
|
if v.y < 0 then --top of screen |
||||||
|
table.remove(Bullets2, i) |
||||||
|
v.collider:destroy() |
||||||
|
elseif v.y > love.graphics.getHeight() then --bottom of screen |
||||||
|
table.remove(Bullets2, i) |
||||||
|
v.collider:destroy() |
||||||
|
end |
||||||
|
|
||||||
|
if v.collider:enter("Player1") then |
||||||
|
print("Player 1 hit") |
||||||
|
table.remove(Bullets2, i) |
||||||
|
v.collider:destroy() |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
UserPlayer1:update(dt) |
||||||
|
UserPlayer2:update(dt) |
||||||
|
|
||||||
|
--WindField |
||||||
|
World:update(dt) |
||||||
|
end |
Binary file not shown.
After Width: | Height: | Size: 827 B |
After Width: | Height: | Size: 556 B |
After Width: | Height: | Size: 4.2 KiB |
@ -0,0 +1,78 @@ |
|||||||
|
Bullet = Object:extend() |
||||||
|
|
||||||
|
function Bullet:new(x, y, p, speed, rotation) |
||||||
|
self.image = love.graphics.newImage("assets/bullet.png") |
||||||
|
self.x = x |
||||||
|
self.y = y |
||||||
|
self.p = p |
||||||
|
self.speed = speed |
||||||
|
self.rotation = rotation |
||||||
|
|
||||||
|
self.width = self.image:getWidth() |
||||||
|
self.height = self.image:getHeight() |
||||||
|
|
||||||
|
self.scaleX = 1 |
||||||
|
self.scaleY = 1 |
||||||
|
self.originX = self.width / 2 |
||||||
|
self.originY = self.height / 2 |
||||||
|
|
||||||
|
self.collider = World:newRectangleCollider(x, y, 10, 15) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
|
||||||
|
if self.p == 1 then |
||||||
|
self.collider:setCollisionClass("Bullet1") |
||||||
|
elseif self.p == 2 then |
||||||
|
self.collider:setCollisionClass("Bullet2") |
||||||
|
end |
||||||
|
--TODO: find out a way to check the direction of a bullet |
||||||
|
end |
||||||
|
|
||||||
|
function Bullet:update(dt) |
||||||
|
local cos = math.cos |
||||||
|
local sin = math.sin --optimisation |
||||||
|
local atan2 = math.atan2 --optimisation |
||||||
|
if self.p == 1 then |
||||||
|
local dx = math.cos(self.rotation) * self.speed * dt |
||||||
|
local dy = math.sin(self.rotation) * self.speed * dt |
||||||
|
self.x = self.x + dx |
||||||
|
self.y = self.y + dy |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
end |
||||||
|
|
||||||
|
if self.p == 2 then |
||||||
|
local dx = math.cos(self.rotation) * self.speed * dt |
||||||
|
local dy = math.sin(self.rotation) * self.speed * dt |
||||||
|
self.x = self.x + dx |
||||||
|
self.y = self.y + dy |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Bullet:draw() |
||||||
|
for _, _ in ipairs(Bullets1) do |
||||||
|
love.graphics.draw( |
||||||
|
self.image, |
||||||
|
self.x, |
||||||
|
self.y, |
||||||
|
self.rotation, |
||||||
|
self.scaleX, |
||||||
|
self.scaleY, |
||||||
|
self.originX, |
||||||
|
self.originY |
||||||
|
) |
||||||
|
end |
||||||
|
|
||||||
|
for _, _ in ipairs(Bullets2) do |
||||||
|
-- love.graphics.draw(self.image, self.x, self.y) |
||||||
|
love.graphics.draw( |
||||||
|
self.image, |
||||||
|
self.x, |
||||||
|
self.y, |
||||||
|
self.rotation, |
||||||
|
self.scaleX, |
||||||
|
self.scaleY, |
||||||
|
self.originX, |
||||||
|
self.originY |
||||||
|
) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,68 @@ |
|||||||
|
-- |
||||||
|
-- classic |
||||||
|
-- |
||||||
|
-- Copyright (c) 2014, rxi |
||||||
|
-- |
||||||
|
-- This module is free software; you can redistribute it and/or modify it under |
||||||
|
-- the terms of the MIT license. See LICENSE for details. |
||||||
|
-- |
||||||
|
|
||||||
|
|
||||||
|
local Object = {} |
||||||
|
Object.__index = Object |
||||||
|
|
||||||
|
|
||||||
|
function Object:new() |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
function Object:extend() |
||||||
|
local cls = {} |
||||||
|
for k, v in pairs(self) do |
||||||
|
if k:find("__") == 1 then |
||||||
|
cls[k] = v |
||||||
|
end |
||||||
|
end |
||||||
|
cls.__index = cls |
||||||
|
cls.super = self |
||||||
|
setmetatable(cls, self) |
||||||
|
return cls |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
function Object:implement(...) |
||||||
|
for _, cls in pairs({...}) do |
||||||
|
for k, v in pairs(cls) do |
||||||
|
if self[k] == nil and type(v) == "function" then |
||||||
|
self[k] = v |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
function Object:is(T) |
||||||
|
local mt = getmetatable(self) |
||||||
|
while mt do |
||||||
|
if mt == T then |
||||||
|
return true |
||||||
|
end |
||||||
|
mt = getmetatable(mt) |
||||||
|
end |
||||||
|
return false |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
function Object:__tostring() |
||||||
|
return "Object" |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
function Object:__call(...) |
||||||
|
local obj = setmetatable({}, self) |
||||||
|
obj:new(...) |
||||||
|
return obj |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
return Object |
@ -0,0 +1,6 @@ |
|||||||
|
--Config file for the game |
||||||
|
function love.conf(t) |
||||||
|
t.window.width = 1600 |
||||||
|
t.window.height = 900 |
||||||
|
t.window.title = "Game" |
||||||
|
end |
Binary file not shown.
@ -0,0 +1,941 @@ |
|||||||
|
**windfield** is a physics module for LÖVE. It wraps LÖVE's physics API so that using box2d becomes as simple as possible. |
||||||
|
|
||||||
|
# Contents |
||||||
|
|
||||||
|
* [Quick Start](#quick-start) |
||||||
|
* [Create a world](#create-a-world) |
||||||
|
* [Create colliders](#create-colliders) |
||||||
|
* [Create joints](#create-joints) |
||||||
|
* [Create collision classes](#create-collision-classes) |
||||||
|
* [Capture collision events](#capture-collision-events) |
||||||
|
* [Query the world](#query-the-world) |
||||||
|
* [Examples & Tips](#examples-tips) |
||||||
|
* [Checking collisions between game objects](#checking-collisions-between-game-objects) |
||||||
|
* [One-way Platforms](#one-way-platforms) |
||||||
|
* [Documentation](#documentation) |
||||||
|
* [World](#world) |
||||||
|
* [newWorld](#newworldxg-yg-sleep) |
||||||
|
* [update](#updatedt) |
||||||
|
* [draw](#drawalpha) |
||||||
|
* [destroy](#destroy) |
||||||
|
* [addCollisionClass](#addcollisionclasscollision_class_name-collision_class) |
||||||
|
* [newCircleCollider](#newcirclecolliderx-y-r) |
||||||
|
* [newRectangleCollider](#newrectanglecolliderx-y-w-h) |
||||||
|
* [newBSGRectangleCollider](#newbsgrectanglecolliderx-y-w-h-corner_cut_size) |
||||||
|
* [newPolygonCollider](#newpolygoncollidervertices) |
||||||
|
* [newLineCollider](#newlinecolliderx1-y1-x2-y2) |
||||||
|
* [newChainCollider](#newchaincollidervertices-loop) |
||||||
|
* [queryCircleArea](#querycircleareax-y-r-collision_class_name) |
||||||
|
* [queryRectangleArea](#queryrectangleareax-y-w-h-collision_class_names) |
||||||
|
* [queryPolygonArea](#querypolygonareavertices-collision_class_names) |
||||||
|
* [queryLine](#querylinex1-y1-x2-y2-collision_class_names) |
||||||
|
* [addJoint](#addjointjoint_type) |
||||||
|
* [removeJoint](#removejointjoint) |
||||||
|
* [setExplicitCollisionEvents](#setexplicitcollisioneventsvalue) |
||||||
|
* [setQueryDebugDrawing](#setquerydebugdrawingvalue) |
||||||
|
* [Collider](#collider) |
||||||
|
* [destroy](#destroy-1) |
||||||
|
* [setCollisionClass](#setcollisionclasscollision_class_name) |
||||||
|
* [enter](#enterother_collision_class_name) |
||||||
|
* [getEnterCollisionData](#getentercollisiondataother_collision_class_name) |
||||||
|
* [exit](#exitother_collision_class_name) |
||||||
|
* [getExitCollisionData](#getexitcollisiondataother_collision_class_name) |
||||||
|
* [stay](#stayother_collision_class_name) |
||||||
|
* [getStayCollisionData](#getstaycollisiondataother_collision_class_name) |
||||||
|
* [setPreSolve](#setpresolvecallback) |
||||||
|
* [setPostSolve](#setpostsolvecallback) |
||||||
|
* [addShape](#addshapeshape_name-shape_type) |
||||||
|
* [removeShape](#removeshapeshape_name) |
||||||
|
* [setObject](#setobjectobject) |
||||||
|
* [getObject](#getobject) |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
# Quick Start |
||||||
|
|
||||||
|
Place the `windfield` folder inside your project and require it: |
||||||
|
|
||||||
|
```lua |
||||||
|
wf = require 'windfield' |
||||||
|
``` |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Create a world |
||||||
|
|
||||||
|
A physics world can be created just like in box2d. The world returned by `wf.newWorld` contains all the functions of a [LÖVE physics World](https://love2d.org/wiki/World) as well as additional ones defined by this library. |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
world = wf.newWorld(0, 0, true) |
||||||
|
world:setGravity(0, 512) |
||||||
|
end |
||||||
|
|
||||||
|
function love.update(dt) |
||||||
|
world:update(dt) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Create colliders |
||||||
|
|
||||||
|
A collider is a composition of a single body, fixture and shape. For most use cases whenever box2d is needed a body will only have one fixture/shape attached to it, so it makes sense to work primarily on that level of abstraction. Colliders contain all the functions of a LÖVE physics [Body](https://love2d.org/wiki/Body), [Fixture](https://love2d.org/wiki/Fixture) and [Shape](https://love2d.org/wiki/Shape) as well as additional ones defined by this library: |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
... |
||||||
|
|
||||||
|
box = world:newRectangleCollider(400 - 50/2, 0, 50, 50) |
||||||
|
box:setRestitution(0.8) |
||||||
|
box:applyAngularImpulse(5000) |
||||||
|
|
||||||
|
ground = world:newRectangleCollider(0, 550, 800, 50) |
||||||
|
wall_left = world:newRectangleCollider(0, 0, 50, 600) |
||||||
|
wall_right = world:newRectangleCollider(750, 0, 50, 600) |
||||||
|
ground:setType('static') -- Types can be 'static', 'dynamic' or 'kinematic'. Defaults to 'dynamic' |
||||||
|
wall_left:setType('static') |
||||||
|
wall_right:setType('static') |
||||||
|
end |
||||||
|
|
||||||
|
... |
||||||
|
|
||||||
|
function love.draw() |
||||||
|
world:draw() -- The world can be drawn for debugging purposes |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/ytfhmjc.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Create joints |
||||||
|
|
||||||
|
Joints are mostly unchanged from how they work normally in box2d: |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
... |
||||||
|
|
||||||
|
box_1 = world:newRectangleCollider(400 - 50/2, 0, 50, 50) |
||||||
|
box_1:setRestitution(0.8) |
||||||
|
box_2 = world:newRectangleCollider(400 - 50/2, 50, 50, 50) |
||||||
|
box_2:setRestitution(0.8) |
||||||
|
box_2:applyAngularImpulse(5000) |
||||||
|
joint = world:addJoint('RevoluteJoint', box_1, box_2, 400, 50, true) |
||||||
|
|
||||||
|
... |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/tSqkxJR.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Create collision classes |
||||||
|
|
||||||
|
Collision classes are used to make colliders ignore other colliders of certain classes and to capture collision events between colliders. The same concept goes by the name of 'collision layer' or 'collision tag' in other engines. In the example below we add a Solid and Ghost collision class. The Ghost collision class is set to ignore the Solid collision class. |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
... |
||||||
|
|
||||||
|
world:addCollisionClass('Solid') |
||||||
|
world:addCollisionClass('Ghost', {ignores = {'Solid'}}) |
||||||
|
|
||||||
|
box_1 = world:newRectangleCollider(400 - 100, 0, 50, 50) |
||||||
|
box_1:setRestitution(0.8) |
||||||
|
box_2 = world:newRectangleCollider(400 + 50, 0, 50, 50) |
||||||
|
box_2:setCollisionClass('Ghost') |
||||||
|
|
||||||
|
ground = world:newRectangleCollider(0, 550, 800, 50) |
||||||
|
ground:setType('static') |
||||||
|
ground:setCollisionClass('Solid') |
||||||
|
|
||||||
|
... |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/j7IhVSe.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
The box that was set be of the Ghost collision class ignored the ground and went right through it, since the ground is set to be of the Solid collision class. |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Capture collision events |
||||||
|
|
||||||
|
Collision events can be captured inside the update function by calling the `enter`, `exit` or `stay` functions of a collider. In the example below, whenever the box collider enters contact with another collider of the Solid collision class it will get pushed to the right: |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.update(dt) |
||||||
|
... |
||||||
|
if box:enter('Solid') then |
||||||
|
box:applyLinearImpulse(1000, 0) |
||||||
|
box:applyAngularImpulse(5000) |
||||||
|
end |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/uF1bqKM.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## Query the world |
||||||
|
|
||||||
|
The world can be queried with a few area functions and then all colliders inside that area will be returned. In the example below, the world is queried at position 400, 300 with a circle of radius 100, and then all colliders in that area are pushed to the right and down. |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
world = wf.newWorld(0, 0, true) |
||||||
|
world:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames |
||||||
|
|
||||||
|
colliders = {} |
||||||
|
for i = 1, 200 do |
||||||
|
table.insert(colliders, world:newRectangleCollider(love.math.random(0, 800), love.math.random(0, 600), 25, 25)) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function love.update(dt) |
||||||
|
world:update(dt) |
||||||
|
end |
||||||
|
|
||||||
|
function love.draw() |
||||||
|
world:draw() |
||||||
|
end |
||||||
|
|
||||||
|
function love.keypressed(key) |
||||||
|
if key == 'p' then |
||||||
|
local colliders = world:queryCircleArea(400, 300, 100) |
||||||
|
for _, collider in ipairs(colliders) do |
||||||
|
collider:applyLinearImpulse(1000, 1000) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/YVxAiuu.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
# Examples & Tips |
||||||
|
|
||||||
|
## Checking collisions between game objects |
||||||
|
|
||||||
|
The most common use case for a physics engine is doing things when things collide. For instance, when the Player collides with an enemy you might want to deal damage to the player. Here's the way to achieve that with this library: |
||||||
|
|
||||||
|
|
||||||
|
```lua |
||||||
|
-- in Player.lua |
||||||
|
function Player:new() |
||||||
|
self.collider = world:newRectangleCollider(...) |
||||||
|
self.collider:setCollisionClass('Player') |
||||||
|
self.collider:setObject(self) |
||||||
|
end |
||||||
|
|
||||||
|
-- in Enemy.lua |
||||||
|
function Enemy:new() |
||||||
|
self.collider = world:newRectangleCollider(...) |
||||||
|
self.collider:setCollisionClass('Enemy') |
||||||
|
self.collider:setObject(self) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
First we define in the constructor of both classes the collider that should be attached to them. We set their collision classes (Player and Enemy) and then link the object to the colliders with `setObject`. With this, we can capture collision events between both and then do whatever we wish when a collision happens: |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in Player.lua |
||||||
|
function Player:update(dt) |
||||||
|
if self.collider:enter('Enemy') then |
||||||
|
local collision_data = self.collider:getEnterCollisionData('Enemy') |
||||||
|
local enemy = collision_data.collider:getObject() |
||||||
|
-- Kills the enemy on hit but also take damage |
||||||
|
enemy:die() |
||||||
|
self:takeDamage(10) |
||||||
|
end |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
## One-way Platforms |
||||||
|
|
||||||
|
A common problem people have with using 2D physics engines seems to be getting one-way platforms to work. Here's one way to achieve this with this library: |
||||||
|
|
||||||
|
```lua |
||||||
|
function love.load() |
||||||
|
world = wf.newWorld(0, 512, true) |
||||||
|
world:addCollisionClass('Platform') |
||||||
|
world:addCollisionClass('Player') |
||||||
|
|
||||||
|
ground = world:newRectangleCollider(100, 500, 600, 50) |
||||||
|
ground:setType('static') |
||||||
|
platform = world:newRectangleCollider(350, 400, 100, 20) |
||||||
|
platform:setType('static') |
||||||
|
platform:setCollisionClass('Platform') |
||||||
|
player = world:newRectangleCollider(390, 450, 20, 40) |
||||||
|
player:setCollisionClass('Player') |
||||||
|
|
||||||
|
player:setPreSolve(function(collider_1, collider_2, contact) |
||||||
|
if collider_1.collision_class == 'Player' and collider_2.collision_class == 'Platform' then |
||||||
|
local px, py = collider_1:getPosition() |
||||||
|
local pw, ph = 20, 40 |
||||||
|
local tx, ty = collider_2:getPosition() |
||||||
|
local tw, th = 100, 20 |
||||||
|
if py + ph/2 > ty - th/2 then contact:setEnabled(false) end |
||||||
|
end |
||||||
|
end) |
||||||
|
end |
||||||
|
|
||||||
|
function love.keypressed(key) |
||||||
|
if key == 'space' then |
||||||
|
player:applyLinearImpulse(0, -1000) |
||||||
|
end |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
And that looks like this: |
||||||
|
|
||||||
|
<p align="center"> |
||||||
|
<img src="http://i.imgur.com/ouwxVRH.gif"/> |
||||||
|
</p> |
||||||
|
|
||||||
|
The way this works is that by disabling the contact before collision response is applied (so in the preSolve callback) we can make a collider ignore another. And then all we do is check to see if the player is below platform, and if he is then we disable the contact. |
||||||
|
|
||||||
|
<br> |
||||||
|
|
||||||
|
# Documentation |
||||||
|
|
||||||
|
## World |
||||||
|
|
||||||
|
On top of containing all functions exposed in this documentation it also contains all functions of a [box2d World](https://love2d.org/wiki/World). |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `.newWorld(xg, yg, sleep)` |
||||||
|
|
||||||
|
Creates a new World. |
||||||
|
|
||||||
|
```lua |
||||||
|
world = wf.newWorld(0, 0, true) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `xg` `(number)` - The world's x gravity component |
||||||
|
* `yg` `(number)` - The world's y gravity component |
||||||
|
* `sleep=true` `(boolean)` - If the world's bodies are allowed to sleep or not |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `World` `(table)` - the World object, containing all attributes and methods defined below as well as all of a [box2d World](https://love2d.org/wiki/World) |
||||||
|
|
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:update(dt)` |
||||||
|
|
||||||
|
Updates the world. |
||||||
|
|
||||||
|
```lua |
||||||
|
world:update(dt) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `dt` `(number)` - The time step delta |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:draw(alpha)` |
||||||
|
|
||||||
|
Draws the world, drawing all colliders, joints and world queries (for debugging purposes). |
||||||
|
|
||||||
|
```lua |
||||||
|
world:draw() -- default drawing |
||||||
|
world:draw(128) -- semi transparent drawing |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `alpha=255` `(number)` - The optional alpha value to use when drawing, defaults to 255 |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:destroy()` |
||||||
|
|
||||||
|
Destroys the world and removes all bodies, fixtures, shapes and joints from it. This must be called whenever the World is to discarded otherwise it will result in it not getting collected (and so memory will leak). |
||||||
|
|
||||||
|
```lua |
||||||
|
world:destroy() |
||||||
|
``` |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:addCollisionClass(collision_class_name, collision_class)` |
||||||
|
|
||||||
|
Adds a new collision class to the World. Collision classes are attached to Colliders and defined their behaviors in terms of which ones will physically ignore each other and which ones will generate collision events between each other. All collision classes must be added before any Collider is created. If `world:setExplicitCollisionEvents` is set to false (the default setting) then `enter`, `exit`, `pre` and `post` settings don't need to be specified, otherwise they do. |
||||||
|
```lua |
||||||
|
world:addCollisionClass('Player', {ignores = {'NPC', 'Enemy'}}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `collision_class_name` `(string)` - The unique name of the collision class |
||||||
|
* `collision_class` `(table)` - The collision class. This table can contain: |
||||||
|
|
||||||
|
Settings: |
||||||
|
|
||||||
|
* `[ignores]` `(table[string])` - The collision classes that will be physically ignored |
||||||
|
* `[enter]` `(table[string])` - The collision classes that will generate collision events with the collider of this collision class when they enter contact with each other |
||||||
|
* `[exit]` `(table[string])` - The collision classes that will generate collision events with the collider of this collision class when they exit contact with each other |
||||||
|
* `[pre]` `(table[string])` - The collision classes that will generate collision events with the collider of this collision class right before collision response is applied |
||||||
|
* `[post]` `(table[string])` - The collision classes that will generate collision events with the collider of this collision class right after collision response is applied |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newCircleCollider(x, y, r)` |
||||||
|
|
||||||
|
Creates a new CircleCollider. |
||||||
|
|
||||||
|
```lua |
||||||
|
circle = world:newCircleCollider(100, 100, 30) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x` `(number)` - The x position of the circle's center |
||||||
|
* `y` `(number)` - The y position of the circle's center |
||||||
|
* `r` `(number)` - The radius of the circle |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created CircleCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newRectangleCollider(x, y, w, h)` |
||||||
|
|
||||||
|
Creates a new RectangleCollider. |
||||||
|
|
||||||
|
```lua |
||||||
|
rectangle = world:newRectangleCollider(100, 100, 50, 50) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x` `(number)` - The x position of the rectangle's top-left corner |
||||||
|
* `y` `(number)` - The y position of the rectangle's top-left corner |
||||||
|
* `w` `(number)` - The width of the rectangle |
||||||
|
* `h` `(number)` - The height of the rectangle |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created RectangleCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newBSGRectangleCollider(x, y, w, h, corner_cut_size)` |
||||||
|
|
||||||
|
Creates a new BSGRectangleCollider, which is a rectangle with its corners cut (an octagon). |
||||||
|
|
||||||
|
```lua |
||||||
|
bsg_rectangle = world:newBSGRectangleCollider(100, 100, 50, 50, 5) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x` `(number)` - The x position of the rectangle's top-left corner |
||||||
|
* `y` `(number)` - The y position of the rectangle's top-left corner |
||||||
|
* `w` `(number)` - The width of the rectangle |
||||||
|
* `h` `(number)` - The height of the rectangle |
||||||
|
* `corner_cut_size` `(number)` - The corner cut size |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created BSGRectangleCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newPolygonCollider(vertices)` |
||||||
|
|
||||||
|
Creates a new PolygonCollider. |
||||||
|
|
||||||
|
```lua |
||||||
|
polygon = world:newPolygonCollider({10, 10, 10, 20, 20, 20, 20, 10}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `vertices` `(table[number])` - The polygon vertices as a table of numbers |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created PolygonCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newLineCollider(x1, y1, x2, y2)` |
||||||
|
|
||||||
|
Creates a new LineCollider. |
||||||
|
|
||||||
|
```lua |
||||||
|
line = world:newLineCollider(100, 100, 200, 200) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x1` `(number)` - The x position of the first point of the line |
||||||
|
* `y1` `(number)` - The y position of the first point of the line |
||||||
|
* `x2` `(number)` - The x position of the second point of the line |
||||||
|
* `y2` `(number)` - The y position of the second point of the line |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created LineCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:newChainCollider(vertices, loop)` |
||||||
|
|
||||||
|
Creates a new ChainCollider. |
||||||
|
|
||||||
|
```lua |
||||||
|
chain = world:newChainCollider({10, 10, 10, 20, 20, 20}, true) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `vertices` `(table[number])` - The chain vertices as a table of numbers |
||||||
|
* `loop` `(boolean)` - If the chain should loop back from the last to the first point |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `Collider` `(table)` - The newly created ChainCollider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:queryCircleArea(x, y, r, collision_class_names)` |
||||||
|
|
||||||
|
Queries a circular area around a point for colliders. |
||||||
|
|
||||||
|
```lua |
||||||
|
colliders_1 = world:queryCircleArea(100, 100, 50, {'Enemy', 'NPC'}) |
||||||
|
colliders_2 = world:queryCircleArea(100, 100, 50, {'All', except = {'Player'}}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x` `(number)` - The x position of the circle's center |
||||||
|
* `y` `(number)` - The y position of the circle's center |
||||||
|
* `r` `(number)` - The radius of the circle |
||||||
|
* `[collision_class_names='All']` `(table[string])` - A table of strings with collision class names to be queried. The special value `'All'` (default) can be used to query for all existing collision classes. Another special value `except` can be used to exclude some collision classes when `'All'` is used. |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `table[Collider]` - The table of colliders with the specified collision classes inside the area |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:queryRectangleArea(x, y, w, h, collision_class_names)` |
||||||
|
|
||||||
|
Queries a rectangular area for colliders. |
||||||
|
|
||||||
|
```lua |
||||||
|
colliders_1 = world:queryRectangleArea(100, 100, 50, 50, {'Enemy', 'NPC'}) |
||||||
|
colliders_2 = world:queryRectangleArea(100, 100, 50, 50, {'All', except = {'Player'}}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x` `(number)` - The x position of the rectangle's top-left corner |
||||||
|
* `y` `(number)` - The y position of the rectangle's top-left corner |
||||||
|
* `w` `(number)` - The width of the rectangle |
||||||
|
* `h` `(number)` - The height of the rectangle |
||||||
|
* `[collision_class_names='All']` `(table[string])` - A table of strings with collision class names to be queried. The special value `'All'` (default) can be used to query for all existing collision classes. Another special value `except` can be used to exclude some collision classes when `'All'` is used. |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `table[Collider]` - The table of colliders with the specified collision classes inside the area |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:queryPolygonArea(vertices, collision_class_names)` |
||||||
|
|
||||||
|
Queries a polygon area for colliders. |
||||||
|
|
||||||
|
```lua |
||||||
|
colliders_1 = world:queryPolygonArea({10, 10, 20, 10, 20, 20, 10, 20}, {'Enemy'}) |
||||||
|
colliders_2 = world:queryPolygonArea({10, 10, 20, 10, 20, 20, 10, 20}, {'All', except = {'Player'}}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `vertices` `(table[number])` - The polygon vertices as a table of numbers |
||||||
|
* `[collision_class_names='All']` `(table[string])` - A table of strings with collision class names to be queried. The special value `'All'` (default) can be used to query for all existing collision classes. Another special value `except` can be used to exclude some collision classes when `'All'` is used. |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `table[Collider]` - The table of colliders with the specified collision classes inside the area |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:queryLine(x1, y1, x2, y2, collision_class_names)` |
||||||
|
|
||||||
|
Queries for colliders that intersect with a line. |
||||||
|
|
||||||
|
```lua |
||||||
|
colliders_1 = world:queryLine(100, 100, 200, 200, {'Enemy', 'NPC', 'Projectile'}) |
||||||
|
colliders_2 = world:queryLine(100, 100, 200, 200, {'All', except = {'Player'}}) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `x1` `(number)` - The x position of the first point of the line |
||||||
|
* `y1` `(number)` - The y position of the first point of the line |
||||||
|
* `x2` `(number)` - The x position of the second point of the line |
||||||
|
* `y2` `(number)` - The y position of the second point of the line |
||||||
|
* `[collision_class_names='All']` `(table[string])` - A table of strings with collision class names to be queried. The special value `'All'` (default) can be used to query for all existing collision classes. Another special value `except` can be used to exclude some collision classes when `'All'` is used. |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `table[Collider]` - The table of colliders with the specified collision classes inside the area |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:addJoint(joint_type, ...)` |
||||||
|
|
||||||
|
Adds a joint to the world. |
||||||
|
|
||||||
|
```lua |
||||||
|
joint = world:addJoint('RevoluteJoint', collider_1, collider_2, 50, 50, true) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `joint_type` `(string)` - The joint type, it can be `'DistanceJoint'`, `'FrictionJoint'`, `'GearJoint'`, `'MouseJoint'`, `'PrismaticJoint'`, `'PulleyJoint'`, `'RevoluteJoint'`, `'RopeJoint'`, `'WeldJoint'` or `'WheelJoint'` |
||||||
|
* `...` `(*)` - The joint creation arguments that are different for each joint type, check [here](https://love2d.org/wiki/Joint) for more details |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `joint` `(Joint)` - The newly created Joint |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:removeJoint(joint)` |
||||||
|
|
||||||
|
Removes a joint from the world. |
||||||
|
|
||||||
|
```lua |
||||||
|
joint = world:addJoint('RevoluteJoint', collider_1, collider_2, 50, 50, true) |
||||||
|
world:removeJoint(joint) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `joint` `(Joint)` - The joint to be removed |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setExplicitCollisionEvents(value)` |
||||||
|
|
||||||
|
Sets collision events to be explicit or not. If explicit, then collision events will only be generated between collision classes when they are specified in `addCollisionClasses`. By default this is set to false, meaning that collision events are generated between all collision classes. The main reason why you might want to set this to true is for performance, since not generating collision events between every collision class will require less computation. This function must be called before any collision class is added to the world. |
||||||
|
|
||||||
|
```lua |
||||||
|
world:setExplicitCollisionEvents(true) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `value` `(boolean)` - If collision events are explicit or not |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setQueryDebugDrawing(value)` |
||||||
|
|
||||||
|
Sets query debug drawing to be active or not. If active, then collider queries will be drawn to the screen for 10 frames. This is used for debugging purposes and incurs a performance penalty. Don't forget to turn it off! |
||||||
|
|
||||||
|
```lua |
||||||
|
world:setQueryDebugDrawing(true) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `value` `(boolean)` - If query debug drawing is active or not |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
## Collider |
||||||
|
|
||||||
|
On top of containing all functions exposed in this documentation it also contains all functions of a [Body](https://love2d.org/wiki/Body), [Fixture](https://love2d.org/wiki/Fixture) and [Shape](https://love2d.org/wiki/Shape). |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:destroy()` |
||||||
|
|
||||||
|
Destroys the collider and removes it from the world. This must be called whenever the Collider is to discarded otherwise it will result in it not getting collected (and so memory will leak). |
||||||
|
|
||||||
|
```lua |
||||||
|
collider:destroy() |
||||||
|
``` |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setCollisionClass(collision_class_name)` |
||||||
|
|
||||||
|
Sets this collider's collision class. The collision class must be a valid one previously added with `world:addCollisionClass`. |
||||||
|
|
||||||
|
```lua |
||||||
|
world:addCollisionClass('Player') |
||||||
|
collider = world:newRectangleCollider(100, 100, 50, 50) |
||||||
|
collider:setCollisionClass('Player') |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `collision_class_name` `(string)` - The name of the collision class |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:enter(other_collision_class_name)` |
||||||
|
|
||||||
|
Checks for collision enter events from this collider with another. Enter events are generated on the frame when one collider enters contact with another. |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:enter('Enemy') then |
||||||
|
print('Collision entered!') |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `boolean` - If the enter collision event between both colliders happened on this frame or not |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:getEnterCollisionData(other_collision_class_name)` |
||||||
|
|
||||||
|
Gets the collision data generated from the last collision enter event |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:enter('Enemy') then |
||||||
|
local collision_data = collider:getEnterCollisionData('Enemy') |
||||||
|
print(collision_data.collider, collision_data.contact) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `collision_data` `(table[Collider, Contact])` - A table containing the Collider and the [Contact](https://love2d.org/wiki/Contact) generated from the last enter collision event |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:exit(other_collision_class_name)` |
||||||
|
|
||||||
|
Checks for collision exit events from this collider with another. Exit events are generated on the frame when one collider exits contact with another. |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:exit('Enemy') then |
||||||
|
print('Collision exited!') |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `boolean` - If the exit collision event between both colliders happened on this frame or not |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:getExitCollisionData(other_collision_class_name)` |
||||||
|
|
||||||
|
Gets the collision data generated from the last collision exit event |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:exit('Enemy') then |
||||||
|
local collision_data = collider:getEnterCollisionData('Enemy') |
||||||
|
print(collision_data.collider, collision_data.contact) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `collision_data` `(table[Collider, Contact])` - A table containing the Collider and the [Contact](https://love2d.org/wiki/Contact) generated from the last exit collision event |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:stay(other_collision_class_name)` |
||||||
|
|
||||||
|
Checks for collision stay events from this collider with another. Stay events are generated on every frame when one collider is in contact with another. |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:stay('Enemy') then |
||||||
|
print('Collision staying!') |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `boolean` - If the stay collision event between both colliders is happening on this frame or not |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:getStayCollisionData(other_collision_class_name)` |
||||||
|
|
||||||
|
Gets the collision data generated from the last collision stay event |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in some update function |
||||||
|
if collider:stay('Enemy') then |
||||||
|
local collision_data_list = collider:getStayCollisionData('Enemy') |
||||||
|
for _, collision_data in ipairs(collision_data_list) do |
||||||
|
print(collision_data.collider, collision_data.contact) |
||||||
|
end |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `other_collision_class_name` `(string)` - The name of the target collision class |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `collision_data_list` `(table[table[Collider, Contact]])` - A table containing multiple Colliders and [Contacts](https://love2d.org/wiki/Contact) generated from the last stay collision event. Usually this list will be of size 1, but sometimes this collider will be staying in contact with multiple other colliders on the same frame, and so those multiple stay events (with multiple colliders) are returned. |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setPreSolve(callback)` |
||||||
|
|
||||||
|
Sets the preSolve callback. Unlike with `:enter` or `:exit` that can be delayed and checked after the physics simulation is done for this frame, both preSolve and postSolve must be callbacks that are resolved immediately, since they may change how the rest of the simulation plays out on this frame. |
||||||
|
|
||||||
|
```lua |
||||||
|
collider:setPreSolve(function(collider_1, collider_2, contact) |
||||||
|
contact:setEnabled(false) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `callback` `(function)` - The preSolve callback. Receives `collider_1`, `collider_2` and `contact` as arguments |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setPostSolve(callback)` |
||||||
|
|
||||||
|
Sets the postSolve callback. Unlike with `:enter` or `:exit` that can be delayed and checked after the physics simulation is done for this frame, both preSolve and postSolve must be callbacks that are resolved immediately, since they may change how the rest of the simulation plays out on this frame. |
||||||
|
|
||||||
|
```lua |
||||||
|
collider:setPostSolve(function(collider_1, collider_2, contact, ni1, ti1, ni2, ti2) |
||||||
|
contact:setEnabled(false) |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `callback` `(function)` - The postSolve callback. Receives `collider_1`, `collider_2`, `contact`, `normal_impulse1`, `tangent_impulse1`, `normal_impulse2` and `tangent_impulse2` as arguments |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:addShape(shape_name, shape_type, ...)` |
||||||
|
|
||||||
|
Adds a shape to the collider. A shape can be accessed via collider.shapes[shape_name]. A fixture of the same name is also added to attach the shape to the collider body. A fixture can be accessed via collider.fixtures[fixture_name]. |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `shape_name` `(string)` - The unique name of the shape |
||||||
|
* `shape_type` `(string)` - The shape type, can be `'ChainShape'`, `'CircleShape'`, `'EdgeShape'`, `'PolygonShape'` or `'RectangleShape'` |
||||||
|
* `...` `(*)` - The shape creation arguments that are different for each shape. Check [here](https://love2d.org/wiki/Shape) for more details |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:removeShape(shape_name)` |
||||||
|
|
||||||
|
Removes a shape from the collider (also removes the accompanying fixture). |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `shape_name` `(string)` - The unique name of the shape to be removed. Must be a name previously added with `:addShape` |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:setObject(object)` |
||||||
|
|
||||||
|
Sets the collider's object. This is useful to set to the object the collider belongs to, so that when a query call is made and colliders are returned you can immediately get the pertinent object. |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in the constructor of some object |
||||||
|
self.collider = world:newRectangleCollider(...) |
||||||
|
self.collider:setObject(self) |
||||||
|
``` |
||||||
|
|
||||||
|
Arguments: |
||||||
|
|
||||||
|
* `object` `(*)` - The object that this collider belongs to |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
#### `:getObject()` |
||||||
|
|
||||||
|
Gets the object a collider belongs to. |
||||||
|
|
||||||
|
```lua |
||||||
|
-- in an update function |
||||||
|
if self.collider:enter('Enemy') then |
||||||
|
local collision_data = self.collider:getEnterCollisionData('SomeTag') |
||||||
|
-- gets the reference to the enemy object, the enemy object must have used :setObject(self) to attach itself to the collider otherwise this wouldn't work |
||||||
|
local enemy = collision_data.collider:getObject() |
||||||
|
end |
||||||
|
``` |
||||||
|
|
||||||
|
Returns: |
||||||
|
|
||||||
|
* `object` `(*)` - The object that is attached to this collider |
||||||
|
|
||||||
|
--- |
||||||
|
|
||||||
|
# LICENSE |
||||||
|
|
||||||
|
You can do whatever you want with this. See the license at the top of the main file. |
@ -0,0 +1,193 @@ |
|||||||
|
local clock = os.clock |
||||||
|
|
||||||
|
--- Simple profiler written in Lua. |
||||||
|
-- @module profile |
||||||
|
-- @alias profile |
||||||
|
local profile = {} |
||||||
|
|
||||||
|
-- function labels |
||||||
|
local _labeled = {} |
||||||
|
-- function definitions |
||||||
|
local _defined = {} |
||||||
|
-- time of last call |
||||||
|
local _tcalled = {} |
||||||
|
-- total execution time |
||||||
|
local _telapsed = {} |
||||||
|
-- number of calls |
||||||
|
local _ncalls = {} |
||||||
|
-- list of internal profiler functions |
||||||
|
local _internal = {} |
||||||
|
|
||||||
|
--- This is an internal function. |
||||||
|
-- @tparam string event Event type |
||||||
|
-- @tparam number line Line number |
||||||
|
-- @tparam[opt] table info Debug info table |
||||||
|
function profile.hooker(event, line, info) |
||||||
|
info = info or debug.getinfo(2, 'fnS') |
||||||
|
local f = info.func |
||||||
|
-- ignore the profiler itself |
||||||
|
if _internal[f] or info.what ~= "Lua" then |
||||||
|
return |
||||||
|
end |
||||||
|
-- get the function name if available |
||||||
|
if info.name then |
||||||
|
_labeled[f] = info.name |
||||||
|
end |
||||||
|
-- find the line definition |
||||||
|
if not _defined[f] then |
||||||
|
_defined[f] = info.short_src..":"..info.linedefined |
||||||
|
_ncalls[f] = 0 |
||||||
|
_telapsed[f] = 0 |
||||||
|
end |
||||||
|
if _tcalled[f] then |
||||||
|
local dt = clock() - _tcalled[f] |
||||||
|
_telapsed[f] = _telapsed[f] + dt |
||||||
|
_tcalled[f] = nil |
||||||
|
end |
||||||
|
if event == "tail call" then |
||||||
|
local prev = debug.getinfo(3, 'fnS') |
||||||
|
profile.hooker("return", line, prev) |
||||||
|
profile.hooker("call", line, info) |
||||||
|
elseif event == 'call' then |
||||||
|
_tcalled[f] = clock() |
||||||
|
else |
||||||
|
_ncalls[f] = _ncalls[f] + 1 |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
--- Sets a clock function to be used by the profiler. |
||||||
|
-- @tparam function func Clock function that returns a number |
||||||
|
function profile.setclock(f) |
||||||
|
assert(type(f) == "function", "clock must be a function") |
||||||
|
clock = f |
||||||
|
end |
||||||
|
|
||||||
|
--- Starts collecting data. |
||||||
|
function profile.start() |
||||||
|
if rawget(_G, 'jit') then |
||||||
|
jit.off() |
||||||
|
jit.flush() |
||||||
|
end |
||||||
|
debug.sethook(profile.hooker, "cr") |
||||||
|
end |
||||||
|
|
||||||
|
--- Stops collecting data. |
||||||
|
function profile.stop() |
||||||
|
debug.sethook() |
||||||
|
for f in pairs(_tcalled) do |
||||||
|
local dt = clock() - _tcalled[f] |
||||||
|
_telapsed[f] = _telapsed[f] + dt |
||||||
|
_tcalled[f] = nil |
||||||
|
end |
||||||
|
-- merge closures |
||||||
|
local lookup = {} |
||||||
|
for f, d in pairs(_defined) do |
||||||
|
local id = (_labeled[f] or '?')..d |
||||||
|
local f2 = lookup[id] |
||||||
|
if f2 then |
||||||
|
_ncalls[f2] = _ncalls[f2] + (_ncalls[f] or 0) |
||||||
|
_telapsed[f2] = _telapsed[f2] + (_telapsed[f] or 0) |
||||||
|
_defined[f], _labeled[f] = nil, nil |
||||||
|
_ncalls[f], _telapsed[f] = nil, nil |
||||||
|
else |
||||||
|
lookup[id] = f |
||||||
|
end |
||||||
|
end |
||||||
|
collectgarbage('collect') |
||||||
|
end |
||||||
|
|
||||||
|
--- Resets all collected data. |
||||||
|
function profile.reset() |
||||||
|
for f in pairs(_ncalls) do |
||||||
|
_ncalls[f] = 0 |
||||||
|
end |
||||||
|
for f in pairs(_telapsed) do |
||||||
|
_telapsed[f] = 0 |
||||||
|
end |
||||||
|
for f in pairs(_tcalled) do |
||||||
|
_tcalled[f] = nil |
||||||
|
end |
||||||
|
collectgarbage('collect') |
||||||
|
end |
||||||
|
|
||||||
|
--- This is an internal function. |
||||||
|
-- @tparam function a First function |
||||||
|
-- @tparam function b Second function |
||||||
|
-- @treturn boolean True if "a" should rank higher than "b" |
||||||
|
function profile.comp(a, b) |
||||||
|
local dt = _telapsed[b] - _telapsed[a] |
||||||
|
if dt == 0 then |
||||||
|
return _ncalls[b] < _ncalls[a] |
||||||
|
end |
||||||
|
return dt < 0 |
||||||
|
end |
||||||
|
|
||||||
|
--- Generates a report of functions that have been called since the profile was started. |
||||||
|
-- Returns the report as a numeric table of rows containing the rank, function label, number of calls, total execution time and source code line number. |
||||||
|
-- @tparam[opt] number limit Maximum number of rows |
||||||
|
-- @treturn table Table of rows |
||||||
|
function profile.query(limit) |
||||||
|
local t = {} |
||||||
|
for f, n in pairs(_ncalls) do |
||||||
|
if n > 0 then |
||||||
|
t[#t + 1] = f |
||||||
|
end |
||||||
|
end |
||||||
|
table.sort(t, profile.comp) |
||||||
|
if limit then |
||||||
|
while #t > limit do |
||||||
|
table.remove(t) |
||||||
|
end |
||||||
|
end |
||||||
|
for i, f in ipairs(t) do |
||||||
|
local dt = 0 |
||||||
|
if _tcalled[f] then |
||||||
|
dt = clock() - _tcalled[f] |
||||||
|
end |
||||||
|
t[i] = { i, _labeled[f] or '?', _ncalls[f], _telapsed[f] + dt, _defined[f] } |
||||||
|
end |
||||||
|
return t |
||||||
|
end |
||||||
|
|
||||||
|
local cols = { 3, 29, 11, 24, 32 } |
||||||
|
|
||||||
|
--- Generates a text report of functions that have been called since the profile was started. |
||||||
|
-- Returns the report as a string that can be printed to the console. |
||||||
|
-- @tparam[opt] number limit Maximum number of rows |
||||||
|
-- @treturn string Text-based profiling report |
||||||
|
function profile.report(n) |
||||||
|
local out = {} |
||||||
|
local report = profile.query(n) |
||||||
|
for i, row in ipairs(report) do |
||||||
|
for j = 1, 5 do |
||||||
|
local s = row[j] |
||||||
|
local l2 = cols[j] |
||||||
|
s = tostring(s) |
||||||
|
local l1 = s:len() |
||||||
|
if l1 < l2 then |
||||||
|
s = s..(' '):rep(l2-l1) |
||||||
|
elseif l1 > l2 then |
||||||
|
s = s:sub(l1 - l2 + 1, l1) |
||||||
|
end |
||||||
|
row[j] = s |
||||||
|
end |
||||||
|
out[i] = table.concat(row, ' | ') |
||||||
|
end |
||||||
|
|
||||||
|
local row = " +-----+-------------------------------+-------------+--------------------------+----------------------------------+ \n" |
||||||
|
local col = " | # | Function | Calls | Time | Code | \n" |
||||||
|
local sz = row..col..row |
||||||
|
if #out > 0 then |
||||||
|
sz = sz..' | '..table.concat(out, ' | \n | ')..' | \n' |
||||||
|
end |
||||||
|
return '\n'..sz..row |
||||||
|
end |
||||||
|
|
||||||
|
-- store all internal profiler functions |
||||||
|
for _, v in pairs(profile) do |
||||||
|
if type(v) == "function" then |
||||||
|
_internal[v] = true |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
return profile |
@ -0,0 +1,929 @@ |
|||||||
|
--[[ |
||||||
|
The MIT License (MIT) |
||||||
|
|
||||||
|
Copyright (c) 2018 SSYGEN |
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
||||||
|
of this software and associated documentation files (the "Software"), to deal |
||||||
|
in the Software without restriction, including without limitation the rights |
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||||||
|
copies of the Software, and to permit persons to whom the Software is |
||||||
|
furnished to do so, subject to the following conditions: |
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all |
||||||
|
copies or substantial portions of the Software. |
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||||||
|
SOFTWARE. |
||||||
|
]]-- |
||||||
|
|
||||||
|
local path = ... .. '.' |
||||||
|
local wf = {} |
||||||
|
wf.Math = require(path .. 'mlib.mlib') |
||||||
|
|
||||||
|
World = {} |
||||||
|
World.__index = World |
||||||
|
|
||||||
|
function wf.newWorld(xg, yg, sleep) |
||||||
|
local world = wf.World.new(wf, xg, yg, sleep) |
||||||
|
|
||||||
|
world.box2d_world:setCallbacks(world.collisionOnEnter, world.collisionOnExit, world.collisionPre, world.collisionPost) |
||||||
|
world:collisionClear() |
||||||
|
world:addCollisionClass('Default') |
||||||
|
|
||||||
|
-- Points all box2d_world functions to this wf.World object |
||||||
|
-- This means that the user can call world:setGravity for instance without having to say world.box2d_world:setGravity |
||||||
|
for k, v in pairs(world.box2d_world.__index) do |
||||||
|
if k ~= '__gc' and k ~= '__eq' and k ~= '__index' and k ~= '__tostring' and k ~= 'update' and k ~= 'destroy' and k ~= 'type' and k ~= 'typeOf' then |
||||||
|
world[k] = function(self, ...) |
||||||
|
return v(self.box2d_world, ...) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
return world |
||||||
|
end |
||||||
|
|
||||||
|
function World.new(wf, xg, yg, sleep) |
||||||
|
local self = {} |
||||||
|
local settings = settings or {} |
||||||
|
self.wf = wf |
||||||
|
|
||||||
|
self.draw_query_for_n_frames = 10 |
||||||
|
self.query_debug_drawing_enabled = false |
||||||
|
self.explicit_collision_events = false |
||||||
|
self.collision_classes = {} |
||||||
|
self.masks = {} |
||||||
|
self.is_sensor_memo = {} |
||||||
|
self.query_debug_draw = {} |
||||||
|
|
||||||
|
love.physics.setMeter(32) |
||||||
|
self.box2d_world = love.physics.newWorld(xg, yg, sleep) |
||||||
|
|
||||||
|
return setmetatable(self, World) |
||||||
|
end |
||||||
|
|
||||||
|
function World:update(dt) |
||||||
|
self:collisionEventsClear() |
||||||
|
self.box2d_world:update(dt) |
||||||
|
end |
||||||
|
|
||||||
|
function World:draw(alpha) |
||||||
|
-- get the current color values to reapply |
||||||
|
local r, g, b, a = love.graphics.getColor() |
||||||
|
-- alpha value is optional |
||||||
|
alpha = alpha or 255 |
||||||
|
-- Colliders debug |
||||||
|
love.graphics.setColor(222, 222, 222, alpha) |
||||||
|
local bodies = self.box2d_world:getBodies() |
||||||
|
for _, body in ipairs(bodies) do |
||||||
|
local fixtures = body:getFixtures() |
||||||
|
for _, fixture in ipairs(fixtures) do |
||||||
|
if fixture:getShape():type() == 'PolygonShape' then |
||||||
|
love.graphics.polygon('line', body:getWorldPoints(fixture:getShape():getPoints())) |
||||||
|
elseif fixture:getShape():type() == 'EdgeShape' or fixture:getShape():type() == 'ChainShape' then |
||||||
|
local points = {body:getWorldPoints(fixture:getShape():getPoints())} |
||||||
|
for i = 1, #points, 2 do |
||||||
|
if i < #points-2 then love.graphics.line(points[i], points[i+1], points[i+2], points[i+3]) end |
||||||
|
end |
||||||
|
elseif fixture:getShape():type() == 'CircleShape' then |
||||||
|
local body_x, body_y = body:getPosition() |
||||||
|
local shape_x, shape_y = fixture:getShape():getPoint() |
||||||
|
local r = fixture:getShape():getRadius() |
||||||
|
love.graphics.circle('line', body_x + shape_x, body_y + shape_y, r, 360) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
love.graphics.setColor(255, 255, 255, alpha) |
||||||
|
|
||||||
|
-- Joint debug |
||||||
|
love.graphics.setColor(222, 128, 64, alpha) |
||||||
|
local joints = self.box2d_world:getJoints() |
||||||
|
for _, joint in ipairs(joints) do |
||||||
|
local x1, y1, x2, y2 = joint:getAnchors() |
||||||
|
if x1 and y1 then love.graphics.circle('line', x1, y1, 4) end |
||||||
|
if x2 and y2 then love.graphics.circle('line', x2, y2, 4) end |
||||||
|
end |
||||||
|
love.graphics.setColor(255, 255, 255, alpha) |
||||||
|
|
||||||
|
-- Query debug |
||||||
|
love.graphics.setColor(64, 64, 222, alpha) |
||||||
|
for _, query_draw in ipairs(self.query_debug_draw) do |
||||||
|
query_draw.frames = query_draw.frames - 1 |
||||||
|
if query_draw.type == 'circle' then |
||||||
|
love.graphics.circle('line', query_draw.x, query_draw.y, query_draw.r) |
||||||
|
elseif query_draw.type == 'rectangle' then |
||||||
|
love.graphics.rectangle('line', query_draw.x, query_draw.y, query_draw.w, query_draw.h) |
||||||
|
elseif query_draw.type == 'line' then |
||||||
|
love.graphics.line(query_draw.x1, query_draw.y1, query_draw.x2, query_draw.y2) |
||||||
|
elseif query_draw.type == 'polygon' then |
||||||
|
local triangles = love.math.triangulate(query_draw.vertices) |
||||||
|
for _, triangle in ipairs(triangles) do love.graphics.polygon('line', triangle) end |
||||||
|
end |
||||||
|
end |
||||||
|
for i = #self.query_debug_draw, 1, -1 do |
||||||
|
if self.query_debug_draw[i].frames <= 0 then |
||||||
|
table.remove(self.query_debug_draw, i) |
||||||
|
end |
||||||
|
end |
||||||
|
love.graphics.setColor(r, g, b, a) |
||||||
|
end |
||||||
|
|
||||||
|
function World:setQueryDebugDrawing(value) |
||||||
|
self.query_debug_drawing_enabled = value |
||||||
|
end |
||||||
|
|
||||||
|
function World:setExplicitCollisionEvents(value) |
||||||
|
self.explicit_collision_events = value |
||||||
|
end |
||||||
|
|
||||||
|
function World:addCollisionClass(collision_class_name, collision_class) |
||||||
|
if self.collision_classes[collision_class_name] then error('Collision class ' .. collision_class_name .. ' already exists.') end |
||||||
|
|
||||||
|
if self.explicit_collision_events then |
||||||
|
self.collision_classes[collision_class_name] = collision_class or {} |
||||||
|
else |
||||||
|
self.collision_classes[collision_class_name] = collision_class or {} |
||||||
|
self.collision_classes[collision_class_name].enter = {} |
||||||
|
self.collision_classes[collision_class_name].exit = {} |
||||||
|
self.collision_classes[collision_class_name].pre = {} |
||||||
|
self.collision_classes[collision_class_name].post = {} |
||||||
|
for c_class_name, _ in pairs(self.collision_classes) do |
||||||
|
table.insert(self.collision_classes[collision_class_name].enter, c_class_name) |
||||||
|
table.insert(self.collision_classes[collision_class_name].exit, c_class_name) |
||||||
|
table.insert(self.collision_classes[collision_class_name].pre, c_class_name) |
||||||
|
table.insert(self.collision_classes[collision_class_name].post, c_class_name) |
||||||
|
end |
||||||
|
for c_class_name, _ in pairs(self.collision_classes) do |
||||||
|
table.insert(self.collision_classes[c_class_name].enter, collision_class_name) |
||||||
|
table.insert(self.collision_classes[c_class_name].exit, collision_class_name) |
||||||
|
table.insert(self.collision_classes[c_class_name].pre, collision_class_name) |
||||||
|
table.insert(self.collision_classes[c_class_name].post, collision_class_name) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
self:collisionClassesSet() |
||||||
|
end |
||||||
|
|
||||||
|
function World:collisionClassesSet() |
||||||
|
self:generateCategoriesMasks() |
||||||
|
|
||||||
|
self:collisionClear() |
||||||
|
local collision_table = self:getCollisionCallbacksTable() |
||||||
|
for collision_class_name, collision_list in pairs(collision_table) do |
||||||
|
for _, collision_info in ipairs(collision_list) do |
||||||
|
if collision_info.type == 'enter' then self:addCollisionEnter(collision_class_name, collision_info.other) end |
||||||
|
if collision_info.type == 'exit' then self:addCollisionExit(collision_class_name, collision_info.other) end |
||||||
|
if collision_info.type == 'pre' then self:addCollisionPre(collision_class_name, collision_info.other) end |
||||||
|
if collision_info.type == 'post' then self:addCollisionPost(collision_class_name, collision_info.other) end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
self:collisionEventsClear() |
||||||
|
end |
||||||
|
|
||||||
|
function World:collisionClear() |
||||||
|
self.collisions = {} |
||||||
|
self.collisions.on_enter = {} |
||||||
|
self.collisions.on_enter.sensor = {} |
||||||
|
self.collisions.on_enter.non_sensor = {} |
||||||
|
self.collisions.on_exit = {} |
||||||
|
self.collisions.on_exit.sensor = {} |
||||||
|
self.collisions.on_exit.non_sensor = {} |
||||||
|
self.collisions.pre = {} |
||||||
|
self.collisions.pre.sensor = {} |
||||||
|
self.collisions.pre.non_sensor = {} |
||||||
|
self.collisions.post = {} |
||||||
|
self.collisions.post.sensor = {} |
||||||
|
self.collisions.post.non_sensor = {} |
||||||
|
end |
||||||
|
|
||||||
|
function World:collisionEventsClear() |
||||||
|
local bodies = self.box2d_world:getBodies() |
||||||
|
for _, body in ipairs(bodies) do |
||||||
|
local collider = body:getFixtures()[1]:getUserData() |
||||||
|
collider:collisionEventsClear() |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World:addCollisionEnter(type1, type2) |
||||||
|
if not self:isCollisionBetweenSensors(type1, type2) then |
||||||
|
table.insert(self.collisions.on_enter.non_sensor, {type1 = type1, type2 = type2}) |
||||||
|
else table.insert(self.collisions.on_enter.sensor, {type1 = type1, type2 = type2}) end |
||||||
|
end |
||||||
|
|
||||||
|
function World:addCollisionExit(type1, type2) |
||||||
|
if not self:isCollisionBetweenSensors(type1, type2) then |
||||||
|
table.insert(self.collisions.on_exit.non_sensor, {type1 = type1, type2 = type2}) |
||||||
|
else table.insert(self.collisions.on_exit.sensor, {type1 = type1, type2 = type2}) end |
||||||
|
end |
||||||
|
|
||||||
|
function World:addCollisionPre(type1, type2) |
||||||
|
if not self:isCollisionBetweenSensors(type1, type2) then |
||||||
|
table.insert(self.collisions.pre.non_sensor, {type1 = type1, type2 = type2}) |
||||||
|
else table.insert(self.collisions.pre.sensor, {type1 = type1, type2 = type2}) end |
||||||
|
end |
||||||
|
|
||||||
|
function World:addCollisionPost(type1, type2) |
||||||
|
if not self:isCollisionBetweenSensors(type1, type2) then |
||||||
|
table.insert(self.collisions.post.non_sensor, {type1 = type1, type2 = type2}) |
||||||
|
else table.insert(self.collisions.post.sensor, {type1 = type1, type2 = type2}) end |
||||||
|
end |
||||||
|
|
||||||
|
function World:doesType1IgnoreType2(type1, type2) |
||||||
|
local collision_ignores = {} |
||||||
|
for collision_class_name, collision_class in pairs(self.collision_classes) do |
||||||
|
collision_ignores[collision_class_name] = collision_class.ignores or {} |
||||||
|
end |
||||||
|
local all = {} |
||||||
|
for collision_class_name, _ in pairs(collision_ignores) do |
||||||
|
table.insert(all, collision_class_name) |
||||||
|
end |
||||||
|
local ignored_types = {} |
||||||
|
for _, collision_class_type in ipairs(collision_ignores[type1]) do |
||||||
|
if collision_class_type == 'All' then |
||||||
|
for _, collision_class_name in ipairs(all) do |
||||||
|
table.insert(ignored_types, collision_class_name) |
||||||
|
end |
||||||
|
else table.insert(ignored_types, collision_class_type) end |
||||||
|
end |
||||||
|
for key, _ in pairs(collision_ignores[type1]) do |
||||||
|
if key == 'except' then |
||||||
|
for _, except_type in ipairs(collision_ignores[type1].except) do |
||||||
|
for i = #ignored_types, 1, -1 do |
||||||
|
if ignored_types[i] == except_type then table.remove(ignored_types, i) end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
for _, ignored_type in ipairs(ignored_types) do |
||||||
|
if ignored_type == type2 then return true end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World:isCollisionBetweenSensors(type1, type2) |
||||||
|
if not self.is_sensor_memo[type1] then self.is_sensor_memo[type1] = {} end |
||||||
|
if not self.is_sensor_memo[type1][type2] then self.is_sensor_memo[type1][type2] = (self:doesType1IgnoreType2(type1, type2) or self:doesType1IgnoreType2(type2, type1)) end |
||||||
|
if self.is_sensor_memo[type1][type2] then return true |
||||||
|
else return false end |
||||||
|
end |
||||||
|
|
||||||
|
-- https://love2d.org/forums/viewtopic.php?f=4&t=75441 |
||||||
|
function World:generateCategoriesMasks() |
||||||
|
local collision_ignores = {} |
||||||
|
for collision_class_name, collision_class in pairs(self.collision_classes) do |
||||||
|
collision_ignores[collision_class_name] = collision_class.ignores or {} |
||||||
|
end |
||||||
|
local incoming = {} |
||||||
|
local expanded = {} |
||||||
|
local all = {} |
||||||
|
for object_type, _ in pairs(collision_ignores) do |
||||||
|
incoming[object_type] = {} |
||||||
|
expanded[object_type] = {} |
||||||
|
table.insert(all, object_type) |
||||||
|
end |
||||||
|
for object_type, ignore_list in pairs(collision_ignores) do |
||||||
|
for key, ignored_type in pairs(ignore_list) do |
||||||
|
if ignored_type == 'All' then |
||||||
|
for _, all_object_type in ipairs(all) do |
||||||
|
table.insert(incoming[all_object_type], object_type) |
||||||
|
table.insert(expanded[object_type], all_object_type) |
||||||
|
end |
||||||
|
elseif type(ignored_type) == 'string' then |
||||||
|
if ignored_type ~= 'All' then |
||||||
|
table.insert(incoming[ignored_type], object_type) |
||||||
|
table.insert(expanded[object_type], ignored_type) |
||||||
|
end |
||||||
|
end |
||||||
|
if key == 'except' then |
||||||
|
for _, except_ignored_type in ipairs(ignored_type) do |
||||||
|
for i, v in ipairs(incoming[except_ignored_type]) do |
||||||
|
if v == object_type then |
||||||
|
table.remove(incoming[except_ignored_type], i) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
for _, except_ignored_type in ipairs(ignored_type) do |
||||||
|
for i, v in ipairs(expanded[object_type]) do |
||||||
|
if v == except_ignored_type then |
||||||
|
table.remove(expanded[object_type], i) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
local edge_groups = {} |
||||||
|
for k, v in pairs(incoming) do |
||||||
|
table.sort(v, function(a, b) return string.lower(a) < string.lower(b) end) |
||||||
|
end |
||||||
|
local i = 0 |
||||||
|
for k, v in pairs(incoming) do |
||||||
|
local str = "" |
||||||
|
for _, c in ipairs(v) do |
||||||
|
str = str .. c |
||||||
|
end |
||||||
|
if not edge_groups[str] then i = i + 1; edge_groups[str] = {n = i} end |
||||||
|
table.insert(edge_groups[str], k) |
||||||
|
end |
||||||
|
local categories = {} |
||||||
|
for k, _ in pairs(collision_ignores) do |
||||||
|
categories[k] = {} |
||||||
|
end |
||||||
|
for k, v in pairs(edge_groups) do |
||||||
|
for i, c in ipairs(v) do |
||||||
|
categories[c] = v.n |
||||||
|
end |
||||||
|
end |
||||||
|
for k, v in pairs(expanded) do |
||||||
|
local category = {categories[k]} |
||||||
|
local current_masks = {} |
||||||
|
for _, c in ipairs(v) do |
||||||
|
table.insert(current_masks, categories[c]) |
||||||
|
end |
||||||
|
self.masks[k] = {categories = category, masks = current_masks} |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World:getCollisionCallbacksTable() |
||||||
|
local collision_table = {} |
||||||
|
for collision_class_name, collision_class in pairs(self.collision_classes) do |
||||||
|
collision_table[collision_class_name] = {} |
||||||
|
for _, v in ipairs(collision_class.enter or {}) do table.insert(collision_table[collision_class_name], {type = 'enter', other = v}) end |
||||||
|
for _, v in ipairs(collision_class.exit or {}) do table.insert(collision_table[collision_class_name], {type = 'exit', other = v}) end |
||||||
|
for _, v in ipairs(collision_class.pre or {}) do table.insert(collision_table[collision_class_name], {type = 'pre', other = v}) end |
||||||
|
for _, v in ipairs(collision_class.post or {}) do table.insert(collision_table[collision_class_name], {type = 'post', other = v}) end |
||||||
|
end |
||||||
|
return collision_table |
||||||
|
end |
||||||
|
|
||||||
|
local function collEnsure(collision_class_name1, a, collision_class_name2, b) |
||||||
|
if a.collision_class == collision_class_name2 and b.collision_class == collision_class_name1 then return b, a |
||||||
|
else return a, b end |
||||||
|
end |
||||||
|
|
||||||
|
local function collIf(collision_class_name1, collision_class_name2, a, b) |
||||||
|
if (a.collision_class == collision_class_name1 and b.collision_class == collision_class_name2) or |
||||||
|
(a.collision_class == collision_class_name2 and b.collision_class == collision_class_name1) then |
||||||
|
return true |
||||||
|
else return false end |
||||||
|
end |
||||||
|
|
||||||
|
function World.collisionOnEnter(fixture_a, fixture_b, contact) |
||||||
|
local a, b = fixture_a:getUserData(), fixture_b:getUserData() |
||||||
|
|
||||||
|
if fixture_a:isSensor() and fixture_b:isSensor() then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.on_enter.sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
table.insert(a.collision_events[collision.type2], {collision_type = 'enter', collider_1 = a, collider_2 = b, contact = contact}) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
table.insert(b.collision_events[collision.type1], {collision_type = 'enter', collider_1 = b, collider_2 = a, contact = contact}) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
elseif not (fixture_a:isSensor() or fixture_b:isSensor()) then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.on_enter.non_sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
table.insert(a.collision_events[collision.type2], {collision_type = 'enter', collider_1 = a, collider_2 = b, contact = contact}) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
table.insert(b.collision_events[collision.type1], {collision_type = 'enter', collider_1 = b, collider_2 = a, contact = contact}) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World.collisionOnExit(fixture_a, fixture_b, contact) |
||||||
|
local a, b = fixture_a:getUserData(), fixture_b:getUserData() |
||||||
|
|
||||||
|
if fixture_a:isSensor() and fixture_b:isSensor() then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.on_exit.sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
table.insert(a.collision_events[collision.type2], {collision_type = 'exit', collider_1 = a, collider_2 = b, contact = contact}) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
table.insert(b.collision_events[collision.type1], {collision_type = 'exit', collider_1 = b, collider_2 = a, contact = contact}) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
elseif not (fixture_a:isSensor() or fixture_b:isSensor()) then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.on_exit.non_sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
table.insert(a.collision_events[collision.type2], {collision_type = 'exit', collider_1 = a, collider_2 = b, contact = contact}) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
table.insert(b.collision_events[collision.type1], {collision_type = 'exit', collider_1 = b, collider_2 = a, contact = contact}) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World.collisionPre(fixture_a, fixture_b, contact) |
||||||
|
local a, b = fixture_a:getUserData(), fixture_b:getUserData() |
||||||
|
|
||||||
|
if fixture_a:isSensor() and fixture_b:isSensor() then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.pre.sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
a:preSolve(b, contact) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
b:preSolve(a, contact) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
elseif not (fixture_a:isSensor() or fixture_b:isSensor()) then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.pre.non_sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
a:preSolve(b, contact) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
b:preSolve(a, contact) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World.collisionPost(fixture_a, fixture_b, contact, ni1, ti1, ni2, ti2) |
||||||
|
local a, b = fixture_a:getUserData(), fixture_b:getUserData() |
||||||
|
|
||||||
|
if fixture_a:isSensor() and fixture_b:isSensor() then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.post.sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
a:postSolve(b, contact, ni1, ti1, ni2, ti2) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
b:postSolve(a, contact, ni1, ti1, ni2, ti2) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
elseif not (fixture_a:isSensor() or fixture_b:isSensor()) then |
||||||
|
if a and b then |
||||||
|
for _, collision in ipairs(a.world.collisions.post.non_sensor) do |
||||||
|
if collIf(collision.type1, collision.type2, a, b) then |
||||||
|
a, b = collEnsure(collision.type1, a, collision.type2, b) |
||||||
|
a:postSolve(b, contact, ni1, ti1, ni2, ti2) |
||||||
|
if collision.type1 == collision.type2 then |
||||||
|
b:postSolve(a, contact, ni1, ti1, ni2, ti2) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World:newCircleCollider(x, y, r, settings) |
||||||
|
return self.wf.Collider.new(self, 'Circle', x, y, r, settings) |
||||||
|
end |
||||||
|
|
||||||
|
function World:newRectangleCollider(x, y, w, h, settings) |
||||||
|
return self.wf.Collider.new(self, 'Rectangle', x, y, w, h, settings) |
||||||
|
end |
||||||
|
|
||||||
|
function World:newBSGRectangleCollider(x, y, w, h, corner_cut_size, settings) |
||||||
|
return self.wf.Collider.new(self, 'BSGRectangle', x, y, w, h, corner_cut_size, settings) |
||||||
|
end |
||||||
|
|
||||||
|
function World:newPolygonCollider(vertices, settings) |
||||||
|
return self.wf.Collider.new(self, 'Polygon', vertices, settings) |
||||||
|
end |
||||||
|
|
||||||
|
function World:newLineCollider(x1, y1, x2, y2, settings) |
||||||
|
return self.wf.Collider.new(self, 'Line', x1, y1, x2, y2, settings) |
||||||
|
end |
||||||
|
|
||||||
|
function World:newChainCollider(vertices, loop, settings) |
||||||
|
return self.wf.Collider.new(self, 'Chain', vertices, loop, settings) |
||||||
|
end |
||||||
|
|
||||||
|
-- Internal AABB box2d query used before going for more specific and precise computations. |
||||||
|
function World:_queryBoundingBox(x1, y1, x2, y2) |
||||||
|
local colliders = {} |
||||||
|
local callback = function(fixture) |
||||||
|
if not fixture:isSensor() then table.insert(colliders, fixture:getUserData()) end |
||||||
|
return true |
||||||
|
end |
||||||
|
self.box2d_world:queryBoundingBox(x1, y1, x2, y2, callback) |
||||||
|
return colliders |
||||||
|
end |
||||||
|
|
||||||
|
function World:collisionClassInCollisionClassesList(collision_class, collision_classes) |
||||||
|
if collision_classes[1] == 'All' then |
||||||
|
local all_collision_classes = {} |
||||||
|
for class, _ in pairs(self.collision_classes) do |
||||||
|
table.insert(all_collision_classes, class) |
||||||
|
end |
||||||
|
if collision_classes.except then |
||||||
|
for _, except in ipairs(collision_classes.except) do |
||||||
|
for i, class in ipairs(all_collision_classes) do |
||||||
|
if class == except then |
||||||
|
table.remove(all_collision_classes, i) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
for _, class in ipairs(all_collision_classes) do |
||||||
|
if class == collision_class then return true end |
||||||
|
end |
||||||
|
else |
||||||
|
for _, class in ipairs(collision_classes) do |
||||||
|
if class == collision_class then return true end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function World:queryCircleArea(x, y, radius, collision_class_names) |
||||||
|
if not collision_class_names then collision_class_names = {'All'} end |
||||||
|
if self.query_debug_drawing_enabled then table.insert(self.query_debug_draw, {type = 'circle', x = x, y = y, r = radius, frames = self.draw_query_for_n_frames}) end |
||||||
|
|
||||||
|
local colliders = self:_queryBoundingBox(x-radius, y-radius, x+radius, y+radius) |
||||||
|
local outs = {} |
||||||
|
for _, collider in ipairs(colliders) do |
||||||
|
if self:collisionClassInCollisionClassesList(collider.collision_class, collision_class_names) then |
||||||
|
for _, fixture in ipairs(collider.body:getFixtures()) do |
||||||
|
if self.wf.Math.polygon.getCircleIntersection(x, y, radius, {collider.body:getWorldPoints(fixture:getShape():getPoints())}) then |
||||||
|
table.insert(outs, collider) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
return outs |
||||||
|
end |
||||||
|
|
||||||
|
function World:queryRectangleArea(x, y, w, h, collision_class_names) |
||||||
|
if not collision_class_names then collision_class_names = {'All'} end |
||||||
|
if self.query_debug_drawing_enabled then table.insert(self.query_debug_draw, {type = 'rectangle', x = x, y = y, w = w, h = h, frames = self.draw_query_for_n_frames}) end |
||||||
|
|
||||||
|
local colliders = self:_queryBoundingBox(x, y, x+w, y+h) |
||||||
|
local outs = {} |
||||||
|
for _, collider in ipairs(colliders) do |
||||||
|
if self:collisionClassInCollisionClassesList(collider.collision_class, collision_class_names) then |
||||||
|
for _, fixture in ipairs(collider.body:getFixtures()) do |
||||||
|
if self.wf.Math.polygon.isPolygonInside({x, y, x+w, y, x+w, y+h, x, y+h}, {collider.body:getWorldPoints(fixture:getShape():getPoints())}) then |
||||||
|
table.insert(outs, collider) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
return outs |
||||||
|
end |
||||||
|
|
||||||
|
function World:queryPolygonArea(vertices, collision_class_names) |
||||||
|
if not collision_class_names then collision_class_names = {'All'} end |
||||||
|
if self.query_debug_drawing_enabled then table.insert(self.query_debug_draw, {type = 'polygon', vertices = vertices, frames = self.draw_query_for_n_frames}) end |
||||||
|
|
||||||
|
local cx, cy = self.wf.Math.polygon.getCentroid(vertices) |
||||||
|
local d_max = 0 |
||||||
|
for i = 1, #vertices, 2 do |
||||||
|
local d = self.wf.Math.line.getLength(cx, cy, vertices[i], vertices[i+1]) |
||||||
|
if d > d_max then d_max = d end |
||||||
|
end |
||||||
|
local colliders = self:_queryBoundingBox(cx-d_max, cy-d_max, cx+d_max, cy+d_max) |
||||||
|
local outs = {} |
||||||
|
for _, collider in ipairs(colliders) do |
||||||
|
if self:collisionClassInCollisionClassesList(collider.collision_class, collision_class_names) then |
||||||
|
for _, fixture in ipairs(collider.body:getFixtures()) do |
||||||
|
if self.wf.Math.polygon.isPolygonInside(vertices, {collider.body:getWorldPoints(fixture:getShape():getPoints())}) then |
||||||
|
table.insert(outs, collider) |
||||||
|
break |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
return outs |
||||||
|
end |
||||||
|
|
||||||
|
function World:queryLine(x1, y1, x2, y2, collision_class_names) |
||||||
|
if not collision_class_names then collision_class_names = {'All'} end |
||||||
|
if self.query_debug_drawing_enabled then |
||||||
|
table.insert(self.query_debug_draw, {type = 'line', x1 = x1, y1 = y1, x2 = x2, y2 = y2, frames = self.draw_query_for_n_frames}) |
||||||
|
end |
||||||
|
|
||||||
|
local colliders = {} |
||||||
|
local callback = function(fixture, ...) |
||||||
|
if not fixture:isSensor() then table.insert(colliders, fixture:getUserData()) end |
||||||
|
return 1 |
||||||
|
end |
||||||
|
self.box2d_world:rayCast(x1, y1, x2, y2, callback) |
||||||
|
|
||||||
|
local outs = {} |
||||||
|
for _, collider in ipairs(colliders) do |
||||||
|
if self:collisionClassInCollisionClassesList(collider.collision_class, collision_class_names) then |
||||||
|
table.insert(outs, collider) |
||||||
|
end |
||||||
|
end |
||||||
|
return outs |
||||||
|
end |
||||||
|
|
||||||
|
function World:addJoint(joint_type, ...) |
||||||
|
local args = {...} |
||||||
|
if args[1].body then args[1] = args[1].body end |
||||||
|
if type(args[2]) == "table" and args[2].body then args[2] = args[2].body end |
||||||
|
local joint = love.physics['new' .. joint_type](unpack(args)) |
||||||
|
return joint |
||||||
|
end |
||||||
|
|
||||||
|
function World:removeJoint(joint) |
||||||
|
joint:destroy() |
||||||
|
end |
||||||
|
|
||||||
|
function World:destroy() |
||||||
|
local bodies = self.box2d_world:getBodies() |
||||||
|
for _, body in ipairs(bodies) do |
||||||
|
local collider = body:getFixtures()[1]:getUserData() |
||||||
|
collider:destroy() |
||||||
|
end |
||||||
|
local joints = self.box2d_world:getJoints() |
||||||
|
for _, joint in ipairs(joints) do joint:destroy() end |
||||||
|
self.box2d_world:destroy() |
||||||
|
self.box2d_world = nil |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local Collider = {} |
||||||
|
Collider.__index = Collider |
||||||
|
|
||||||
|
local generator = love.math.newRandomGenerator(os.time()) |
||||||
|
local function UUID() |
||||||
|
local fn = function(x) |
||||||
|
local r = generator:random(16) - 1 |
||||||
|
r = (x == "x") and (r + 1) or (r % 4) + 9 |
||||||
|
return ("0123456789abcdef"):sub(r, r) |
||||||
|
end |
||||||
|
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn)) |
||||||
|
end |
||||||
|
|
||||||
|
function Collider.new(world, collider_type, ...) |
||||||
|
local self = {} |
||||||
|
self.id = UUID() |
||||||
|
self.world = world |
||||||
|
self.type = collider_type |
||||||
|
self.object = nil |
||||||
|
|
||||||
|
self.shapes = {} |
||||||
|
self.fixtures = {} |
||||||
|
self.sensors = {} |
||||||
|
|
||||||
|
self.collision_events = {} |
||||||
|
self.collision_stay = {} |
||||||
|
self.enter_collision_data = {} |
||||||
|
self.exit_collision_data = {} |
||||||
|
self.stay_collision_data = {} |
||||||
|
|
||||||
|
local args = {...} |
||||||
|
local shape, fixture |
||||||
|
if self.type == 'Circle' then |
||||||
|
self.collision_class = (args[4] and args[4].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, args[1], args[2], (args[4] and args[4].body_type) or 'dynamic') |
||||||
|
shape = love.physics.newCircleShape(args[3]) |
||||||
|
|
||||||
|
elseif self.type == 'Rectangle' then |
||||||
|
self.collision_class = (args[5] and args[5].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, args[1] + args[3]/2, args[2] + args[4]/2, (args[5] and args[5].body_type) or 'dynamic') |
||||||
|
shape = love.physics.newRectangleShape(args[3], args[4]) |
||||||
|
|
||||||
|
elseif self.type == 'BSGRectangle' then |
||||||
|
self.collision_class = (args[6] and args[6].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, args[1] + args[3]/2, args[2] + args[4]/2, (args[6] and args[6].body_type) or 'dynamic') |
||||||
|
local w, h, s = args[3], args[4], args[5] |
||||||
|
shape = love.physics.newPolygonShape({ |
||||||
|
-w/2, -h/2 + s, -w/2 + s, -h/2, |
||||||
|
w/2 - s, -h/2, w/2, -h/2 + s, |
||||||
|
w/2, h/2 - s, w/2 - s, h/2, |
||||||
|
-w/2 + s, h/2, -w/2, h/2 - s |
||||||
|
}) |
||||||
|
|
||||||
|
elseif self.type == 'Polygon' then |
||||||
|
self.collision_class = (args[2] and args[2].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, 0, 0, (args[2] and args[2].body_type) or 'dynamic') |
||||||
|
shape = love.physics.newPolygonShape(unpack(args[1])) |
||||||
|
|
||||||
|
elseif self.type == 'Line' then |
||||||
|
self.collision_class = (args[5] and args[5].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, 0, 0, (args[5] and args[5].body_type) or 'dynamic') |
||||||
|
shape = love.physics.newEdgeShape(args[1], args[2], args[3], args[4]) |
||||||
|
|
||||||
|
elseif self.type == 'Chain' then |
||||||
|
self.collision_class = (args[3] and args[3].collision_class) or 'Default' |
||||||
|
self.body = love.physics.newBody(self.world.box2d_world, 0, 0, (args[3] and args[3].body_type) or 'dynamic') |
||||||
|
shape = love.physics.newChainShape(args[1], unpack(args[2])) |
||||||
|
end |
||||||
|
|
||||||
|
-- Define collision classes and attach them to fixture and sensor |
||||||
|
fixture = love.physics.newFixture(self.body, shape) |
||||||
|
if self.world.masks[self.collision_class] then |
||||||
|
fixture:setCategory(unpack(self.world.masks[self.collision_class].categories)) |
||||||
|
fixture:setMask(unpack(self.world.masks[self.collision_class].masks)) |
||||||
|
end |
||||||
|
fixture:setUserData(self) |
||||||
|
local sensor = love.physics.newFixture(self.body, shape) |
||||||
|
sensor:setSensor(true) |
||||||
|
sensor:setUserData(self) |
||||||
|
|
||||||
|
self.shapes['main'] = shape |
||||||
|
self.fixtures['main'] = fixture |
||||||
|
self.sensors['main'] = sensor |
||||||
|
self.shape = shape |
||||||
|
self.fixture = fixture |
||||||
|
|
||||||
|
self.preSolve = function() end |
||||||
|
self.postSolve = function() end |
||||||
|
|
||||||
|
-- Points all body, fixture and shape functions to this wf.Collider object |
||||||
|
-- This means that the user can call collider:setLinearVelocity for instance without having to say collider.body:setLinearVelocity |
||||||
|
for k, v in pairs(self.body.__index) do |
||||||
|
if k ~= '__gc' and k ~= '__eq' and k ~= '__index' and k ~= '__tostring' and k ~= 'destroy' and k ~= 'type' and k ~= 'typeOf' then |
||||||
|
self[k] = function(self, ...) |
||||||
|
return v(self.body, ...) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
for k, v in pairs(self.fixture.__index) do |
||||||
|
if k ~= '__gc' and k ~= '__eq' and k ~= '__index' and k ~= '__tostring' and k ~= 'destroy' and k ~= 'type' and k ~= 'typeOf' then |
||||||
|
self[k] = function(self, ...) |
||||||
|
return v(self.fixture, ...) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
for k, v in pairs(self.shape.__index) do |
||||||
|
if k ~= '__gc' and k ~= '__eq' and k ~= '__index' and k ~= '__tostring' and k ~= 'destroy' and k ~= 'type' and k ~= 'typeOf' then |
||||||
|
self[k] = function(self, ...) |
||||||
|
return v(self.shape, ...) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
return setmetatable(self, Collider) |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:collisionEventsClear() |
||||||
|
self.collision_events = {} |
||||||
|
for other, _ in pairs(self.world.collision_classes) do |
||||||
|
self.collision_events[other] = {} |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:setCollisionClass(collision_class_name) |
||||||
|
if not self.world.collision_classes[collision_class_name] then error("Collision class " .. collision_class_name .. " doesn't exist.") end |
||||||
|
self.collision_class = collision_class_name |
||||||
|
for _, fixture in pairs(self.fixtures) do |
||||||
|
if self.world.masks[collision_class_name] then |
||||||
|
fixture:setCategory(unpack(self.world.masks[collision_class_name].categories)) |
||||||
|
fixture:setMask(unpack(self.world.masks[collision_class_name].masks)) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:enter(other_collision_class_name) |
||||||
|
local events = self.collision_events[other_collision_class_name] |
||||||
|
if events and #events >= 1 then |
||||||
|
for _, e in ipairs(events) do |
||||||
|
if e.collision_type == 'enter' then |
||||||
|
if not self.collision_stay[other_collision_class_name] then self.collision_stay[other_collision_class_name] = {} end |
||||||
|
table.insert(self.collision_stay[other_collision_class_name], {collider = e.collider_2, contact = e.contact}) |
||||||
|
self.enter_collision_data[other_collision_class_name] = {collider = e.collider_2, contact = e.contact} |
||||||
|
return true |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:getEnterCollisionData(other_collision_class_name) |
||||||
|
return self.enter_collision_data[other_collision_class_name] |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:exit(other_collision_class_name) |
||||||
|
local events = self.collision_events[other_collision_class_name] |
||||||
|
if events and #events >= 1 then |
||||||
|
for _, e in ipairs(events) do |
||||||
|
if e.collision_type == 'exit' then |
||||||
|
if self.collision_stay[other_collision_class_name] then |
||||||
|
for i = #self.collision_stay[other_collision_class_name], 1, -1 do |
||||||
|
local collision_stay = self.collision_stay[other_collision_class_name][i] |
||||||
|
if collision_stay.collider.id == e.collider_2.id then table.remove(self.collision_stay[other_collision_class_name], i) end |
||||||
|
end |
||||||
|
end |
||||||
|
self.exit_collision_data[other_collision_class_name] = {collider = e.collider_2, contact = e.contact} |
||||||
|
return true |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:getExitCollisionData(other_collision_class_name) |
||||||
|
return self.exit_collision_data[other_collision_class_name] |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:stay(other_collision_class_name) |
||||||
|
if self.collision_stay[other_collision_class_name] then |
||||||
|
if #self.collision_stay[other_collision_class_name] >= 1 then |
||||||
|
return true |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:getStayCollisionData(other_collision_class_name) |
||||||
|
return self.collision_stay[other_collision_class_name] |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:setPreSolve(callback) |
||||||
|
self.preSolve = callback |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:setPostSolve(callback) |
||||||
|
self.postSolve = callback |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:setObject(object) |
||||||
|
self.object = object |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:getObject() |
||||||
|
return self.object |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:addShape(shape_name, shape_type, ...) |
||||||
|
if self.shapes[shape_name] or self.fixtures[shape_name] then error("Shape/fixture " .. shape_name .. " already exists.") end |
||||||
|
local args = {...} |
||||||
|
local shape = love.physics['new' .. shape_type](unpack(args)) |
||||||
|
local fixture = love.physics.newFixture(self.body, shape) |
||||||
|
if self.world.masks[self.collision_class] then |
||||||
|
fixture:setCategory(unpack(self.world.masks[self.collision_class].categories)) |
||||||
|
fixture:setMask(unpack(self.world.masks[self.collision_class].masks)) |
||||||
|
end |
||||||
|
fixture:setUserData(self) |
||||||
|
local sensor = love.physics.newFixture(self.body, shape) |
||||||
|
sensor:setSensor(true) |
||||||
|
sensor:setUserData(self) |
||||||
|
|
||||||
|
self.shapes[shape_name] = shape |
||||||
|
self.fixtures[shape_name] = fixture |
||||||
|
self.sensors[shape_name] = sensor |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:removeShape(shape_name) |
||||||
|
if not self.shapes[shape_name] then return end |
||||||
|
self.shapes[shape_name] = nil |
||||||
|
self.fixtures[shape_name]:setUserData(nil) |
||||||
|
self.fixtures[shape_name]:destroy() |
||||||
|
self.fixtures[shape_name] = nil |
||||||
|
self.sensors[shape_name]:setUserData(nil) |
||||||
|
self.sensors[shape_name]:destroy() |
||||||
|
self.sensors[shape_name] = nil |
||||||
|
end |
||||||
|
|
||||||
|
function Collider:destroy() |
||||||
|
self.collision_stay = nil |
||||||
|
self.enter_collision_data = nil |
||||||
|
self.exit_collision_data = nil |
||||||
|
self:collisionEventsClear() |
||||||
|
|
||||||
|
self:setObject(nil) |
||||||
|
for name, _ in pairs(self.fixtures) do |
||||||
|
self.shapes[name] = nil |
||||||
|
self.fixtures[name]:setUserData(nil) |
||||||
|
self.fixtures[name] = nil |
||||||
|
self.sensors[name]:setUserData(nil) |
||||||
|
self.sensors[name] = nil |
||||||
|
end |
||||||
|
self.body:destroy() |
||||||
|
self.body = nil |
||||||
|
end |
||||||
|
|
||||||
|
wf.World = World |
||||||
|
wf.Collider = Collider |
||||||
|
|
||||||
|
return wf |
||||||
|
|
@ -0,0 +1,568 @@ |
|||||||
|
0.11.0 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- mlib.vec2 component |
||||||
|
|
||||||
|
To-Do: |
||||||
|
---- |
||||||
|
- Update README.md |
||||||
|
- Update spec.lua |
||||||
|
- Fix tabbing |
||||||
|
|
||||||
|
0.10.1 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- Point category |
||||||
|
- point.rotate |
||||||
|
- point.scale |
||||||
|
- point.polarToCartesian |
||||||
|
- point.cartesianToPolar |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- math.getPercent now returns decimals (instead of percentages) since those are more common to use. |
||||||
|
|
||||||
|
To-Do: |
||||||
|
---- |
||||||
|
- Determine if isCompletelyInsideFunctions should return true with tangents. |
||||||
|
- Check argument order for logicality and consistency. |
||||||
|
- Add error checking. |
||||||
|
- Make sure to see if any aliases were missed. (e.g. isSegmentInside) |
||||||
|
- Clean up and correct README (add "Home" link, etc.) |
||||||
|
|
||||||
|
0.10.0 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- mlib.line.segment is now mlib.segment. |
||||||
|
- mlib.line.getIntercept has been renamed to mlib.line.getYIntercept |
||||||
|
- mlib.line.getYIntercept now returns the x-coordinate for vertical lines instead of false. |
||||||
|
- mlib.line.getYIntercept now returns the value `isVertical` as the second return value. |
||||||
|
- mlib.line.getPerpendicularBisector is now mlib.segment.getPerpendicularBisector. |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- mlib.line.getIntersection now should handle vertical slopes better. |
||||||
|
- mlib.line.getClosestPoint now uses local function checkFuzzy for checking horizontal lines. |
||||||
|
- Fixed possible bug in mlib.line.getSegmentIntersection and vertical lines. |
||||||
|
- mlib.segment.getIntersection now uses fuzzy checking for parallel lines. |
||||||
|
- mlib.math.round is now much more efficient. |
||||||
|
- Removed some useless code from mlib.polygon.isSegmentInside. |
||||||
|
|
||||||
|
To-Do: |
||||||
|
---- |
||||||
|
- Determine if isCompletelyInsideFunctions should return true with tangents. |
||||||
|
- Check argument order for logicality and consistency. |
||||||
|
- Improve speed. |
||||||
|
- Add error checking. |
||||||
|
- Make sure to see if any aliases were missed. (e.g. isSegmentInside) |
||||||
|
- Implement mlib.shapes again(?) |
||||||
|
- Clean up and correct README (add "Home" link, etc.) |
||||||
|
|
||||||
|
0.9.4 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- mlib.line.getDistance is now slightly faster. |
||||||
|
- Made code much easier to debug by using new utility `cycle`. |
||||||
|
- Added new utility. |
||||||
|
- Various other minor changes. |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
- Unused local utility function copy |
||||||
|
|
||||||
|
To-Do |
||||||
|
---- |
||||||
|
- Determine if isCompletelyInsideFunctions should return true with tangents. |
||||||
|
- Make argument order more logical. |
||||||
|
- Improve speed and error checking. |
||||||
|
- Make sure to see if any aliases were missed. (e.g. isSegmentInside) |
||||||
|
- Implement mlib.shapes again(?) |
||||||
|
- Clean up README (add "Home" link, etc.) |
||||||
|
|
||||||
|
0.9.3 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- milb.circle.isCircleCompletelyInside |
||||||
|
- mlib.circle.isPolygonCompletelyInside |
||||||
|
- milb.circle.isSegmentCompletelyInside |
||||||
|
- mlib.polygon.isCircleCompletelyInside |
||||||
|
- mlib.polygon.isPolygonCompletelyInside |
||||||
|
- mlib.polygon.isSegmentCompletelyInside |
||||||
|
|
||||||
|
- ALIASES - |
||||||
|
- mlib.circle.getPolygonIntersection |
||||||
|
- mlib.circle.isCircleInsidePolygon |
||||||
|
- mlib.circle.isCircleCompletelyInsidePolygon |
||||||
|
- milb.line.getCircleIntersection |
||||||
|
- milb.line.getPolygonIntersection |
||||||
|
- milb.line.getLineIntersection |
||||||
|
- mlib.line.segment.getCircleIntersection |
||||||
|
- mlib.line.segment.getPolygonIntersection |
||||||
|
- mlib.line.segment.getLineIntersection |
||||||
|
- mlib.line.segment.getSegmentIntersection |
||||||
|
- mlib.line.segment.isSegmentCompletelyInsideCircle |
||||||
|
- mlib.line.segment.isSegmentCompletelyInsidePolygon |
||||||
|
- mlib.polygon.isCircleCompletelyOver |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- mlib.circle.getCircleIntersection now returns 'inside' instead of 'intersection' if the point has not intersections but is within the circle. |
||||||
|
- Fixed problem involving mlib.circle.getSegmentIntersection |
||||||
|
|
||||||
|
- README.md now has more information on how to run specs and other minor improvements. |
||||||
|
- Fixed some commenting on explanation of derivation of mlib.line.getIntersection. |
||||||
|
- Updated the example to use the current version of mlib. |
||||||
|
- Made/Changed some comments in the example main.lua. |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
To-Do |
||||||
|
---- |
||||||
|
- Make examples file on github (examples/shapes/main.lua, etc.) not just one line. |
||||||
|
- Determine if isCompletelyInsideFunctions should return true with tangents. |
||||||
|
- Make argument order more logical. |
||||||
|
- Make sure to see if any aliases were missed. (e.g. isSegmentInside) |
||||||
|
- Update spec links in README |
||||||
|
|
||||||
|
0.9.2 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- mlib.polygon.getPolygonIntersection now does not create duplicate local table. |
||||||
|
- mlib.line.getPerpendicularSlope now does not create a global variable. |
||||||
|
- mlib.math.getSummation now allows the error to go through instead of returning false if the stop value is not a number. |
||||||
|
|
||||||
|
- Changed any instance of the term "userdata" with "input" |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
0.9.1 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- Added mlib.statistics.getCentralTendency |
||||||
|
- Added mlib.statistics.getDispersion |
||||||
|
- Added mlib.statistics.getStandardDeviation |
||||||
|
- Added mlib.statistics.getVariation |
||||||
|
- Added mlib.statistics.getVariationRatio |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- FIX: mlib.polygon.checkPoint now handles vertices better. |
||||||
|
|
||||||
|
|
||||||
|
To-Do |
||||||
|
---- |
||||||
|
- Add more functions. |
||||||
|
|
||||||
|
0.9.0 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- mlib.line.getDistance as an alias for mlib.line.getLength. |
||||||
|
- mlib.line.checkPoint |
||||||
|
- Internal documentation. |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
- mlib.circle.isPointInCircle is replaced with mlib.circle.checkPoint |
||||||
|
- mlib.circle.checkPoint is replaced with mlib.circle.isPointOnCircle |
||||||
|
- Variation of mlib.circle.getLineIntersection( cx, cy, radius, slope, intercept ) is no longer supported, as it can cause errors with vertical lines. |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- CHANGE: mlib.line.getIntersection now returns true for colinear lines. |
||||||
|
- CHANGE: mlib.line.getIntersection now returns true if the line are collinear. |
||||||
|
- CHANGE: mlib.line.getIntersection now returns true if vertical lines are collinear. |
||||||
|
- CHANGE: mlib.line.getSegmentIntersection now returns true if the line and segment are collinear. |
||||||
|
- CHANGE: Changed the order of mlib.line.segment.checkPoint arguments. |
||||||
|
- NAME: mlib.polygon.lineIntersects is now mlib.polygon.getLineIntersection |
||||||
|
- NAME: mlib.polygon.lineSegmentIntersects is now mlib.polygon.getSegmentIntersection |
||||||
|
- NAME: mlib.polygon.isLineSegmentInside is now mlib.polygon.isSegmentInside |
||||||
|
- NAME: mlib.polygon.polygonIntersects is now mlib.polygon.getPolygonIntersection |
||||||
|
- CHANGED: mlib.circle.checkPoint now takes arguments ( px, py, cx, cy, radius ). |
||||||
|
- CHANGED: mlib.circle.isPointOnCircle now takes arguments ( px, py, cx, cy, radius ). |
||||||
|
- NAME: mlib.polygon.circleIntersects is now mlib.polygon.getCircleIntersection |
||||||
|
- NAME: mlib.circle.isLineSecant is now mlib.circle.getLineIntersection |
||||||
|
- NAME: mlib.circle.isSegmentSecant is now mlib.circle.getSegmentIntersection |
||||||
|
- NAME: mlib.circle.circlesIntersects is now mlib.circle.getCircleIntersection |
||||||
|
- CHANGE: Added types 'tangent' and 'intersection' to mlib.circle.getCircleIntersection. |
||||||
|
- NAME: mlib.math.getRootsOfQuadratic is now mlib.math.getQuadraticRoots |
||||||
|
- CHANGE: mlib.math.getRoot now only returns the positive, since it there is not always negatives. |
||||||
|
- NAME: mlib.math.getPercent is now mlib.math.getPercentage |
||||||
|
|
||||||
|
- Cleaned up code (added comments, spaced lines, etc.) |
||||||
|
- Made syntax that uses camelCase instead of CamelCase. |
||||||
|
- Match style of more programmers. |
||||||
|
- Easier to type. |
||||||
|
- Moved to semantic numbering. |
||||||
|
- Made any returns strings lower-case. |
||||||
|
- Updated specs for missing functions. |
||||||
|
|
||||||
|
To-Do |
||||||
|
---- |
||||||
|
- Update readme. |
||||||
|
- Add mlib.statistics.getStandardDeviation |
||||||
|
- Add mlib.statistics.getMeasuresOfCentralTendency |
||||||
|
- Add mlib.statistics.getMeasuresOfDispersion |
||||||
|
|
||||||
|
1.1.0.2 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- MLib.Polygon.IsPolygonInside |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
- Removed all MLib.Shape: |
||||||
|
- Was very slow. |
||||||
|
- Could not define custom callbacks. |
||||||
|
- Allow for flexibility. |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- Switched MLib.Line.GetIntersection back to the old way |
||||||
|
- MLib.Line.GetSegmentIntersection now returns 4 values if the lines are parallel. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Make it so that MLib.Shape objects can use ':' syntax for other functions (i.e. MLib.Line.GetLength for Line objects, etc.) |
||||||
|
- Intuitive error messages. |
||||||
|
|
||||||
|
|
||||||
|
1.1.0.1 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
- MLib.Line.GetIntersection now returns true, instead of two points. |
||||||
|
|
||||||
|
---- |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- MLib.Line.GetIntersection now handles vertical lines: returns true if they collide, false otherwise. |
||||||
|
- MLib.Polygon.LineIntersects now also handles verticals. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Fix |
||||||
|
- MLib.Shape Table can't have metatables. |
||||||
|
|
||||||
|
1.1.0.0 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
- MLib.Polygon.IsCircleInside |
||||||
|
- MLib.Polygon.LineSegmentIntersects |
||||||
|
- MLib.Polygon.IsLineSegmentInside |
||||||
|
- MLib.Statistics.GetFrequency |
||||||
|
- MLib.Math.Factorial |
||||||
|
- MLib.Math.SystemOfEquations |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- MLib.Polygon.LineIntersects is now MLib.Polygon.LineSegmentIntersects. |
||||||
|
- Put Word-wrap on Changes.txt |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- Problems with numberous MLib.Polygon and MLib.Circle problems. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Fix |
||||||
|
- MLib.Shape Table can't have metatables. |
||||||
|
|
||||||
|
1.0.0.3 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- README.md |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Add: |
||||||
|
- Frequency |
||||||
|
- Binomial Probability |
||||||
|
- Standard Deviation |
||||||
|
- Conditional Probability |
||||||
|
|
||||||
|
1.0.0.2 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
- Ability to use a direction for Math.GetAngle's 5th argument instead of having a third point. See Fixed for more. |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- Changed README.md for clarity and consistency. |
||||||
|
- Updated spec.lua |
||||||
|
- See Fixed for more. |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- Circle.IsSegmentSecant now properly accounts for chords actually being chords, and not secants. |
||||||
|
- Circle.CircleIntersects now can return 'Colinear' or 'Equal' if the circles have same x and y but different radii (Colinear) or are exactly the same (Equal). |
||||||
|
- Statistics.GetMode now returns a table with the modes, and the second argument as the number of times they appear. |
||||||
|
- Math.GetRoot now returns the negative number as a second argument. |
||||||
|
- Math.GetPercentOfChange now works for 0 to 0 (previously false). |
||||||
|
- Math.GetAngle now takes only three points and no direction option. |
||||||
|
- Typos in Shape.CheckCollisions and Shape.Remove. |
||||||
|
- Fixed nil problems in Shape.CheckCollisions. |
||||||
|
- Improved readablility and DRYness of Shape.CheckCollisions. |
||||||
|
- Bugs in Shape.Remove and Shape.CheckCollisions regarding passing tables as arguments. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Add: |
||||||
|
- Frequency |
||||||
|
- Binomial Probability |
||||||
|
- Standard Deviation |
||||||
|
- Conditional Probability |
||||||
|
|
||||||
|
1.0.0.1 |
||||||
|
==== |
||||||
|
Added: |
||||||
|
---- |
||||||
|
|
||||||
|
Removed: |
||||||
|
---- |
||||||
|
|
||||||
|
Changed: |
||||||
|
---- |
||||||
|
- Changes.txt now expanded to include short excertps from all previous commits. |
||||||
|
- Changed release number from 3.0.0 to 1.0.0.1 |
||||||
|
- Math.Round now can round to decimal places as the second argument. |
||||||
|
- Commented unnecessary call of Segment.CheckPoint in Polygon.LineIntersects. |
||||||
|
- Polygon.LineIntersects now returns where the lines intersect. |
||||||
|
- false if not intersection. |
||||||
|
- A table with all of the intersections { { px, py } } |
||||||
|
- Same with Polygon.PolygonIntersects, Polygon.CircleIntersects, |
||||||
|
|
||||||
|
Fixed: |
||||||
|
---- |
||||||
|
- Error with GetSlope being called incorrectly. |
||||||
|
- README.md Line.GetPerpendicularSlope misdirection. |
||||||
|
- Same with Line.GetPerpendicularBisector, Line.Segment.GetIntersection, Circle.IsLineSecant, Circle.IsSegmentSecant, Statistics.GetMean, Median, Mode, and Range, and Shape:Remove, and fixed the naming for Shape:CheckCollisions and Shape:Remove. |
||||||
|
- Clarified README.md |
||||||
|
- Made util SortWithReferences local. |
||||||
|
- Errors caused by local functions. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Add: |
||||||
|
- Frequency |
||||||
|
- Binomial Probability |
||||||
|
- Standard Deviation |
||||||
|
- Conditional Probability |
||||||
|
|
||||||
|
3.0.0 |
||||||
|
----- |
||||||
|
ADDED: |
||||||
|
- Added function GetSignedArea. |
||||||
|
REMOVED: |
||||||
|
- Removed drawing functions. |
||||||
|
- Removed MLib.Line.Functions entirely. |
||||||
|
CHANGED: |
||||||
|
- Changed all the names to CamelCase. |
||||||
|
- Changed module name to MLib. |
||||||
|
- Changed return order of GetPerpendicualrBisector from Slope, Midpoint to Midpoint, Slope. |
||||||
|
- Changed returned string of MLib.circle.isLineSecant to be upper-case. |
||||||
|
- Changed IsPrime to accept only one number at a time. |
||||||
|
- Changed NewShape's type to Capitals. |
||||||
|
|
||||||
|
Related to code: |
||||||
|
- Added more accuarate comments. |
||||||
|
- Made code more DRY. |
||||||
|
- Made code monkey-patchable and saved space (by declaring all functions as local values then inserted them into a large table. |
||||||
|
|
||||||
|
TODO: |
||||||
|
- Make LineIntersectsPolygon return where intersection occurs. |
||||||
|
- Ditto with PolygonIntersectsPolygon. |
||||||
|
- Add: |
||||||
|
- Frequency |
||||||
|
- Binomial Probability |
||||||
|
- Standard Deviation |
||||||
|
- Conditional Probability |
||||||
|
|
||||||
|
|
||||||
|
Not as accurately maintained before 2.0.2 |
||||||
|
----------------------------------------- |
||||||
|
|
||||||
|
2.0.2 |
||||||
|
----- |
||||||
|
- Cleaned up code, mostly. |
||||||
|
|
||||||
|
2.0.1 |
||||||
|
----- |
||||||
|
- Bug fixes, mlib.shape:remove & demos added. |
||||||
|
|
||||||
|
2.0.0 |
||||||
|
----- |
||||||
|
- Added mlib.shape and various bug fixes. |
||||||
|
|
||||||
|
2.0.0 |
||||||
|
----- |
||||||
|
- Made mlib.shape and made numberous bug fixes. |
||||||
|
|
||||||
|
1.9.4 |
||||||
|
----- |
||||||
|
- Made mlib.math.prime faster and removed ability to test multiple numbers at once. Thanks Robin! |
||||||
|
|
||||||
|
1.9.3 |
||||||
|
----- |
||||||
|
- Fixed polygon.area and polygon.centroid |
||||||
|
|
||||||
|
1.9.2 |
||||||
|
----- |
||||||
|
- Updated to LOVE 0.9.0. |
||||||
|
|
||||||
|
1.9.1 |
||||||
|
----- |
||||||
|
- Made mlib.line.closestPoint able to take either two points on the slope or the slope and intercept. |
||||||
|
|
||||||
|
1.9.0 |
||||||
|
----- |
||||||
|
- Added mlib.lineSegmentIntersects (no affiliation with previous one (changed to mlib.line.segment.intersect)) and mlib.line.closestPoint |
||||||
|
|
||||||
|
1.8.3 |
||||||
|
----- |
||||||
|
- Changed naming mechanism to be more organized. |
||||||
|
|
||||||
|
1.8.2 |
||||||
|
----- |
||||||
|
- "Fixed" mlib.lineSegmentsIntersect AGAIN!!!! :x |
||||||
|
|
||||||
|
1.8.1 |
||||||
|
----- |
||||||
|
- Removed a print statement. |
||||||
|
|
||||||
|
1.8.0 |
||||||
|
----- |
||||||
|
- mlib.pointInPolygon added |
||||||
|
|
||||||
|
1.7.5 |
||||||
|
----- |
||||||
|
- mlib.lineSegmentsIntersect vertical lines fixed again. This time for real. I promise... or hope, at least... :P |
||||||
|
|
||||||
|
1.7.4 |
||||||
|
----- |
||||||
|
- mlib.lineSegmentsIntersect vertical parallels fixed |
||||||
|
|
||||||
|
1.7.3 |
||||||
|
----- |
||||||
|
- mlib.lineSegmentsIntersect parallels fixed |
||||||
|
|
||||||
|
1.7.2 |
||||||
|
----- |
||||||
|
- mlib.lineSegmentsIntersect now handles vertical lines |
||||||
|
|
||||||
|
1.7.1 |
||||||
|
----- |
||||||
|
- mlib.lineSegmentsIntersect now returns the two places in between where the line segments begin to intersect. |
||||||
|
|
||||||
|
1.7.0 |
||||||
|
----- |
||||||
|
- Added mlib.circlesIntersect, mlib.pointOnLineSegment, mlib.linesIntersect, and mlib.lineSegmentsIntersect |
||||||
|
|
||||||
|
1.6.1 |
||||||
|
----- |
||||||
|
- Employed usage of summations for mlib.getPolygonArea and mlib.getPolygonCentroid and removed area as an argument for mlib.getPolygonCentroid. |
||||||
|
|
||||||
|
1.6.0 |
||||||
|
----- |
||||||
|
- Added several functions. |
||||||
|
|
||||||
|
1.5.0 |
||||||
|
----- |
||||||
|
- Made lots of changes to syntax to make it easier to use (hopefully). I also put out specs. |
||||||
|
|
||||||
|
1.4.1 |
||||||
|
----- |
||||||
|
- Localized mlib. Thanks, Yonaba! |
||||||
|
|
||||||
|
1.4.0 |
||||||
|
----- |
||||||
|
- Added mlib.getPolygonCentroid (gets the midpoint of a non-self-intersecting polygons) |
||||||
|
|
||||||
|
1.3.2 |
||||||
|
----- |
||||||
|
- Made mlib.getPrime take tables as arguments, so you can check all the values of a table. |
||||||
|
|
||||||
|
1.3.1 |
||||||
|
----- |
||||||
|
- Changed name method to mlib.getPolygonArea |
||||||
|
|
||||||
|
1.3.0 |
||||||
|
----- |
||||||
|
- Added mlib.get_polygon_area and removed mlib.get_convex_area and mlib.get_triangle_area since they are repetitive. |
||||||
|
|
||||||
|
1.2.2 |
||||||
|
----- |
||||||
|
- Made functions return faster, functions that previously returned tables now return multiple arguments. |
||||||
|
|
||||||
|
1.2.1 |
||||||
|
----- |
||||||
|
- Localized functions, made tables acceptable as arguments, refined function speed, mlib.get_mode now returns number most repeated as well as how many times. |
||||||
|
|
||||||
|
1.2.0 |
||||||
|
----- |
||||||
|
- Added mlib.get_angle |
||||||
|
|
||||||
|
1.1.0 |
||||||
|
----- |
||||||
|
- Added mlib.get_convex_area |
||||||
|
|
||||||
|
1.0.4 |
||||||
|
----- |
||||||
|
- Fixed get_mode to handle bimodials. |
||||||
|
|
||||||
|
1.0.3 |
||||||
|
----- |
||||||
|
- Prime Checker optimized (hopefully final update on this.) |
||||||
|
|
||||||
|
1.0.2 |
||||||
|
----- |
||||||
|
- Prime checker now works! (At least to 1000. I haven't tested any |
||||||
|
further) |
||||||
|
|
||||||
|
1.0.1 |
||||||
|
----- |
||||||
|
- 'Fixed' the prime checker |
||||||
|
|
||||||
|
1.0.0 |
||||||
|
----- |
||||||
|
- Initial release |
@ -0,0 +1,17 @@ |
|||||||
|
Copyright (c) 2015 Davis Claiborne |
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied |
||||||
|
warranty. In no event will the authors be held liable for any damages |
||||||
|
arising from the use of this software. |
||||||
|
|
||||||
|
Permission is granted to anyone to use this software for any purpose, |
||||||
|
including commercial applications, and to alter it and redistribute it |
||||||
|
freely, subject to the following restrictions: |
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not |
||||||
|
claim that you wrote the original software. If you use this software |
||||||
|
in a product, an acknowledgement in the product documentation would be |
||||||
|
appreciated but is not required. |
||||||
|
2. Altered source versions must be plainly marked as such, and must not be |
||||||
|
misrepresented as being the original software. |
||||||
|
3. This notice may not be removed or altered from any source distribution. |
@ -0,0 +1,890 @@ |
|||||||
|
MLib |
||||||
|
==== |
||||||
|
|
||||||
|
__MLib__ is a math and shape-intersection detection library written in Lua. It's aim is to be __robust__ and __easy to use__. |
||||||
|
|
||||||
|
__NOTE:__ |
||||||
|
- I am (slowly) working on completely rewriting this in order to be easier to use and less bug-prone. You can check out the progress [here](../../tree/dev). |
||||||
|
- I am currently slowing development of MLib while moving over to helping with [CPML](https://github.com/excessive/cpml). To discuss this, please comment [here](../../issues/12). |
||||||
|
|
||||||
|
If you are looking for a library that handles updating/collision responses for you, take a look at [hxdx](https://github.com/adonaac/hxdx). It uses MLib functions as well as Box2d to handle physics calculations. |
||||||
|
|
||||||
|
## Downloading |
||||||
|
You can download the latest __stable__ version of MLib by downloading the latest [release](../../releases/). |
||||||
|
You can download the latest __working__ version of MLib by downloading the latest [commit](../../commits/master/). Documentation will __only__ be updated upon releases, not upon commits. |
||||||
|
|
||||||
|
## Implementing |
||||||
|
To use MLib, simply place [mlib.lua](mlib.lua) inside the desired folder in your project. Then use the `require 'path.to.mlib'` to use any of the functions. |
||||||
|
|
||||||
|
## Examples |
||||||
|
If you don't have [LÖVE](https://love2d.org/) installed, you can download the .zip of the demo from the [Executables](Examples/Executables) folder and extract and run the .exe that way. |
||||||
|
You can see some examples of the code in action [here](Examples). |
||||||
|
All examples are done using the *awesome* engine of [LÖVE](https://love2d.org/). |
||||||
|
To run them properly, download the [.love file](Examples/LOVE) and install LÖVE to your computer. |
||||||
|
After that, make sure you set .love files to open with "love.exe". |
||||||
|
For more, see [here](https://love2d.org/). |
||||||
|
|
||||||
|
## When should I use MLib? |
||||||
|
- If you need to know exactly where two objects intersect. |
||||||
|
- If you need general mathematical equations to be done. |
||||||
|
- If you need very precise details about point intersections. |
||||||
|
|
||||||
|
## When should I __not__ use MLib? |
||||||
|
- All of the objects in a platformer, or other game, for instance, should not be registered with MLib. Only ones that need very specific information. |
||||||
|
- When you don't need precise information/odd shapes. |
||||||
|
|
||||||
|
## Specs |
||||||
|
#### For Windows |
||||||
|
If you run Windows and have Telescope in `%USERPROFILE%\Documents\GitHub` (you can also manually change the path in [test.bat](test.bat)) you can simply run [test.bat](test.bat) and it will display the results, and then clean up after it's finished. |
||||||
|
|
||||||
|
#### Default |
||||||
|
Alternatively, you can find the tests [here](spec.lua). Keep in mind that you may need to change certain semantics to suit your OS. |
||||||
|
You can run them via [Telescope](https://github.com/norman/telescope/) and type the following command in the command-line of the root folder: |
||||||
|
``` |
||||||
|
tsc -f specs.lua |
||||||
|
``` |
||||||
|
If that does not work, you made need to put a link to Lua inside of the folder for `telescope` and run the following command: |
||||||
|
``` |
||||||
|
lua tsc -f specs.lua |
||||||
|
``` |
||||||
|
If you encounter further errors, try to run the command line as an administrator (usually located in `C:\Windows\System32\`), then right-click on `cmd.exe` and select `Run as administrator`, then do |
||||||
|
``` |
||||||
|
cd C:\Path\to\telescope\ |
||||||
|
``` |
||||||
|
And __then__ run one of the above commands. If none of those work, just take my word for it that all the tests pass and look at this picture. |
||||||
|
![Success](Reference Pictures/Success.png) |
||||||
|
|
||||||
|
## Functions |
||||||
|
- [mlib.line](#mlibline) |
||||||
|
- [mlib.line.checkPoint](#mliblinecheckpoint) |
||||||
|
- [mlib.line.getClosestPoint](#mliblinegetclosestpoint) |
||||||
|
- [mlib.line.getYIntercept](#mliblinegetintercept) |
||||||
|
- [mlib.line.getIntersection](#mliblinegetintersection) |
||||||
|
- [mlib.line.getLength](#mliblinegetlength) |
||||||
|
- [mlib.line.getMidpoint](#mliblinegetmidpoint) |
||||||
|
- [mlib.line.getPerpendicularSlope](#mliblinegetperpendicularslope) |
||||||
|
- [mlib.line.getSegmentIntersection](#mliblinegetsegmentintersection) |
||||||
|
- [mlib.line.getSlope](#mliblinegetslope) |
||||||
|
- [mlib.segment](#mlibsegment) |
||||||
|
- [mlib.segment.checkPoint](#mlibsegmentcheckpoint) |
||||||
|
- [mlib.segment.getPerpendicularBisector](#mlibsegmentgetperpendicularbisector) |
||||||
|
- [mlib.segment.getIntersection](#mlibsegmentgetintersection) |
||||||
|
- [mlib.polygon](#mlibpolygon) |
||||||
|
- [mlib.polygon.checkPoint](#mlibpolygoncheckpoint) |
||||||
|
- [mlib.polygon.getCentroid](#mlibpolygongetcentroid) |
||||||
|
- [mlib.polygon.getCircleIntersection](#mlibpolygongetcircleintersection) |
||||||
|
- [mlib.polygon.getLineIntersection](#mlibpolygongetlineintersection) |
||||||
|
- [mlib.polygon.getPolygonArea](#mlibpolygongetpolygonarea) |
||||||
|
- [mlib.polygon.getPolygonIntersection](#mlibpolygongetpolygonintersection) |
||||||
|
- [mlib.polygon.getSegmentIntersection](#mlibpolygongetsegmentintersection) |
||||||
|
- [mlib.polygon.getSignedPolygonArea](#mlibpolygongetsignedpolygonarea) |
||||||
|
- [mlib.polygon.getTriangleHeight](#mlibpolygongettriangleheight) |
||||||
|
- [mlib.polygon.isCircleInside](#mlibpolygoniscircleinside) |
||||||
|
- [mlib.polygon.isCircleCompletelyInside](#mlibpolygoniscirclecompletelyinside) |
||||||
|
- [mlib.polygon.isPolygonInside](#mlibpolygonispolygoninside) |
||||||
|
- [mlib.polygon.isPolygonCompletelyInside](#mlibpolygonispolygoncompletelyinside) |
||||||
|
- [mlib.polygon.isSegmentInside](#mlibpolygonissegmentinside) |
||||||
|
- [mlib.polygon.isSegmentCompletelyInside](#mlibpolygonissegmentcompletelyinside) |
||||||
|
- [mlib.circle](#mlibcircle) |
||||||
|
- [mlib.circle.checkPoint](#mlibcirclecheckpoint) |
||||||
|
- [mlib.circle.getArea](#mlibcirclegetarea) |
||||||
|
- [mlib.circle.getCircleIntersection](#mlibcirclegetcircleintersection) |
||||||
|
- [mlib.circle.getCircumference](#mlibcirclegetcircumference) |
||||||
|
- [mlib.circle.getLineIntersection](#mlibcirclegetlineintersection) |
||||||
|
- [mlib.circle.getSegmentIntersection](#mlibcirclegetsegmentintersection) |
||||||
|
- [mlib.circle.isCircleCompletelyInside](#mlibcircleiscirclecompletelyinside) |
||||||
|
- [mlib.circle.isCircleCompletelyInsidePolygon](#mlibcircleiscirclecompletelyinsidepolygon) |
||||||
|
- [mlib.circle.isPointOnCircle](#mlibcircleispointoncircle) |
||||||
|
- [mlib.circle.isPolygonCompletelyInside](#mlibcircleispolygoncompletelyinside) |
||||||
|
- [mlib.statistics](#mlibstatistics) |
||||||
|
- [mlib.statistics.getCentralTendency](#mlibstatisticsgetcentraltendency) |
||||||
|
- [mlib.statistics.getDispersion](#mlibstatisticsgetdispersion) |
||||||
|
- [mlib.statistics.getMean](#mlibstatisticsgetmean) |
||||||
|
- [mlib.statistics.getMedian](#mlibstatisticsgetmedian) |
||||||
|
- [mlib.statistics.getMode](#mlibstatisticsgetmode) |
||||||
|
- [mlib.statistics.getRange](#mlibstatisticsgetrange) |
||||||
|
- [mlib.statistics.getStandardDeviation](#mlibstatisticsgetstandarddeviation) |
||||||
|
- [mlib.statistics.getVariance](#mlibstatisticsgetvariance) |
||||||
|
- [mlib.statistics.getVariationRatio](#mlibstatisticsgetvariationratio) |
||||||
|
- [mlib.math](#mlibmath) |
||||||
|
- [mlib.math.getAngle](#mlibmathgetangle) |
||||||
|
- [mlib.math.getPercentage](#mlibmathgetpercentage) |
||||||
|
- [mlib.math.getPercentOfChange](#mlibmathgetpercentofchange) |
||||||
|
- [mlib.math.getQuadraticRoots](#mlibmathgetquadraticroots) |
||||||
|
- [mlib.math.getRoot](#mlibmathgetroot) |
||||||
|
- [mlib.math.getSummation](#mlibmathgetsummation) |
||||||
|
- [mlib.math.isPrime](#mlibmathisprime) |
||||||
|
- [mlib.math.round](#mlibmathround) |
||||||
|
- [Aliases](#aliases) |
||||||
|
|
||||||
|
#### mlib.line |
||||||
|
- Deals with linear aspects, such as slope and length. |
||||||
|
|
||||||
|
##### mlib.line.checkPoint |
||||||
|
- Checks if a point lies on a line. |
||||||
|
- Synopsis: |
||||||
|
- `onPoint = mlib.line.checkPoint( px, px, x1, y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `px`, `py`: Numbers. The x and y coordinates of the point being tested. |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates of the line being tested. |
||||||
|
- Returns: |
||||||
|
- `onPoint`: Boolean. |
||||||
|
- `true` if the point is on the line. |
||||||
|
- `false` if it does not. |
||||||
|
- Notes: |
||||||
|
- You cannot use the format `mlib.line.checkPoint( px, px, slope, intercept )` because this would lead to errors on vertical lines. |
||||||
|
|
||||||
|
##### mlib.line.getClosestPoint |
||||||
|
- Gives the closest point to a line. |
||||||
|
- Synopses: |
||||||
|
- `cx, cy = mlib.line.getClosestPoint( px, py, x1, y1, x2, y2 )` |
||||||
|
- `cx, cy = mlib.line.getClosestPoint( px, py, slope, intercept )` |
||||||
|
- Arguments: |
||||||
|
- `x`, `y`: Numbers. The x and y coordinates of the point. |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates on the line. |
||||||
|
- `slope`, `intercept`: |
||||||
|
- Numbers. The slope and y-intercept of the line. |
||||||
|
- Booleans (`false`). The slope and y-intercept of a vertical line. |
||||||
|
- Returns: |
||||||
|
- `cx`, `cy`: Numbers. The closest points that lie on the line to the point. |
||||||
|
|
||||||
|
##### mlib.line.getYIntercept |
||||||
|
- Gives y-intercept of the line. |
||||||
|
- Synopses: |
||||||
|
- `intercept, isVertical = mlib.line.getYIntercept( x1, y1, x2, y2 )` |
||||||
|
- `intercept, isVertical = mlib.line.getYIntercept( x1, y1, slope )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates that lie on the line. |
||||||
|
- `slope`: |
||||||
|
- Number. The slope of the line. |
||||||
|
- Returns: |
||||||
|
- `intercept`: |
||||||
|
- Number. The y-intercept of the line. |
||||||
|
- Number. The `x1` coordinate of the line if the line is vertical. |
||||||
|
- `isVertical`: |
||||||
|
- Boolean. `true` if the line is vertical, `false` if the line is not vertical. |
||||||
|
|
||||||
|
##### mlib.line.getIntersection |
||||||
|
- Gives the intersection of two lines. |
||||||
|
- Synopses: |
||||||
|
- `x, y = mlib.line.getIntersection( x1, y1, x2, y2, x3, y3, x4, y4 )` |
||||||
|
- `x, y = mlib.line.getIntersection( slope1, intercept1, x3, y3, x4, y4 )` |
||||||
|
- `x, y = mlib.line.getIntersection( slope1, intercept1, slope2, intercept2 )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates that lie on the first line. |
||||||
|
- `x3`, `y3`, `x4`, `y4`: Numbers. Two x and y coordinates that lie on the second line. |
||||||
|
- `slope1`, `intercept1`: |
||||||
|
- Numbers. The slope and y-intercept of the first line. |
||||||
|
- Booleans (`false`). The slope and y-intercept of the first line (if the first line is vertical). |
||||||
|
- `slope2`, `intercept2`: |
||||||
|
- Numbers. The slope and y-intercept of the second line. |
||||||
|
- Booleans (`false`). The slope and y-intercept of the second line (if the second line is vertical). |
||||||
|
- Returns: |
||||||
|
- `x`, `y`: |
||||||
|
- Numbers. The x and y coordinate where the lines intersect. |
||||||
|
- Boolean: |
||||||
|
- `true`, `nil`: The lines are collinear. |
||||||
|
- `false`, `nil`: The lines are parallel and __not__ collinear. |
||||||
|
|
||||||
|
##### mlib.line.getLength |
||||||
|
- Gives the distance between two points. |
||||||
|
- Synopsis: |
||||||
|
- `length = mlib.line.getLength( x1, y1, x2, y2 ) |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- Returns: |
||||||
|
- `length`: Number. The distance between the two points. |
||||||
|
|
||||||
|
##### mlib.line.getMidpoint |
||||||
|
- Gives the midpoint of two points. |
||||||
|
- Synopsis: |
||||||
|
- `x, y = mlib.line.getMidpoint( x1, y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- Returns: |
||||||
|
- `x`, `y`: Numbers. The midpoint x and y coordinates. |
||||||
|
|
||||||
|
##### mlib.line.getPerpendicularSlope |
||||||
|
- Gives the perpendicular slope of a line. |
||||||
|
- Synopses: |
||||||
|
- `perpSlope = mlib.line.getPerpendicularSlope( x1, y1, x2, y2 )` |
||||||
|
- `perpSlope = mlib.line.getPerpendicularSlope( slope )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- `slope`: Number. The slope of the line. |
||||||
|
- Returns: |
||||||
|
- `perpSlope`: |
||||||
|
- Number. The perpendicular slope of the line. |
||||||
|
- Boolean (`false`). The perpendicular slope of the line (if the original line was horizontal). |
||||||
|
|
||||||
|
##### mlib.line.getSegmentIntersection |
||||||
|
- Gives the intersection of a line segment and a line. |
||||||
|
- Synopses: |
||||||
|
- `x1, y1, x2, y2 = mlib.line.getSegmentIntersection( x1, y1, x2, y2, x3, y3, x4, y4 )` |
||||||
|
- `x1, y1, x2, y2 = mlib.line.getSegmentIntersection( x1, y1, x2, y2, slope, intercept )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates that lie on the line segment. |
||||||
|
- `x3`, `y3`, `x4`, `y4`: Numbers. Two x and y coordinates that lie on the line. |
||||||
|
- `slope`, `intercept`: |
||||||
|
- Numbers. The slope and y-intercept of the the line. |
||||||
|
- Booleans (`false`). The slope and y-intercept of the line (if the line is vertical). |
||||||
|
- Returns: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: |
||||||
|
- Number, Number, Number, Number. |
||||||
|
- The points of the line segment if the line and segment are collinear. |
||||||
|
- Number, Number, Boolean (`nil`), Boolean (`nil`). |
||||||
|
- The coordinate of intersection if the line and segment intersect and are not collinear. |
||||||
|
- Boolean (`false`), Boolean (`nil`), Boolean (`nil`), |
||||||
|
- Boolean (`nil`). If the line and segment don't intersect. |
||||||
|
|
||||||
|
##### mlib.line.getSlope |
||||||
|
- Gives the slope of a line. |
||||||
|
- Synopsis: |
||||||
|
- `slope = mlib.line.getSlope( x1, y1, x2, y2 ) |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- Returns: |
||||||
|
- `slope`: |
||||||
|
- Number. The slope of the line. |
||||||
|
- Boolean (`false`). The slope of the line (if the line is vertical). |
||||||
|
|
||||||
|
#### mlib.segment |
||||||
|
- Deals with line segments. |
||||||
|
|
||||||
|
##### mlib.segment.checkPoint |
||||||
|
- Checks if a point lies on a line segment. |
||||||
|
- Synopsis: |
||||||
|
- `onSegment = mlib.segment.checkPoint( px, py, x1 y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `px`, `py`: Numbers. The x and y coordinates of the point being checked. |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- Returns: |
||||||
|
- `onSegment`: Boolean. |
||||||
|
- `true` if the point lies on the line segment. |
||||||
|
- `false` if the point does not lie on the line segment. |
||||||
|
|
||||||
|
##### mlib.segment.getPerpendicularBisector |
||||||
|
- Gives the perpendicular bisector of a line. |
||||||
|
- Synopsis: |
||||||
|
- `x, y, slope = mlib.segment.getPerpendicularBisector( x1, y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- Returns: |
||||||
|
- `x`, `y`: Numbers. The midpoint of the line. |
||||||
|
- `slope`: |
||||||
|
- Number. The perpendicular slope of the line. |
||||||
|
- Boolean (`false`). The perpendicular slope of the line (if the original line was horizontal). |
||||||
|
|
||||||
|
##### mlib.segment.getIntersection |
||||||
|
- Checks if two line segments intersect. |
||||||
|
- Synopsis: |
||||||
|
- `cx1, cy1, cx2, cy2 = mlib.segment.getIntersection( x1, y1, x2, y2, x3, y3 x4, y4 )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates of the first line segment. |
||||||
|
- `x3`, `y3`, `x4`, `y4`: Numbers. Two x and y coordinates of the second line segment. |
||||||
|
- Returns: |
||||||
|
- `cx1`, `cy1`, `cx2`, `cy2`: |
||||||
|
- Number, Number, Number, Number. |
||||||
|
- The points of the resulting intersection if the line segments are collinear. |
||||||
|
- Number, Number, Boolean (`nil`), Boolean (`nil`). |
||||||
|
- The point of the resulting intersection if the line segments are not collinear. |
||||||
|
- Boolean (`false`), Boolean (`nil`), Boolean (`nil`) , Boolean (`nil`). |
||||||
|
- If the line segments don't intersect. |
||||||
|
|
||||||
|
#### mlib.polygon |
||||||
|
- Handles aspects involving polygons. |
||||||
|
|
||||||
|
##### mlib.polygon.checkPoint |
||||||
|
- Checks if a point is inside of a polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.checkPoint( px, py, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.checkPoint( px, py, ... )` |
||||||
|
- Arguments: |
||||||
|
- `px`, `py`: Numbers. The x and y coordinate of the point being checked. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the point is inside the polygon. |
||||||
|
- `false` if the point is not inside the polygon. |
||||||
|
|
||||||
|
##### mlib.polygon.getCentroid |
||||||
|
- Returns the centroid of the polygon. |
||||||
|
- Synopses: |
||||||
|
- `cx, cy = mlib.polygon.getCentroid( vertices )` |
||||||
|
- `cx, cy = mlib.polygon.getCentroid( ... )` |
||||||
|
- Arguments: |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates of the centroid. |
||||||
|
|
||||||
|
##### mlib.polygon.getCircleIntersection |
||||||
|
- Returns the coordinates of where a circle intersects a polygon. |
||||||
|
- Synopses: |
||||||
|
- `intersections = mlib.polygon.getCircleIntersection( cx, cy, radius, vertices )` |
||||||
|
- `intersections = mlib.polygon.getCircleIntersection( cx, cy, radius, ... ) |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Number. The coordinates of the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. Contains the intersections and type. |
||||||
|
- Example: |
||||||
|
```lua |
||||||
|
local tab = _.polygon.getCircleIntersection( 5, 5, 1, 4, 4, 6, 4, 6, 6, 4, 6 ) |
||||||
|
for i = 1, # tab do |
||||||
|
print( i .. ':', unpack( tab[i] ) ) |
||||||
|
end |
||||||
|
-- 1: tangent 5 4 |
||||||
|
-- 2: tangent 6 5 |
||||||
|
-- 3: tangent 5 6 |
||||||
|
-- 4: tagnent 4 5 |
||||||
|
``` |
||||||
|
- For more see [mlib.circle.getSegmentIntersection](#mlibcirclegetsegmentintersection) or the [specs](spec.lua# L676) |
||||||
|
|
||||||
|
##### mlib.polygon.getLineIntersection |
||||||
|
- Returns the coordinates of where a line intersects a polygon. |
||||||
|
- Synopses: |
||||||
|
- `intersections = mlib.polygon.getLineIntersection( x1, y1, x2, y2, vertices )` |
||||||
|
- `intersections = mlib.polygon.getLineIntersection( x1, y1, x2, y2, ... ) |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. Contains the intersections. |
||||||
|
- Notes: |
||||||
|
- With collinear lines, they are actually broken up. i.e. `{ 0, 4, 0, 0 }` would become `{ 0, 4 }, { 0, 0 }`. |
||||||
|
|
||||||
|
##### mlib.polygon.getPolygonArea |
||||||
|
- Gives the area of a polygon. |
||||||
|
- Synopses: |
||||||
|
- `area = mlib.polygon.getArea( vertices )` |
||||||
|
- `area = mlib.polygon.getArea( ... ) |
||||||
|
- Arguments: |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `area`: Number. The area of the polygon. |
||||||
|
|
||||||
|
##### mlib.polygon.getPolygonIntersection |
||||||
|
- Gives the intersection of two polygons. |
||||||
|
- Synopsis: |
||||||
|
- `intersections = mlib.polygon.getPolygonIntersections( polygon1, polygon2 )` |
||||||
|
- Arguments: |
||||||
|
- `polygon1`: Table. The vertices of the first polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `polygon2`: Table. The vertices of the second polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. A table of the points of intersection. |
||||||
|
|
||||||
|
##### mlib.polygon.getSegmentIntersection |
||||||
|
- Returns the coordinates of where a line segmeing intersects a polygon. |
||||||
|
- Synopses: |
||||||
|
- `intersections = mlib.polygon.getSegmentIntersection( x1, y1, x2, y2, vertices )` |
||||||
|
- `intersections = mlib.polygon.getSegmentIntersection( x1, y1, x2, y2, ... ) |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. Contains the intersections. |
||||||
|
- Notes: |
||||||
|
- With collinear line segments, they are __not__ broken up. See the [specs](spec.lua# L508) for more. |
||||||
|
|
||||||
|
##### mlib.polygon.getSignedPolygonArea |
||||||
|
- Gets the signed area of the polygon. If the points are ordered counter-clockwise the area is positive. If the points are ordered clockwise the number is negative. |
||||||
|
- Synopses: |
||||||
|
- `area = mlib.polygon.getLineIntersection( vertices )` |
||||||
|
- `area = mlib.polygon.getLineIntersection( ... ) |
||||||
|
- Arguments: |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `area`: Number. The __signed__ area of the polygon. If the points are ordered counter-clockwise the area is positive. If the points are ordered clockwise the number is negative. |
||||||
|
|
||||||
|
##### mlib.polygon.getTriangleHeight |
||||||
|
- Gives the height of a triangle. |
||||||
|
- Synopses: |
||||||
|
- `height = mlib.polygon.getTriangleHeigh( base, x1, y1, x2, y2, x3, y3 )` |
||||||
|
- `height = mlib.polygon.getTriangleHeight( base, area )` |
||||||
|
- Arguments: |
||||||
|
- `base`: Number. The length of the base of the triangle. |
||||||
|
- `x1`, `y1`, `x2`, `y2`, `x3`, `y3`: Numbers. The x and y coordinates of the triangle. |
||||||
|
- `area`: Number. The regular area of the triangle. __Not__ the signed area. |
||||||
|
- Returns: |
||||||
|
- `height`: Number. The height of the triangle. |
||||||
|
|
||||||
|
##### mlib.polygon.isCircleInside |
||||||
|
- Checks if a circle is inside the polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.isCircleInside( cx, cy, radius, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.isCircleInside( cx, cy, radius, ... )` |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates for the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the circle is inside the polygon. |
||||||
|
- `false` if the circle is not inside the polygon. |
||||||
|
- Notes: |
||||||
|
- Only returns true if the center of the circle is inside the circle. |
||||||
|
|
||||||
|
##### mlib.polygon.isCircleCompletelyInside |
||||||
|
- Checks if a circle is completely inside the polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.isCircleCompletelyInside( cx, cy, radius, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.isCircleCompletelyInside( cx, cy, radius, ... )` |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates for the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the circle is __completely__ inside the polygon. |
||||||
|
- `false` if the circle is not inside the polygon. |
||||||
|
|
||||||
|
##### mlib.polygon.isPolygonInside |
||||||
|
- Checks if a polygon is inside a polygon. |
||||||
|
- Synopsis: |
||||||
|
- `inPolygon = mlib.polygon.isPolygonInside( polygon1, polygon2 )` |
||||||
|
- Arguments: |
||||||
|
- `polygon1`: Table. The vertices of the first polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `polygon2`: Table. The vertices of the second polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the `polygon2` is inside of `polygon1`. |
||||||
|
- `false` if `polygon2` is not inside of `polygon2`. |
||||||
|
- Notes: |
||||||
|
- Returns true as long as any of the line segments of `polygon2` are inside of the `polygon1`. |
||||||
|
|
||||||
|
##### mlib.polygon.isPolygonCompletelyInside |
||||||
|
- Checks if a polygon is completely inside a polygon. |
||||||
|
- Synopsis: |
||||||
|
- `inPolygon = mlib.polygon.isPolygonCompletelyInside( polygon1, polygon2 )` |
||||||
|
- Arguments: |
||||||
|
- `polygon1`: Table. The vertices of the first polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `polygon2`: Table. The vertices of the second polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the `polygon2` is __completely__ inside of `polygon1`. |
||||||
|
- `false` if `polygon2` is not inside of `polygon2`. |
||||||
|
|
||||||
|
##### mlib.polygon.isSegmentInside |
||||||
|
- Checks if a line segment is inside a polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.isSegmentInside( x1, y1, x2, y2, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.isSegmentInside( x1, y1, x2, y2, ... )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. The x and y coordinates of the line segment. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the line segment is inside the polygon. |
||||||
|
- `false` if the line segment is not inside the polygon. |
||||||
|
- Note: |
||||||
|
- Only one of the points has to be in the polygon to be considered 'inside' of the polygon. |
||||||
|
- This is really just a faster version of [mlib.polygon.getPolygonIntersection](#mlibpolygongetpolygonintersection) that does not give the points of intersection. |
||||||
|
|
||||||
|
##### mlib.polygon.isSegmentCompletelyInside |
||||||
|
- Checks if a line segment is completely inside a polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.isSegmentCompletelyInside( x1, y1, x2, y2, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.isSegmentCompletelyInside( x1, y1, x2, y2, ... )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. The x and y coordinates of the line segment. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the line segment is __completely__ inside the polygon. |
||||||
|
- `false` if the line segment is not inside the polygon. |
||||||
|
|
||||||
|
#### mlib.circle |
||||||
|
- Handles aspects involving circles. |
||||||
|
|
||||||
|
##### mlib.circle.checkPoint |
||||||
|
- Checks if a point is on the inside or on the edge the circle. |
||||||
|
- Synopsis: |
||||||
|
- `inCircle = mlib.circle.checkPoint( px, px, cx, cy, radius )` |
||||||
|
- Arguments: |
||||||
|
- `px`, `py`: Numbers. The x and y coordinates of the point being tested. |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates of the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- Returns: |
||||||
|
- `inCircle`: Boolean. |
||||||
|
- `true` if the point is inside or on the circle. |
||||||
|
- `false` if the point is outside of the circle. |
||||||
|
|
||||||
|
##### mlib.circle.getArea |
||||||
|
- Gives the area of a circle. |
||||||
|
- Synopsis: |
||||||
|
- `area = mlib.circle.getArea( radius )` |
||||||
|
- Arguments: |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- Returns: |
||||||
|
- `area`: Number. The area of the circle. |
||||||
|
|
||||||
|
##### mlib.circle.getCircleIntersection |
||||||
|
- Gives the intersections of two circles. |
||||||
|
- Synopsis: |
||||||
|
- `intersections = mlib.circle.getCircleIntersection( c1x, c1y, radius1, c2x, c2y, radius2 ) |
||||||
|
- Arguments: |
||||||
|
- `c1x`, `c1y`: Numbers. The x and y coordinate of the first circle. |
||||||
|
- `radius1`: Number. The radius of the first circle. |
||||||
|
- `c2x`, `c2y`: Numbers. The x and y coordinate of the second circle. |
||||||
|
- `radius2`: Number. The radius of the second circle. |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. A table that contains the type and where the circle collides. See the [specs](spec.lua# L698) for more. |
||||||
|
|
||||||
|
##### mlib.circle.getCircumference |
||||||
|
- Returns the circumference of a circle. |
||||||
|
- Synopsis: |
||||||
|
- `circumference = mlib.circle.getCircumference( radius )` |
||||||
|
- Arguments: |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- Returns: |
||||||
|
- `circumference`: Number. The circumference of a circle. |
||||||
|
|
||||||
|
##### mlib.circle.getLineIntersection |
||||||
|
- Returns the intersections of a circle and a line. |
||||||
|
- Synopsis: |
||||||
|
- `intersections = mlib.circle.getLineIntersections( cx, cy, radius, x1, y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates for the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. Two x and y coordinates the lie on the line. |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. A table with the type and where the intersections happened. Table is formatted: |
||||||
|
- `type`, `x1`, `y1`, `x2`, `y2` |
||||||
|
- String (`'secant'`), Number, Number, Number, Number |
||||||
|
- The numbers are the x and y coordinates where the line intersects the circle. |
||||||
|
- String (`'tangent'`), Number, Number, Boolean (`nil`), Boolean (`nil`) |
||||||
|
- `x1` and `x2` represent where the line intersects the circle. |
||||||
|
- Boolean (`false`), Boolean (`nil`), Boolean (`nil`), Boolean (`nil`), Boolean (`nil`) |
||||||
|
- No intersection. |
||||||
|
- For more see the [specs](spec.lua# L660). |
||||||
|
|
||||||
|
##### mlib.circle.getSegmentIntersection |
||||||
|
- Returns the intersections of a circle and a line segment. |
||||||
|
- Synopsis: |
||||||
|
- `intersections = mlib.circle.getSegmentIntersections( cx, cy, radius, x1, y1, x2, y2 )` |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates for the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `x1`, `y1`, `x2`, `y2`: Numbers. The two x and y coordinates of the line segment. |
||||||
|
- Returns: |
||||||
|
- `intersections`: Table. A table with the type and where the intersections happened. Table is formatted: |
||||||
|
- `type`, `x1`, `y1`, `x2`, `y2` |
||||||
|
- String (`'chord'`), Number, Number, Number, Number |
||||||
|
- The numbers are the x and y coordinates where the line segment is on both edges of the circle. |
||||||
|
- String (`'enclosed'`), Number, Number, Number, Number |
||||||
|
- The numbers are the x and y coordinates of the line segment if it is fully inside of the circle. |
||||||
|
- String (`'secant'`), Number, Number, Number, Number |
||||||
|
- The numbers are the x and y coordinates where the line segment intersects the circle. |
||||||
|
- String (`'tangent'`), Number, Number, Boolean (`nil`), Boolean (`nil`) |
||||||
|
- `x1` and `x2` represent where the line segment intersects the circle. |
||||||
|
- Boolean (`false`), Boolean (`nil`), Boolean (`nil`), Boolean (`nil`), Boolean (`nil`) |
||||||
|
- No intersection. |
||||||
|
- For more see the [specs](spec.lua# L676). |
||||||
|
|
||||||
|
##### mlib.circle.isCircleCompletelyInside |
||||||
|
- Checks if one circle is completely inside of another circle. |
||||||
|
- Synopsis: |
||||||
|
- `completelyInside = mlib.circle.isCircleCompletelyInside( c1x, c1y, c1radius, c2x, c2y, c2radius )` |
||||||
|
- Arguments: |
||||||
|
- `c1x`, `c1y`: Numbers. The x and y coordinates of the first circle. |
||||||
|
- `c1radius`: Number. The radius of the first circle. |
||||||
|
- `c2x`, `c2y`: Numbers. The x and y coordinates of the second circle. |
||||||
|
- `c2radius`: Number. The radius of the second circle. |
||||||
|
- Returns: |
||||||
|
- `completelyInside`: Boolean. |
||||||
|
- `true` if circle1 is inside of circle2. |
||||||
|
- `false` if circle1 is not __completely__ inside of circle2. |
||||||
|
|
||||||
|
##### mlib.circle.isCircleCompletelyInsidePolygon |
||||||
|
- Checks if a circle is completely inside the polygon. |
||||||
|
- Synopses: |
||||||
|
- `inPolygon = mlib.polygon.isCircleCompletelyInside( cx, cy, radius, vertices )` |
||||||
|
- `inPolygon = mlib.polygon.isCircleCompletelyInside( cx, cy, radius, ... )` |
||||||
|
- Arguments: |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates for the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- `vertices`: Table. The vertices of the polygon in the format `{ x1, y1, x2, y2, x3, y3, ... }` |
||||||
|
- `...`: Numbers. The x and y coordinates of the polygon. (Same as using `unpack( vertices )`) |
||||||
|
- Returns: |
||||||
|
- `inPolygon`: Boolean. |
||||||
|
- `true` if the circle is __completely__ inside the polygon. |
||||||
|
- `false` if the circle is not inside the polygon. |
||||||
|
|
||||||
|
##### mlib.circle.isPointOnCircle |
||||||
|
- Checks if a point is __exactly__ on the edge of the circle. |
||||||
|
- Synopsis: |
||||||
|
- `onCircle = mlib.circle.checkPoint( px, px, cx, cy, radius )` |
||||||
|
- Arguments: |
||||||
|
- `px`, `py`: Numbers. The x and y coordinates of the point being tested. |
||||||
|
- `cx`, `cy`: Numbers. The x and y coordinates of the center of the circle. |
||||||
|
- `radius`: Number. The radius of the circle. |
||||||
|
- Returns: |
||||||
|
- `onCircle`: Boolean. |
||||||
|
- `true` if the point is on the circle. |
||||||
|
- `false` if the point is on the inside or outside of the circle. |
||||||
|
- Notes: |
||||||
|
- Will return false if the point is inside __or__ outside of the circle. |
||||||
|
|
||||||
|
##### mlib.circle.isPolygonCompletelyInside |
||||||
|
- Checks if a polygon is completely inside of a circle. |
||||||
|
- Synopsis: |
||||||
|
- `completelyInside = mlib.circle.isPolygonCompletelyInside( circleX, circleY, circleRadius, vertices )` |
||||||
|
- `completelyInside = mlib.circle.isPolygonCompletelyInside( circleX, circleY, circleRadius, ... )` |
||||||
|
- Arguments: |
||||||
|
- `circleX`, `circleY`: Numbers. The x and y coordinates of the circle. |
||||||
|
- `circleRadius`: Number. The radius of the circle. |
||||||
|
- `vertices`: Table. A table containing all of the vertices of the polygon. |
||||||
|
- `...`: Numbers. All of the points of the polygon. |
||||||
|
- Returns: |
||||||
|
- `completelyInside`: Boolean. |
||||||
|
- `true` if the polygon is inside of the circle. |
||||||
|
- `false` if the polygon is not __completely__ inside of the circle. |
||||||
|
|
||||||
|
#### mlib.statistics |
||||||
|
- Handles statistical aspects of math. |
||||||
|
|
||||||
|
##### mlib.statistics.getCentralTendency |
||||||
|
- Gets the central tendency of the data. |
||||||
|
- Synopses: |
||||||
|
- `modes, occurrences, median, mean = mlib.statistics.getCentralTendency( data )` |
||||||
|
- `modes, occurrences, median, mean = mlib.statistics.getCentralTendency( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `modes, occurrences`: Table, Number. The modes of the data and the number of times it occurs. See [mlib.statistics.getMode](#mlibstatisticsgetmode). |
||||||
|
- `median`: Number. The median of the data set. |
||||||
|
- `mean`: Number. The mean of the data set. |
||||||
|
|
||||||
|
##### mlib.statistics.getDispersion |
||||||
|
- Gets the dispersion of the data. |
||||||
|
- Synopses: |
||||||
|
- `variationRatio, range, standardDeviation = mlib.statistics.getDispersion( data )` |
||||||
|
- `variationRatio, range, standardDeviation = mlib.statistics.getDispersion( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `variationRatio`: Number. The variation ratio of the data set. |
||||||
|
- `range`: Number. The range of the data set. |
||||||
|
- `standardDeviation`: Number. The standard deviation of the data set. |
||||||
|
|
||||||
|
##### mlib.statistics.getMean |
||||||
|
- Gets the arithmetic mean of the data. |
||||||
|
- Synopses: |
||||||
|
- `mean = mlib.statistics.getMean( data )` |
||||||
|
- `mean = mlib.statistics.getMean( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `mean`: Number. The arithmetic mean of the data set. |
||||||
|
|
||||||
|
##### mlib.statistics.getMedian |
||||||
|
- Gets the median of the data set. |
||||||
|
- Synopses: |
||||||
|
- `median = mlib.statistics.getMedian( data )` |
||||||
|
- `median = mlib.statistics.getMedian( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `median`: Number. The median of the data. |
||||||
|
|
||||||
|
##### mlib.statistics.getMode |
||||||
|
- Gets the mode of the data set. |
||||||
|
- Synopses: |
||||||
|
- `mode, occurrences = mlib.statistics.getMode( data )` |
||||||
|
- `mode, occurrences = mlib.statistics.getMode( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `mode`: Table. The mode(s) of the data. |
||||||
|
- `occurrences`: Number. The number of time the mode(s) occur. |
||||||
|
|
||||||
|
##### mlib.statistics.getRange |
||||||
|
- Gets the range of the data set. |
||||||
|
- Synopses: |
||||||
|
- `range = mlib.statistics.getRange( data )` |
||||||
|
- `range = mlib.statistics.getRange( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `range`: Number. The range of the data. |
||||||
|
|
||||||
|
##### mlib.statistics.getStandardDeviation |
||||||
|
- Gets the standard deviation of the data. |
||||||
|
- Synopses: |
||||||
|
- `standardDeviation = mlib.statistics.getStandardDeviation( data )` |
||||||
|
- `standardDeviation = mlib.statistics.getStandardDeviation( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `standardDeviation`: Number. The standard deviation of the data set. |
||||||
|
|
||||||
|
##### mlib.statistics.getVariance |
||||||
|
- Gets the variation of the data. |
||||||
|
- Synopses: |
||||||
|
- `variance = mlib.statistics.getVariance( data )` |
||||||
|
- `variance = mlib.statistics.getVariance( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `variance`: Number. The variation of the data set. |
||||||
|
|
||||||
|
##### mlib.statistics.getVariationRatio |
||||||
|
- Gets the variation ratio of the data. |
||||||
|
- Synopses: |
||||||
|
- `variationRatio = mlib.statistics.getVariationRatio( data )` |
||||||
|
- `variationRatio = mlib.statistics.getVariationRatio( ... )` |
||||||
|
- Arguments: |
||||||
|
- `data`: Table. A table containing the values of data. |
||||||
|
- `...`: Numbers. All of the numbers in the data set. |
||||||
|
- Returns: |
||||||
|
- `variationRatio`: Number. The variation ratio of the data set. |
||||||
|
|
||||||
|
#### mlib.math |
||||||
|
- Miscellaneous functions that have no home. |
||||||
|
|
||||||
|
##### mlib.math.getAngle |
||||||
|
- Gets the angle between three points. |
||||||
|
- Synopsis: |
||||||
|
- `angle = mlib.math.getAngle( x1, y1, x2, y2, x3, y3 )` |
||||||
|
- Arguments: |
||||||
|
- `x1`, `y1`: Numbers. The x and y coordinates of the first point. |
||||||
|
- `x2`, `y2`: Numbers. The x and y coordinates of the vertex of the two points. |
||||||
|
- `x3`, `y3`: Numbers. The x and y coordinates of the second point. |
||||||
|
|
||||||
|
##### mlib.math.getPercentage |
||||||
|
- Gets the percentage of a number. |
||||||
|
- Synopsis: |
||||||
|
- `percentage = mlib.math.getPercentage( percent, number )` |
||||||
|
- Arguments: |
||||||
|
- `percent`: Number. The decimal value of the percent (i.e. 100% is 1, 50% is .5). |
||||||
|
- `number`: Number. The number to get the percentage of. |
||||||
|
- Returns: |
||||||
|
- `percentage`: Number. The `percent`age or `number`. |
||||||
|
|
||||||
|
##### mlib.math.getPercentOfChange |
||||||
|
- Gets the percent of change from one to another. |
||||||
|
- Synopsis: |
||||||
|
- `change = mlib.math.getPercentOfChange( old, new )` |
||||||
|
- Arguments: |
||||||
|
- `old`: Number. The original number. |
||||||
|
- `new`: Number. The new number. |
||||||
|
- Returns: |
||||||
|
- `change`: Number. The percent of change from `old` to `new`. |
||||||
|
|
||||||
|
##### mlib.math.getQuadraticRoots |
||||||
|
- Gets the quadratic roots of the the equation. |
||||||
|
- Synopsis: |
||||||
|
- `root1, root2 = mlib.math.getQuadraticRoots( a, b, c )` |
||||||
|
- Arguments: |
||||||
|
- `a`, `b`, `c`: Numbers. The a, b, and c values of the equation `a * x ^ 2 + b * x ^ 2 + c`. |
||||||
|
- Returns: |
||||||
|
- `root1`, `root2`: Numbers. The roots of the equation (where `a * x ^ 2 + b * x ^ 2 + c = 0`). |
||||||
|
|
||||||
|
##### mlib.math.getRoot |
||||||
|
- Gets the `n`th root of a number. |
||||||
|
- Synopsis: |
||||||
|
- `x = mlib.math.getRoot( number, root )` |
||||||
|
- Arguments: |
||||||
|
- `number`: Number. The number to get the root of. |
||||||
|
- `root`: Number. The root. |
||||||
|
- Returns: |
||||||
|
- `x`: The `root`th root of `number`. |
||||||
|
- Example: |
||||||
|
```lua |
||||||
|
local a = mlib.math.getRoot( 4, 2 ) -- Same as saying 'math.pow( 4, .5 )' or 'math.sqrt( 4 )' in this case. |
||||||
|
local b = mlib.math.getRoot( 27, 3 ) |
||||||
|
|
||||||
|
print( a, b ) --> 2, 3 |
||||||
|
``` |
||||||
|
- For more, see the [specs](spec.lua# L860). |
||||||
|
|
||||||
|
##### mlib.math.getSummation |
||||||
|
- Gets the summation of numbers. |
||||||
|
- Synopsis: |
||||||
|
- `summation = mlib.math.getSummation( start, stop, func )` |
||||||
|
- Arguments: |
||||||
|
- `start`: Number. The number at which to start the summation. |
||||||
|
- `stop`: Number. The number at which to stop the summation. |
||||||
|
- `func`: Function. The method to add the numbers. |
||||||
|
- Arguments: |
||||||
|
- `i`: Number. Index. |
||||||
|
- `previous`: Table. The previous values used. |
||||||
|
- Returns: |
||||||
|
- `Summation`: Number. The summation of the numbers. |
||||||
|
- For more, see the [specs](spec.lua# L897). |
||||||
|
|
||||||
|
##### mlib.math.isPrime |
||||||
|
- Checks if a number is prime. |
||||||
|
- Synopsis: |
||||||
|
- `isPrime = mlib.math.isPrime( x )` |
||||||
|
- Arguments: |
||||||
|
- `x`: Number. The number to check if it's prime. |
||||||
|
- Returns: |
||||||
|
- `isPrime`: Boolean. |
||||||
|
- `true` if the number is prime. |
||||||
|
- `false` if the number is not prime. |
||||||
|
|
||||||
|
##### mlib.math.round |
||||||
|
- Rounds a number to the given decimal place. |
||||||
|
- Synopsis: |
||||||
|
- `rounded = mlib.math.round( number, [place] ) |
||||||
|
- Arguments: |
||||||
|
- `number`: Number. The number to round. |
||||||
|
- `place (1)`: Number. The decimal place to round to. Defaults to 1. |
||||||
|
- Returns: |
||||||
|
- The rounded number. |
||||||
|
- For more, see the [specs](spec.lua# L881). |
||||||
|
|
||||||
|
#### Aliases |
||||||
|
| Alias | Corresponding Function | |
||||||
|
| ----------------------------------------------|:---------------------------------------------------------------------------------:| |
||||||
|
| milb.line.getDistance | [mlib.line.getLength](#mliblinegetlength) | |
||||||
|
| mlib.line.getCircleIntersection | [mlib.circle.getLineIntersection](#mlibcirclegetlineintersection) | |
||||||
|
| milb.line.getPolygonIntersection | [mlib.polygon.getLineIntersection](#mlibpolygongetlineintersection) | |
||||||
|
| mlib.line.getLineIntersection | [mlib.line.getIntersection](#mliblinegetintersection) | |
||||||
|
| mlib.segment.getCircleIntersection | [mlib.circle.getSegmentIntersection](#mlibcirclegetsegmentintersection) | |
||||||
|
| milb.segment.getPolygonIntersection | [mlib.pollygon.getSegmentIntersection](#mlibpollygongetsegmentintersection) | |
||||||
|
| mlib.segment.getLineIntersection | [mlib.line.getSegmentIntersection](#mliblinegetsegmentintersection) | |
||||||
|
| mlib.segment.getSegmentIntersection | [mlib.segment.getIntersection](#mlibsegmentgetintersection) | |
||||||
|
| milb.segment.isSegmentCompletelyInsideCircle | [mlib.circle.isSegmentCompletelyInside](#mlibcircleissegmentcompletelyinside) | |
||||||
|
| mlib.segment.isSegmentCompletelyInsidePolygon | [mlib.polygon.isSegmentCompletelyInside](#mlibpolygonissegmentcompletelyinside) | |
||||||
|
| mlib.circle.getPolygonIntersection | [mlib.polygon.getCircleIntersection](#mlibpolygongetcircleintersection) | |
||||||
|
| mlib.circle.isCircleInsidePolygon | [mlib.polygon.isCircleInside](#mlibpolygoniscircleinside) | |
||||||
|
| mlib.circle.isCircleCompletelyInsidePolygon | [mlib.polygon.isCircleCompletelyInside](#mlibpolygoniscirclecompletelyinside) | |
||||||
|
| mlib.polygon.isCircleCompletelyOver | [mlib.circleisPolygonCompletelyInside](#mlibcircleispolygoncompletelyinside) | |
||||||
|
|
||||||
|
## License |
||||||
|
A math library made in Lua |
||||||
|
copyright (C) 2014 Davis Claiborne |
||||||
|
This program is free software; you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU General Public License as published by |
||||||
|
the Free Software Foundation; either version 2 of the License, or |
||||||
|
(at your option) any later version. |
||||||
|
This program is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU General Public License for more details. |
||||||
|
You should have received a copy of the GNU General Public License along |
||||||
|
with this program; if not, write to the Free Software Foundation, Inc., |
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||||||
|
Contact me at davisclaib at gmail.com |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,60 @@ |
|||||||
|
function love.load() |
||||||
|
Object = require("classic") |
||||||
|
require("player") |
||||||
|
require("bullet") |
||||||
|
WF = require("libs/windfield") |
||||||
|
require("UpdateGame") |
||||||
|
require("DrawGame") |
||||||
|
require("KeyPressed") |
||||||
|
|
||||||
|
love.profiler = require("libs/profile") |
||||||
|
love.profiler.start() |
||||||
|
|
||||||
|
--WindField |
||||||
|
World = WF.newWorld(0, 0) --no gravity |
||||||
|
World:setQueryDebugDrawing(true) -- Draws the area of a query for 10 frames |
||||||
|
World:addCollisionClass("Player1") |
||||||
|
World:addCollisionClass("Bullet1") |
||||||
|
|
||||||
|
World:addCollisionClass("Player2") |
||||||
|
World:addCollisionClass("Bullet2") |
||||||
|
|
||||||
|
HEALTH = 3 |
||||||
|
DELAY = 0.5 |
||||||
|
DebugFlag = false |
||||||
|
EnableKeyPress1 = true |
||||||
|
KeyPressTime1 = 0 |
||||||
|
KeyDelay1 = DELAY |
||||||
|
EnableKeyPress2 = true |
||||||
|
KeyPressTime2 = 0 |
||||||
|
KeyDelay2 = DELAY |
||||||
|
|
||||||
|
UserPlayer1 = Player(1, 1000, 100, HEALTH, "assets/player1.png", 100) |
||||||
|
UserPlayer2 = Player(2, 800, 300, HEALTH, "assets/player2.png", 100) |
||||||
|
Bullets1 = {} |
||||||
|
Bullets2 = {} |
||||||
|
end |
||||||
|
|
||||||
|
function love.keypressed(key) |
||||||
|
KeyPressed(key) |
||||||
|
end |
||||||
|
|
||||||
|
love.frame = 0 |
||||||
|
function love.update(dt) |
||||||
|
--[[ |
||||||
|
love.frame = love.frame + 1 |
||||||
|
if love.frame % 100 == 0 then |
||||||
|
love.report = love.profiler.report(20) |
||||||
|
love.profiler.reset() |
||||||
|
end |
||||||
|
]] |
||||||
|
UpdateGame(dt) |
||||||
|
end |
||||||
|
|
||||||
|
function love.draw() |
||||||
|
DrawGame() |
||||||
|
if DebugFlag then |
||||||
|
love.graphics.print("Debug Mode", 1200, 850) |
||||||
|
-- love.graphics.print(love.report or "Please wait...") |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,104 @@ |
|||||||
|
Player = Object:extend() |
||||||
|
|
||||||
|
function Player:new(p, x, y, health, image, speed) |
||||||
|
self.p = p |
||||||
|
self.image = love.graphics.newImage(image) |
||||||
|
self.x = x |
||||||
|
self.y = y |
||||||
|
self.health = health |
||||||
|
self.speed = speed |
||||||
|
self.width = self.image:getWidth() |
||||||
|
self.height = self.image:getHeight() |
||||||
|
|
||||||
|
--Collision Stuff |
||||||
|
self.collider = World:newBSGRectangleCollider(x, y, 64, 64, 4) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
if self.p == 1 then |
||||||
|
self.collider:setCollisionClass("Player1") |
||||||
|
elseif self.p == 2 then |
||||||
|
self.collider:setCollisionClass("Player2") |
||||||
|
end |
||||||
|
self.collider:setType("static") |
||||||
|
self.collider:setObject(self) |
||||||
|
|
||||||
|
--Rotation Stuff |
||||||
|
self.rotation = math.rad(90) |
||||||
|
self.rotSpeed = 2 |
||||||
|
self.scaleX = 1 |
||||||
|
self.scaleY = 1 |
||||||
|
self.originX = self.width / 2 |
||||||
|
self.originY = self.height / 2 |
||||||
|
end |
||||||
|
|
||||||
|
function Player:update(dt) |
||||||
|
local cos = math.cos |
||||||
|
local sin = math.sin --optimisation |
||||||
|
local bulletSpeed = 300 |
||||||
|
|
||||||
|
if self.p == 1 then |
||||||
|
if love.keyboard.isDown("w") then |
||||||
|
self.x = self.x + cos(self.rotation) * (self.speed * dt) |
||||||
|
self.y = self.y + sin(self.rotation) * (self.speed * dt) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
elseif love.keyboard.isDown("s") then |
||||||
|
self.x = self.x - cos(self.rotation) * (self.speed * dt) |
||||||
|
self.y = self.y - sin(self.rotation) * (self.speed * dt) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
elseif love.keyboard.isDown("a") then |
||||||
|
self.rotation = self.rotation - (self.rotSpeed * dt) |
||||||
|
elseif love.keyboard.isDown("d") then |
||||||
|
self.rotation = self.rotation + (self.rotSpeed * dt) |
||||||
|
end |
||||||
|
|
||||||
|
if EnableKeyPress1 == true then |
||||||
|
if love.keyboard.isDown("space") then |
||||||
|
local offsetX = math.cos(self.rotation) * self.width / 2 |
||||||
|
local offsetY = math.sin(self.rotation) * self.height / 2 |
||||||
|
local bulletX = self.x + offsetX |
||||||
|
local bulletY = self.y + offsetY |
||||||
|
local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) |
||||||
|
table.insert(Bullets1, newBullet) |
||||||
|
KeyPressTime1 = KeyDelay1 |
||||||
|
EnableKeyPress1 = false |
||||||
|
end |
||||||
|
end |
||||||
|
--Query Collision |
||||||
|
if self.collider:enter("Player2") then |
||||||
|
local collision_data = self.collider:getEnterCollisionData("Player2") |
||||||
|
print(collision_data) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
if self.p == 2 then |
||||||
|
if love.keyboard.isDown("up") then |
||||||
|
self.x = self.x + cos(self.rotation) * (self.speed * dt) |
||||||
|
self.y = self.y + sin(self.rotation) * (self.speed * dt) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
elseif love.keyboard.isDown("down") then |
||||||
|
self.x = self.x - cos(self.rotation) * (self.speed * dt) |
||||||
|
self.y = self.y - sin(self.rotation) * (self.speed * dt) |
||||||
|
self.collider:setPosition(self.x, self.y) |
||||||
|
elseif love.keyboard.isDown("left") then |
||||||
|
self.rotation = self.rotation - (self.rotSpeed * dt) |
||||||
|
elseif love.keyboard.isDown("right") then |
||||||
|
self.rotation = self.rotation + (self.rotSpeed * dt) |
||||||
|
end |
||||||
|
|
||||||
|
if EnableKeyPress2 == true then |
||||||
|
if love.keyboard.isDown("return") then |
||||||
|
local offsetX = math.cos(self.rotation) * self.width / 2 |
||||||
|
local offsetY = math.sin(self.rotation) * self.height / 2 |
||||||
|
local bulletX = self.x + offsetX |
||||||
|
local bulletY = self.y + offsetY |
||||||
|
local newBullet = Bullet(bulletX, bulletY, self.p, bulletSpeed, self.rotation) |
||||||
|
table.insert(Bullets2, newBullet) |
||||||
|
KeyPressTime2 = KeyDelay2 |
||||||
|
EnableKeyPress2 = false |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
function Player:draw() |
||||||
|
love.graphics.draw(self.image, self.x, self.y, self.rotation, self.scaleX, self.scaleY, self.originX, self.originY) |
||||||
|
end |
Loading…
Reference in new issue