Page 1 of 1

Ship jitters

Posted: Sat Apr 23, 2011 11:10 pm
by chris-kun
current code here.

So basically the ship is just a bit jittery and I was wondering if there was a smoother way to do it. The only lines that matter are the three starting from 64. I was trying to compare the current point of the ship with where is was in the previous frame and move the camera according (I'm using the hump library.) Any ideas?

Re: Ship jitters

Posted: Sun Apr 24, 2011 2:53 am
by BlackBulletIV
I don't have the time to track down the jitters; although I have suspicion that it could be caused by your camera. I had trouble with the player jerking (or jittering, whatever) when using physics and a smooth moving camera. Taehl and bartbes helped me out there, so here's the code I came up with for my camera, it might help.

Code: Select all

camera.x = camera.x - (camera.x - (self.player.x - love.graphics.getWidth() / 2)) * dt * camera.speed
camera.y = camera.y - (camera.y - (self.player.y - love.graphics.getHeight() / 2)) * dt * camera.speed
Also, I noticed a block of code which most certainly doesn't need to be as big as it is. This is what I'm referring to:

Code: Select all

  do
    local px, py = playerBody:getWorldCenter()
    local pvec = vector.new(200, 200)
    local x, y = playerBody:getWorldCenter()
    local bvec = vector.new(x,y)
    local distance = pvec - bvec
    local f = 500000 / distance:len2()
    distance:normalize_inplace()
    local force = f * distance
    local fx = force.x
    local fy = force.y
    playerBody:applyForce(fx,fy)
  end
It could be shortened to this:

Code: Select all

do
  local distance = vector.new(200, 200) - vector.new(playerBody:getWorldCenter())
  local f = 500000 / distance:len2()
  distance:normalize_inplace()
  local force = f * distance
  playerBody:applyForce(force.x, force.y)
end
And you might not even need the do/end bit either. Anyway, hope some of that helps.

Re: Ship jitters

Posted: Sun Apr 24, 2011 7:56 am
by vrld
As BlackBulletIV wrote, you have to make the camera movement dependent on the time to get a smooth movement. So instead of

Code: Select all

view:translate(vector((nx-lx)*2,(ny-ly)*2))
use this:

Code: Select all

view:translate( vector(nx-lx, ny-ly) * 0.5 * dt )
What that does is move the camera depending with a speed depending how far away it is from the player body, so it will smoothly follow the player. The 0.5 can be changed to vary the speed of the camera movement (camera.speed in the other example).