Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- PHYSICS LIBRARY THING
- WRITTEN BY MAURICE GUÉGAN FOR MARI0
- DON'T STEAL MY SHIT
- Licensed under the same license as the game itself.
- http://creativecommons.org/licenses/by-nc-sa/3.0/
- ]]--
- function physicsupdate(dt)
- local lobjects = objects
- for j, w in pairs(lobjects) do
- if j ~= "tile" then
- for i, v in pairs(w) do
- if v.static == false and v.active then
- --GRAVITY
- v.speedy = v.speedy + (v.gravity or yacceleration)*dt
- if v.speedy > maxyspeed then
- v.speedy = maxyspeed
- end
- --COLLISIONS ROFL
- local horcollision = false
- local vercollision = false
- --VS OTHER OBJECTS --but not portalwall
- for h, u in pairs(lobjects) do
- if h ~= "tile" and h ~= "portalwall" then
- local hor, ver = handlegroup(i, h, u, v, j, dt)
- if hor then
- horcollision = true
- end
- if ver then
- vercollision = true
- end
- end
- end
- --VS TILES (Because I only wanna check close ones)
- local xstart = math.floor(v.x+v.speedx*dt-2/16)+1
- local ystart = math.floor(v.y+v.speedy*dt-2/16)+1
- if tiledebug then
- debugchecked = {}
- end
- for x = xstart, xstart+math.ceil(v.width) do
- for y = ystart, ystart+math.ceil(v.height) do
- if tiledebug then
- table.insert(debugchecked, x)
- table.insert(debugchecked, y)
- deb = false
- end
- local t = lobjects["tile"][x .. "-" .. y]
- if t then
- -- Same object Active Not masked
- if (i ~= g or j ~= h) and t.active and v.mask[t.category] ~= true then
- local collision1, collision2 = checkcollision(v, t, "tile", x .. "-" .. y, j, i, dt)
- if collision1 then
- horcollision = true
- if tiledebug then
- deb = true
- end
- elseif collision2 then
- vercollision = true
- if tiledebug then
- deb = true
- end
- end
- end
- end
- if tiledebug then
- table.insert(debugchecked, deb)
- end
- end
- end
- --VS PORTALWALL
- local h = portalwall
- local u = objects["portalwall"]
- local hor, ver = handlegroup(i, h, u, v, j, dt)
- if hor then
- horcollision = true
- end
- if ver then
- vercollision = true
- end
- --Check for emancipation grill
- if v.emancipatecheck then
- for h, u in pairs(emancipationgrills) do
- if u.dir == "hor" then
- if inrange(v.x+6/16, u.startx-1, u.endx, true) and inrange(u.y-14/16, v.y, v.y+v.speedy*dt, true) then
- v:emancipate(h)
- end
- else
- if inrange(v.y+6/16, u.starty-1, u.endy, true) and inrange(u.x-14/16, v.x, v.x+v.speedx*dt, true) then
- v:emancipate(h)
- end
- end
- end
- end
- --Move the object
- if vercollision == false then
- v.y = v.y + v.speedy*dt
- if v.gravity then
- if v.speedy == v.gravity*dt and v.startfall then
- v:startfall(i)
- end
- else
- if v.speedy == yacceleration*dt and v.startfall then
- v:startfall(i)
- end
- end
- end
- if horcollision == false then
- v.x = v.x + v.speedx*dt
- end
- end
- end
- end
- end
- end
- function handlegroup(i, h, u, v, j, dt)
- local horcollision = false
- local vercollision = false
- for g, t in pairs(u) do
- -- Same object? Active Not masked
- if (i ~= g or j ~= h) and t.active and (v.mask == nil or v.mask[t.category] ~= true) and (t.mask == nil or t.mask[v.category] ~= true) then
- local collision1, collision2 = checkcollision(v, t, h, g, j, i, dt)
- if collision1 then
- horcollision = true
- elseif collision2 then
- vercollision = true
- end
- end
- end
- return horcollision, vercollision
- end
- function passivecollision(v, t, h, g, j, i, dt)
- if v.passivecollide then
- v:passivecollide(h, t)
- if t.passivecollide then
- t:passivecollide(j, v)
- end
- else
- if v.floorcollide then
- if v:floorcollide(h, t, dt) ~= false then
- if v.speedy > 0 then
- v.speedy = 0
- end
- v.y = t.y - v.height
- return true
- end
- else
- if v.speedy > 0 then
- v.speedy = 0
- end
- v.y = t.y - v.height
- return true
- end
- end
- return false
- end
- function horcollision(v, t, h, g, j, i, dt)
- if v.speedx < 0 then
- --move object RIGHT (because it was moving left)
- if t.rightcollide then
- if t:rightcollide(j, v) ~= false then
- if t.speedx and t.speedx > 0 then
- t.speedx = 0
- end
- end
- else
- if t.speedx and t.speedx > 0 then
- t.speedx = 0
- end
- end
- if v.leftcollide then
- if v:leftcollide(h, t) ~= false then
- if v.speedx < 0 then
- v.speedx = 0
- end
- v.x = t.x + t.width
- return true
- end
- else
- if v.speedx < 0 then
- v.speedx = 0
- end
- v.x = t.x + t.width
- return true
- end
- else
- --move object LEFT (because it was moving right)
- if t.leftcollide then
- if t:leftcollide(j, v) ~= false then
- if t.speedx and t.speedx < 0 then
- t.speedx = 0
- end
- end
- else
- if t.speedx and t.speedx < 0 then
- t.speedx = 0
- end
- end
- if v.rightcollide then
- if v:rightcollide(h, t) ~= false then
- if v.speedx > 0 then
- v.speedx = 0
- end
- v.x = t.x - v.width
- return true
- end
- else
- if v.speedx > 0 then
- v.speedx = 0
- end
- v.x = t.x - v.width
- return true
- end
- end
- return false
- end
- function vercollision(v, t, h, g, j, i, dt)
- if v.speedy < 0 then
- --move object DOWN (because it was moving up)
- if t.floorcollide then
- if t:floorcollide(j, v) ~= false then
- if t.speedy and t.speedy > 0 then
- t.speedy = 0
- end
- end
- else
- if t.speedy and t.speedy > 0 then
- t.speedy = 0
- end
- end
- if v.ceilcollide then
- if v:ceilcollide(h, t) ~= false then
- if v.speedy < 0 then
- v.speedy = 0
- end
- v.y = t.y + t.height
- return true
- end
- else
- if v.speedy < 0 then
- v.speedy = 0
- end
- v.y = t.y + t.height
- return true
- end
- else
- if t.ceilcollide then
- if t:ceilcollide(j, v) ~= false then
- if t.speedy and t.speedy < 0 then
- t.speedy = 0
- end
- end
- else
- if t.speedy and t.speedy < 0 then
- t.speedy = 0
- end
- end
- if v.floorcollide then
- if v:floorcollide(h, t, dt) ~= false then
- if v.speedy > 0 then
- v.speedy = 0
- end
- v.y = t.y - v.height
- return true
- end
- else
- if v.speedy > 0 then
- v.speedy = 0
- end
- v.y = t.y - v.height
- return true
- end
- end
- return false
- end
- function checkcollision(v, t, h, g, j, i, dt) --v: b1table | t: b2table | h: b2type | g: b2id | j: b1type | i: b1id
- local hadhorcollision = false
- local hadvercollision = false
- if true then --math.abs(v.x-t.x)+math.abs(v.y-t.y) < 5 then
- --check if it's a passive collision (Object is colliding anyway)
- if aabb(v.x, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --passive collision! (oh noes!)
- if passivecollision(v, t, h, g, j, i, dt) then
- hadvercollision = true
- end
- elseif aabb(v.x + v.speedx*dt, v.y + v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then
- if aabb(v.x + v.speedx*dt, v.y, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is horizontal!
- if horcollision(v, t, h, g, j, i, dt) then
- hadhorcollision = true
- end
- elseif aabb(v.x, v.y+v.speedy*dt, v.width, v.height, t.x, t.y, t.width, t.height) then --Collision is vertical!
- if vercollision(v, t, h, g, j, i, dt) then
- hadvercollision = true
- end
- else
- --We're fucked, it's a diagonal collision! run!
- --Okay actually let's take this slow okay. Let's just see if we're moving faster horizontally than vertically, aight?
- local grav = yacceleration
- if self and self.gravity then
- grav = self.gravity
- end
- if math.abs(v.speedy-grav*dt) < math.abs(v.speedx) then
- --vertical collision it is.
- if vercollision(v, t, h, g, j, i, dt) then
- hadvercollision = true
- end
- else
- --okay so we're moving mainly vertically, so let's just pretend it was a horizontal collision? aight cool.
- if horcollision(v, t, h, g, j, i, dt) then
- hadhorcollision = true
- end
- end
- end
- end
- end
- return hadhorcollision, hadvercollision
- end
- function aabb(ax, ay, awidth, aheight, bx, by, bwidth, bheight)
- return ax+awidth > bx and ax < bx+bwidth and ay+aheight > by and ay < by+bheight
- end
- function aabbOLD(ax, ay, awidth, aheight, bx, by, bwidth, bheight) --old.
- if math.abs((ay+aheight/2) - (by+bheight/2)) < bheight/2+aheight/2 and
- math.abs((ax+awidth/2) - (bx+bwidth/2)) < bwidth/2+awidth/2 then
- return true
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement