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)