Page 1 of 1

Jerking in move

Posted: Fri Sep 27, 2019 12:42 pm
by cry_san
Hi!

I check the movement of the image by pressing the second button of the mouse.
I do not understand why the image moves with jerks.
How to make the movement more smooth?

aangle = math.atan2(self.destY - self.y, self.destX - self.x)
dist = math.sqrt((self.destX - self.x)^2 + (self.destY - self.y)^2)

self.x = self.x + math.cos(aangle)*self.speed*dt
self.y = self.y + math.sin(aangle)*self.speed*dt

Thanks for your help!

Re: Jerking in move

Posted: Fri Sep 27, 2019 5:51 pm
by MrFariator
A couple of things are at play here.

First, in your love.update you have the following:

Code: Select all

function love.update(dt)
  -- ...
  local dx,dy = Player.x - camera.x, Player.y - camera.y
  camera:move(dx/2, dy/2)
  Player:update(dt)
end
Here you are updating the camera before player moves. What this effectively does is that the camera is "lagging" behind one frame's worth of movement, which contributes to the jerky camera. Just flip the lines around, and this is fixed:

Code: Select all

function love.update(dt)
  -- ...
  Player:update(dt)
  local dx,dy = Player.x - camera.x, Player.y - camera.y
  camera:move(dx/2, dy/2)
end
Second thing is that you are moving the player about in fractional units, ie. non-integers. This affects rendering a little bit, since you're also moving the camera about in non-integer steps. There's different ways on how to deal with this, but for example you could only move the camera in integers, and draw the player graphic at integer positions. Effectively, round to nearest integer.

Code: Select all

-- Returns 'n' rounded to the nearest 'deci'th (defaulting whole numbers).
-- This function is taken from https://love2d.org/wiki/General_math, you could put it at the top of main.lua or someplace else
function math.round(n, deci) deci = 10^(deci or 0) return math.floor(n*deci+.5)/deci end

-- in main.lua
function love.update(dt)
  -- ...
  Player:update(dt)
  local dx,dy = Player.x - camera.x, Player.y - camera.y
  camera:move(math.round(dx), math.round(dy))
end
-- in player.lua
function Player:draw()
  local x,y = math.round(self.x), math.round(self.y)
  love.graphics.draw(self.img, x, y, round(self.angle,0), self.sx, self.sy, self.ox, self.oy)
end
These changes made the movement and camera smooth on my end.

Re: Jerking in move

Posted: Sat Sep 28, 2019 1:33 am
by cry_san
Thanks for the answer!
Big jerks disappeared. Indeed, your code is very helpful:

Code: Select all

Player:update(dt)
...
camera:move(math.round(dx), math.round(dy))
But here the movement of the image was not smooth.
Add more image to become visible little jerks.
How to achieve a smooth movement?
When rounding the coordinates of a small image, the motion becomes a little smoother, but it does not go in a straight line.
What other methods are there for this?

I put the new code space.love

Thanks for your help!