[SOLVED] Characters moving super fast at *144fps*

Hi all

Today I tested my game on a friend’s computer, and it appears that the computer in question is far more powerful than mine :sweat_smile:

On my computer:
image

On his computer:
image

I don’t have any gameplay footage to show the visual result, but the characters were moving way faster (while the spine animation seemed to remain the same).

It’s been a while, but… it seems like I’m using something called “Metrics” (?) to display the FPS.
The code looks like this:

function init(self)
	self.instance = fps.create(self.samples, nil, go.get_world_position(), self.color)
end

function update(self, dt)
    self.instance.update()
    if self.show then
    	self.instance.draw()
    end

    fps.update()
    debug.fps = lume.round(fps.fps(), 1)	
end

And for the the characters movements:

function update(self, dt)
    local p, movement = go.get_world_position(), self.move_direction * self.move_speed_cur * move_speed_factor
    go.set_position(p + movement)
end


1/ Is it possible to prevent the game from going beyond 60fps?
2/ And most of all, how to make sure the character movements don’t depend on the fps? (so they don’t move slowly at 30fps and super fast at 120fps)

The “Metrics” mentioned above is the defold-metrics extension (remembered it a few seconds ago).

But since the purpose of this extension is just to display the fps, it probably has nothing to do with my issue or any solution.

Sounds like a vsync issue!

You can cap the framerate by using this field:

1 Like

Oh ok thanks! This answers my first question.

For the second one, can the issue be solved with “fixed_update”?

I just replaced update() with fixed_update() in my character.script, and the character seems to move at the same speed at both 30 and 60fps…

Is there a reason not to use fixed_update… everywhere? :thinking:

Your movement calculation should use the delta time which is passed into the update function. This allows you to account for how much time each frame is actually taking.

2 Likes

Just added *dt at the end (like in the movement tutorial) but the characters don’t move anymore (looks like it somehow “neutralizes” the movement vector).

function update(self, dt)
    local p, movement = go.get_world_position(), self.move_direction * self.move_speed_cur * move_speed_factor * dt
    go.set_position(p + movement)
end

Did I miss something?

If you didn’t change your speed value, then you have effectively reduced speed to 1/60th of what you are used to seeing. Think of the speed value as “pixels per second” (currently it is “pixels per frame”).

4 Likes

Multiplying your movement speed by dt changes the meaning from units/frame to units/second. So if your speed was 10 pixels per frame, it’s now 10 pixels per second which is much slower. You’ll need to increase your speed values accordingly (multiplying it by 60 should put it back to normal).

7 Likes

Your answers helped me fix my issue and I learned some useful stuff in the process :slight_smile:

Thank you all!

5 Likes