modules/world: use checkCollisions to get if an object is on ground

This commit is contained in:
Kazhnuz 2019-05-05 13:01:25 +02:00
parent b80566b4ec
commit 174b51acfa

View file

@ -136,7 +136,6 @@ function Actor2D:solveAllCollisions(cols)
self:collisionResponse(v) self:collisionResponse(v)
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y) self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y)
self:checkGround(col.normal.x, col.normal.y)
end end
end end
end end
@ -159,13 +158,29 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny)
self.xsp, self.ysp = xsp, ysp self.xsp, self.ysp = xsp, ysp
end end
function Actor2D:checkGround(nx, ny) function Actor2D:checkGroundX()
if not (self.xgrav == 0) then local dx, dy = self.x + utils.math.sign(self.xgrav), self.y
if nx ~= utils.math.sign(self.xgrav) then self.onGround = true end local newx, newy, cols, colNumber = self.world:checkCollision(self, dx, dy, self.filter)
for i, col in ipairs(cols) do
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
if not (self.ygrav == 0) then
if col.normal.x ~= utils.math.sign(self.xgrav) then self.onGround = true end
end
end
end
end end
function Actor2D:checkGroundY()
local dx, dy = self.x, self.y + utils.math.sign(self.ygrav)
local newx, newy, cols, colNumber = self.world:checkCollision(self, dx, dy, self.filter)
for i, col in ipairs(cols) do
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
if not (self.ygrav == 0) then if not (self.ygrav == 0) then
if ny ~= utils.math.sign(self.ygrav) then self.onGround = true end if col.normal.y ~= utils.math.sign(self.ygrav) then self.onGround = true end
end
end
end end
end end
@ -200,6 +215,14 @@ end
function Actor2D:applyGravity(dt) function Actor2D:applyGravity(dt)
self.xsp = self.xsp + self.xgrav * dt self.xsp = self.xsp + self.xgrav * dt
self.ysp = self.ysp + self.ygrav * dt self.ysp = self.ysp + self.ygrav * dt
if utils.math.sign(self.ysp) == utils.math.sign(self.ygrav) then
self:checkGroundY( )
end
if utils.math.sign(self.xsp) == utils.math.sign(self.xgrav) then
self:checkGroundX( )
end
end end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS