The problem is that when you move the movement isn't really smooth especially not when you move diagonally. Im new to löve and lua so this is the only way of moving my player I know Iäve tried looking at other ways but can't find any, The weird thing is that if I remove
local x = 10
local y = 10
local s = 100
function love.load()
end
function love.update(dt)
if love.keyboard.isDown("w") then
y = y - s * dt
end
if love.keyboard.isDown("s") then
y = y + s * dt
end
if love.keyboard.isDown("a") then
x = x - s * dt
end
if love.keyboard.isDown("d") then
x = x + s * dt
end
end
function love.draw()
love.graphics.rectangle("fill", x, y, 32, 32)
end
I have a feeling this has to do with your computer's specs.
Delta Time is the amount of time passed since the last update, which is directly based on how fast your computer can process and draw each cycle. So if the movement is choppy while using dt, your dt is probably higher than normal.
I've seen Love run great on even toasters though, so either your computer is from the mid 90s or there's a different issue.
player = {
xPos = 0, yPos = 0, --Position of player
xVel = 0, yVel = 0, --Velocity of player
speed = 100, --Acceleration speed of player
maxSpeed = 200, --Max speed of player
friction = 10, --Deleceration of player
}
function player.checkKeys(dt)
if not (love.keyboard.isDown("w") and love.keyboard.isDown("a")) then --Check if both keys arent pressed at the same time.
if love.keyboard.isDown("w") then
player.yVel = player.yVel - (player.speed*dt) --Up
elseif love.keyboard.isDown("s") then
player.yVel = player.yVel + (player.speed*dt) --Down
end
end
if not (love.keyboard.isDown("a") and love.keyboard.isDown("d")) then --Check if both keys arent pressed at the same time.
if love.keyboard.isDown("a") then
player.yVel = player.yVel - (player.speed*dt) --Left
elseif love.keyboard.isDown("d") then
player.yVel = player.yVel + (player.speed*dt) --Right
end
end
end
function player.applyFriction(dt)
if player.xVel > 0 then
player.xVel = math.max(player.xVel-player.friction*dt, 0) --Round to everything above 0, or 0 itself
elseif player.xVel < 0 then
player.xVel = math.min(player.xVel+player.friction*dt, 0) --Round to everything below 0, or 0 itself
end
if player.yVel > 0 then
player.yVel = math.max(player.yVel-player.friction*dt, 0) --Round to everything above 0, or 0 itself
elseif player.yVel < 0 then
player.yVel = math.min(player.yVel+player.friction*dt, 0) --Round to everything below 0, or 0 itself
end
end
function player.limitSpeed(dt)
if player.xVel > 0 then
player.xVel = math.min(player.xVel, player.maxSpeed) --Round to everything below maxSpeed, or maxSpeed itself
elseif player.xVel < 0 then
player.xVel = math.min(player.xVel, player.maxSpeed*-1) --Round to everything below maxSpeed, or maxSpeed itself
end
if player.yVel > 0 then
player.yVel = math.min(player.yVel, player.maxSpeed) --Round to everything below maxSpeed, or maxSpeed itself
elseif player.yVel < 0 then
player.yVel = math.min(player.yVel, player.maxSpeed*-1) --Round to everything below maxSpeed, or maxSpeed itself
end
end
function player.move(dt)
player.xPos = player.xPos + player.xVel*dt
player.yPos = player.yPos + player.yVel*dt
end
function player.update(dt)
player.checkKeys(dt) --Check all the keys and apply velocity accordingly
player.applyFriction(dt)--Apply friction to the player velocity. ||This code uses a fancy trick, ask me if you want it explained||
player.limitSpeed() --Limits the player speed. ||This code is also quite tricky, again, ask if you want it explained||
player.move(dt) --Apply velocity to the player position
end
function player.draw()
love.graphics.rectangle("fill", player.xPos, player.yPos, 32, 32)
end
function player.limitSpeed() -- dt is unnecessary here!
-- get the current speed
local s = math.sqrt(player.xVel^2 + player.yVel^2)
if s > player.maxSpeed then
-- find the scalar "sv" so that: player.vel = player.vel * sv or player.vel = player.vel * 1/s * maxSpeed
local sv = maxSpeed/s
player.xVel = player.xVel * sv
player.yVel = player.yVel * sv
end
end
Also there are simpler ways to implement "damping" which in the code is referred to as "fiction":
function player.applyDamping(dt)
-- where "player.damping" is a positive value (could be greater than 1)
local d = 1 + player.damping*dt
player.xVel = player.xVel/d
player.yVel = player.yVec/d
end
Edit: Bug fix
Last edited by ivan on Fri Feb 06, 2015 6:31 am, edited 1 time in total.
I acully think this is a bug in löve with integrated graphics cards, I've sent my game to several computers and it choppy on everyone and then a look a bit closer and everyone of them had integrated graphics cards
Do you think this is the issue and if so do you think it will be possible for them to fix it if I send a bug report?
The same thing happens to me. It's our graphic cards. If you force your game into fullscreen it won't happen, though. I really hope this will be fixed in a future release.
It might be a bug for some specific integrated graphic card models then, I just tried your love file and it works smooth on my macbook air with an intel HD5000