From c43317bfe283346c06751f82356e5e16ca09387f Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 28 Apr 2019 09:31:10 +0200 Subject: [PATCH] modules/world: add friction system to Actor2D --- gamecore/modules/world/actors/actor2D.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 250aa58..b6b3a31 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -61,6 +61,9 @@ function Actor2D:initPhysics(x, y, w, h, isSolid) self.xsp = 0 self.ysp = 0 + self.xfrc = 0 + self.yfrc = 0 + self.isSolid = isSolid or false self:setFilter() @@ -105,6 +108,13 @@ end function Actor2D:autoMove(dt) local newx, newy, cols, colNumber = self:move(self.x + self.xsp * dt, self.y + self.ysp * dt) + + -- apply after the movement the friction, until the player stop + -- note: the friction is applied according to the delta time, + -- thus the friction should be how much speed is substracted in 1 second + + self.xsp = utils.math.toZero(self.xsp, self.xfrc * dt) + self.ysp = utils.math.toZero(self.ysp, self.yfrc * dt) end function Actor2D:move(dx, dy)