-
Notifications
You must be signed in to change notification settings - Fork 0
/
physicsobject.lua
37 lines (32 loc) · 973 Bytes
/
physicsobject.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require("lovepunk.entity")
PhysicsObject = Entity.new(0, 0, 1, 1)
PhysicsObject.__index = PhysicsObject
function PhysicsObject.new(x, y, w, h)
local self = setmetatable({}, PhysicsObject)
self.x = x
self.y = y
self.width = w
self.height = h
self.v = {x=0, y=0}
self.filters = {["level"]="slide"}
self.collideTypes = {"level"}
self.bounciness=1.1
return self
end
function PhysicsObject:update(dt)
local actualX,actualY,cols = self.scene.bumpWorld:move(self, self.x+self.v.x, self.y+self.v.y, getFilter)
for _, col in pairs(cols) do
for _, type in pairs(self.collideTypes) do
if col.other.type == type then
self.v.x = self.v.x + (col.normal.x)*math.abs(self.v.x)*self.bounciness
self.v.y = self.v.y + (col.normal.y)*math.abs(self.v.y)*self.bounciness
end
end
end
self.x = actualX
self.y = actualY
end
function getFilter(item,other)
if (item.filters[other.type] == nil) then return "cross"
else return item.filters[other.type] end
end