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)
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y)
self:checkGround(col.normal.x, col.normal.y)
end
end
end
@ -159,13 +158,29 @@ function Actor2D:changeSpeedToCollisionNormal(nx, ny)
self.xsp, self.ysp = xsp, ysp
end
function Actor2D:checkGround(nx, ny)
if not (self.xgrav == 0) then
if nx ~= utils.math.sign(self.xgrav) then self.onGround = true end
end
function Actor2D:checkGroundX()
local dx, dy = self.x + utils.math.sign(self.xgrav), self.y
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 ny ~= utils.math.sign(self.ygrav) then self.onGround = true end
if col.normal.x ~= utils.math.sign(self.xgrav) then self.onGround = true 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 col.normal.y ~= utils.math.sign(self.ygrav) then self.onGround = true end
end
end
end
end
@ -200,6 +215,14 @@ end
function Actor2D:applyGravity(dt)
self.xsp = self.xsp + self.xgrav * 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
-- DRAW FUNCTIONS