## Choppy player movement

General discussion about LÖVE, Lua, game development, puns, and unicorns.
VinnieH01
Prole
Posts: 5
Joined: Tue Feb 03, 2015 3:46 pm
Location: Sweden

### Choppy player movement

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

Code: Select all

* dt
the problem docent occur.

This is my code..

Code: Select all

local x = 10
local y = 10
local s = 100

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
Attachments
Game.love
This is the love file

Duster
Prole
Posts: 32
Joined: Fri Dec 19, 2014 8:34 pm

### Re: Choppy player movement

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.

kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Contact:

### Re: Choppy player movement

Try updating your graphic card drivers
When I write def I mean function.

Tjakka5
Party member
Posts: 240
Joined: Thu Dec 26, 2013 12:17 pm

### Re: Choppy player movement

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

Check out my portfolio: http://tjakka5.sorunome.de/

ivan
Party member
Posts: 1524
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: Choppy player movement

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
Last edited by ivan on Fri Feb 06, 2015 6:31 am, edited 1 time in total.

VinnieH01
Prole
Posts: 5
Joined: Tue Feb 03, 2015 3:46 pm
Location: Sweden

### Re: Choppy player movement

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?

megalukes
Citizen
Posts: 91
Joined: Fri Jun 27, 2014 11:29 pm
Location: Brazil

### Re: Choppy player movement

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.

CandyFace
Prole
Posts: 4
Joined: Tue Jan 20, 2015 5:04 pm

### Re: Choppy player movement

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

### Who is online

Users browsing this forum: No registered users and 4 guests