improvement(actor): put physics functions that make sense in baseactor

This commit is contained in:
Kazhnuz 2019-06-27 22:14:12 +02:00
parent ff78a6ecfe
commit b4f3008552
2 changed files with 33 additions and 28 deletions

View File

@ -51,7 +51,7 @@ function Actor2D:autoMove(dt)
self:applyGravity(dt)
local dx, dy = self:getFuturePosition(dt)
local newx, newy, cols, colNumber = self:move(self.x + self.xsp * dt, self.y + self.ysp * dt)
local newx, newy, cols, colNumber = self:move(dx, dy)
-- apply after the movement the friction, until the player stop
-- note: the friction is applied according to the delta time,
@ -62,32 +62,9 @@ function Actor2D:autoMove(dt)
self:applyFriction(dt)
end
function Actor2D:getFuturePosition(dt)
local dx, dy
dx = self.x + self.xsp * dt
dy = self.y + self.ysp * dt
end
function Actor2D:applyFriction(dt)
self.xsp = utils.math.toZero(self.xsp, self.xfrc * dt)
self.ysp = utils.math.toZero(self.ysp, self.yfrc * dt)
end
function Actor2D:solveAllCollisions(cols)
for i, col in ipairs(cols) do
self:collisionResponse(col)
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
self:changeSpeedToCollisionNormal(col.normal.x, col.normal.y)
end
end
end
function Actor2D:collisionResponse(collision)
-- here come the response to the collision
end
function Actor2D:changeSpeedToCollisionNormal(nx, ny)
function Actor2D:changeSpeedToCollisionNormal(normal)
local xsp, ysp = self.xsp, self.ysp
local nx, ny = normal.x, normal.y
if (nx < 0 and xsp > 0) or (nx > 0 and xsp < 0) then
xsp = -xsp * self.bounceFactor

View File

@ -117,8 +117,36 @@ function BaseActor:setFilter()
end
end
function BaseActor:initGravity( )
-- Empty placeholder function
function BaseActor:getFuturePosition(dt)
local dx, dy
dx = self.x + self.xsp * dt
dy = self.y + self.ysp * dt
dz = self.z + self.zsp * dt
return dx, dy, dz
end
function BaseActor:applyFriction(dt)
self.xsp = utils.math.toZero(self.xsp, self.xfrc * dt)
self.ysp = utils.math.toZero(self.ysp, self.yfrc * dt)
self.zsp = utils.math.toZero(self.zsp, self.zfrc * dt)
end
function BaseActor:solveAllCollisions(cols)
for i, col in ipairs(cols) do
self:collisionResponse(col)
if (col.type == "touch") or (col.type == "bounce") or (col.type == "slide") then
self:changeSpeedToCollisionNormal(col.normal)
end
end
end
function BaseActor:collisionResponse(collision)
-- here come the response to the collision
end
function BaseActor:changeSpeedToCollisionNormal(normal)
-- Empty function in baseactor
end
-- COORDINATE/MOVEMENT FUNCTIONS