From 174b51acfa5954517be9d0a4e8301e5b6fa01ea9 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 5 May 2019 13:01:25 +0200 Subject: [PATCH] modules/world: use checkCollisions to get if an object is on ground --- gamecore/modules/world/actors/actor2D.lua | 37 ++++++++++++++++++----- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 6437182..a1799ee 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -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) - if not (self.ygrav == 0) then - if ny ~= utils.math.sign(self.ygrav) then self.onGround = true end + 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 + +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