My current problem is that I don't seem to be able to handle "dt" properly. If I set or unset vsync in this simple test, the player moves really differently and I don't get why. I use dt in "CHero:updateSpeedX" when adding acceleration or deceleration constant to speed. I thought I should use it in "CHero:updatePosition" too (changing it to self.x = self.x + (self.spX * dt)) but the result is still very different with vsync on and off.
Would someone please tell me what I'm doing wrong? I'd love finally understanding one of my common mistakes. I bet it must be a very basic thing.
Code: Select all
CHero = class:new()
function CHero:init (x, y)
self.x = x or 300
self.y = y or 210
self.width = gTileSize
self.height = (2 * gTileSize)
self.edgeX = x
self.edgeY = y
self.spX = 0
self.maxSpX = 1000
self.acc = 5
self.dec = 10
self.friction = 5
end
function CHero:update(dt)
self:updateSpeedX(dt)
self:updatePosition(dt)
end
function CHero:updateSpeedX(dt)
if (gKeyPressed.left) then
if (self.spX > 0) then
self.spX = self.spX - (self.dec * dt)
elseif (self.spX > -self.maxSpX) then
self.spX = self.spX - (self.acc * dt)
end
elseif (gKeyPressed.right) then
if (self.spX < 0) then
self.spX = self.spX + (self.dec * dt )
elseif (self.spX < self.maxSpX) then
self.spX = self.spX + (self.acc * dt)
end
else
self.spX = self.spX - math.min(math.abs(self.spX), (self.friction * dt)) * math.sign (self.spX)
end
end
-- UPDATE POSITION
function CHero:updatePosition(dt)
self.x = self.x + self.spX
end
--DRAW
function CHero:draw()
love.graphics.setColor(self.color)
love.graphics.rectangle('fill', self.x, self.y, self.width, -self.height)
love.graphics.setColor(255,255,255)
end