modules/world: add a way to change speed to collision normal

This commit is contained in:
Kazhnuz 2019-04-28 11:09:55 +02:00
parent 0d25dbd839
commit 6c2b308580

View file

@ -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)