General Category > General Game and Application Development

delta time alternative

(1/4) > >>

hosch:
Hello,

I recently published an App on Android made in AGK (which is not specifically causing the problem). I tied the movement of the enemies to delta time and it works consistent and like a charm across all devices. However I got some reports by players that the movement of the main player is perceived as jittery and unpleasant. This happened with the newest update where I tied the player movement to delta time as well (before that I just incremented it gradually, which was dumb  :))).

I looked into it a little further and even a short lasting difference of 2-3 FPS (which can easily happen on mobile while a particle effect plays etc.) indeed makes the player movement look completly off. I think it has something to do with the player going in circles around a planet and the slightest stutter is very noticable.

Anyways, this is the current code for the player movement

--- Code: ---function RotatePlayer()
local x as float
local y as float

local angle as float

angle = GetSpriteAngle(spr_player) + ((playerSpeed * playerDir) * deltaFrame)
SetSpriteAngle(spr_player,angle)

  x = resX / 2.0 + ((levelRadius) * cos(angle))
  y = resY / 2.0 + ((levelRadius) * sin(angle))
  SetSpritePositionByOffset(spr_player,x,y)
endfunction
--- End code ---

Nothing fancy here. If I ditch the deltaFrame portion, the movement looks silky smooth again. Are there other methods of controlling the movement more consistently and frame independent I didn't think of? Thanks so much for your input.

Steve Elliott:
You didn't supply the code for deltaFrame (which could be the problem).

hosch:
Of course, sorry. This gets called every frame


--- Code: ---function GetDeltaTime()
    thisFrame = Timer()
    deltaFrame = thisFrame - lastFrame
    lastFrame = thisFrame
endfunction
--- End code ---

And lastFrame is set on the game round start to the current milliseconds. I am fairly certain that it sticks out that much, because the slightest FPS difference breaks the illusion of a smooth circle. That's why it is not noticable at all for the enemies, they simply move towards the player in a straight line.

Steve Elliott:
You can't just use a timer and not dampen any spikes in frame time - that will be jerky.

This is my code for delta-time:


--- Code: ---// init delta time

Type Delta

prev_time   As Float
time_frame  As Float
update_time As Float
req_fps     As Float
dt          As Float
prev_dt     As Float
min         As Float
max         As Float

EndType

Global delta As Delta
ResetTimer()

delta.req_fps      = 60.0
delta.update_time  = 1000.0 / delta.req_fps
delta.min          = 0.005
delta.max          = 5.0
delta.prev_time    = Timer()
delta.time_frame   = 1.0 / delta.update_time
delta.dt           = 1.0 / delta.update_time
delta.prev_dt      = 1.0 / delta.update_time

// delta time function

Function get_delta()

delta.time_frame = ( Timer() - delta.prev_time ) * 1000.0
delta.prev_time  = Timer()

delta.prev_dt    = delta.dt
delta.dt         = delta.time_frame / delta.update_time

// limit increase from frame to frame

If( delta.dt > (delta.prev_dt * 2.0) )

delta.dt = delta.prev_dt * 2.0

ElseIf( delta.dt < (delta.prev_dt / 2.0) )

delta.dt = delta.prev_dt / 2.0

Endif

// keep frameskip sensible and avoid delta becoming too small

if( delta.dt > delta.max )

delta.dt = delta.max

Elseif( delta.dt < delta.min )

delta.dt = delta.min

Endif

EndFunction delta.dt



// update with delta eg)

Function update_enemy( dt As Float )

        enemy.speed = 2.0
enemy.x = enemy.x + ( enemy.speed * dt )

...

Endfunction


// main loop

dt As Float

Repeat
  dt = get_delta()

        update_enemy( dt )

Until game_mode = QUIT

--- End code ---

hosch:
Ooph, yeah I've implemented your code and it is very smooth again  :o Am I allowed to use this for my project?

May I ask what you are doing in the ResetTimer() function? How did you come up with the value of 2.0?

Navigation

[0] Message Index

[#] Next page

Go to full version