Page 1 of 1
Choppy player movement
Posted: Tue Feb 03, 2015 4:04 pm
by VinnieH01
Hi,
Ive got a playerX, playerY, playerSpeed variable.
When I draw the player I put the playerX and playerY variable as the coordinates then I add speed to them in the update function like this
Code: Select all
playerX = playerX + playerSpeed * dt
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
the problem docent occur.
This is my code..
Code: Select all
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
Re: Choppy player movement
Posted: Tue Feb 03, 2015 5:40 pm
by Duster
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.
Re: Choppy player movement
Posted: Tue Feb 03, 2015 5:44 pm
by kikito
Try updating your graphic card drivers
Re: Choppy player movement
Posted: Wed Feb 04, 2015 8:47 am
by Tjakka5
Try to use velocity, similair to this code:
Code: Select all
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
Re: Choppy player movement
Posted: Wed Feb 04, 2015 10:45 am
by ivan
Hey Tjakaa5, you're on the right path there although I would like to point out that
there is an easier (and slightly more precise) way to clamp the maxSpeed:
Code: Select all
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":
Code: Select all
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
Re: Choppy player movement
Posted: Wed Feb 04, 2015 2:44 pm
by VinnieH01
Thanks for all the replies..
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?
Re: Choppy player movement
Posted: Wed Feb 04, 2015 3:37 pm
by megalukes
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.
Re: Choppy player movement
Posted: Fri Feb 06, 2015 9:29 pm
by CandyFace
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