From 6c2b3085809cea657e922d92994c4f6c57b5a6fb Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 28 Apr 2019 11:09:55 +0200 Subject: [PATCH] modules/world: add a way to change speed to collision normal --- gamecore/modules/world/actors/actor2D.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index f26e505..a8e79c2 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -64,6 +64,8 @@ function Actor2D:initPhysics(x, y, w, h, isSolid) self.xfrc = 0 self.yfrc = 0 + self.bounceFactor = 0 + self.isSolid = isSolid or false self:setFilter() @@ -120,8 +122,11 @@ function Actor2D:autoMove(dt) end function Actor2D:solveAllCollisions(cols) - for i,v in ipairs(cols) do + for i, col in ipairs(cols) do self:collisionResponse(v) + if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then + self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y) + end end end @@ -129,6 +134,20 @@ function Actor2D:collisionResponse(collision) -- here come the response to the collision end +function Actor2D:changeSpeedToCollisionNormal(nx, ny) + local xsp, ysp = self.xsp, self.ysp + + if (nx < 0 and xsp > 0) or (nx > 0 and xsp < 0) then + xsp = -xsp * self.bounceFactor + end + + if (ny < 0 and ysp > 0) or (ny > 0 and ysp < 0) then + ysp = -ysp * self.bounceFactor + end + + self.xsp, self.ysp = xsp, ysp +end + function Actor2D:move(dx, dy) local cols, colNumber self.x, self.y, cols, colNumber = self.world:moveActor(self, dx, dy, self.filter)