# Animate with Vsync

**URL:** <https://forum.defold.com/t/animate-with-vsync/42002>\
**Category:** Questions\
**Created:** [January 30, 2019, 12:07pm UTC](https://forum.defold.com/t/animate-with-vsync/42002 "2019-01-30T12:07:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [January 30, 2019, 12:07pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/1 "2019-01-30T12:07:55Z")

</div>

I’m keen to use Vsync because without it movement can be a bit stuttery. What’s the best approach to make `go.animate` play back at the same duration on various devices (iPhone on low power mode, external monitors)? Is using delta time the only option?

ps. This is slightly related to [this post](https://forum.defold.com/t/can-a-html5-physics-game-run-smoothly-at-60fps-on-an-external-monitor/41515), but this is a more general question about animations not taking physics into account.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [January 30, 2019, 12:12pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/2 "2019-01-30T12:12:49Z")

</div>

> [@Can a HTML5 physics game run smoothly at 60fps on an external monitor?](https://forum.defold.com/t/can-a-html5-physics-game-run-smoothly-at-60fps-on-an-external-monitor/41515/2):
>
> Your best bet for consistent behaviour across those setups (HTML5, iPhone in power-save mode etc.) is to uncheck vsync and leave frame cap at 0.

This kind of works, but is a bit choppy on desktop only (for some reason).

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [January 30, 2019, 3:25pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/3 "2019-01-30T15:25:01Z")

</div>

Maybe one option would be to control all important animations completely with code and multiply by delta time?

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 30, 2019, 9:11pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/4 "2019-01-30T21:11:34Z")

</div>

> [@totebo](#):
>
> What’s the best approach to make `go.animate` play back at the same duration on various devices (iPhone on low power mode, external monitors)? Is using delta time the only option?

If go.animate() doesn’t respect the duration and delay given when the function is called it should be considered a bug that we need to fix. I’m not really sure of what problems we might have, if any. @Johan_Beck-Noren knows.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [January 31, 2019, 9:37am UTC](https://forum.defold.com/t/animate-with-vsync/42002/5 "2019-01-31T09:37:50Z")

</div>

It seems to me that when using animate vs. moving the sprite “manually” each frame, the animate stutters a bit, as if it’s skipping frames. I’ll see if I can work up a minimal example to make sure the stutter isn’t IN MY MIND.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [January 31, 2019, 10:28am UTC](https://forum.defold.com/t/animate-with-vsync/42002/6 "2019-01-31T10:28:15Z")

</div>

Conclusion: I can’t see any difference between animate and frame by frame. The stuttery issue happens when run on an external monitor, regardless of Vsync.

Vsync unticked and Frame cap 0 seems to be smooth on mobile and the laptop screen.

The project if anyone wants to tinker: [AnimateVsFramebyframe.zip](https://defold-forum-bucket.nbg1.your-objectstorage.com/original/2X/3/35226d2e13822be6a7be808421a294e3fdf01784.zip) (66.2 KB)

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 7, 2019, 6:18pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/7 "2019-02-07T18:18:13Z")

</div>

Would it be possible to control Vsync and frame cap at runtime? Then I’d be able to cater for devices manually, so all players don’t get the “lowest denominator”.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 15, 2019, 10:27am UTC](https://forum.defold.com/t/animate-with-vsync/42002/8 "2019-02-15T10:27:25Z")

</div>

Bump?

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 15, 2019, 11:03am UTC](https://forum.defold.com/t/animate-with-vsync/42002/9 "2019-02-15T11:03:32Z")

</div>

Vsync is controllable from runtime via message `set_vsync`; [API reference (System)](https://www.defold.com/ref/sys/#set_vsync:swap_interval).

Frame cap maps to the (old) name for it, `update_frequency` and can be set in runtime; [API reference (System)](https://www.defold.com/ref/sys/#set_update_frequency:frequency)

If you haven’t seen it already, you can find out more here; [Defold project settings](https://www.defold.com/manuals/project-settings/#_vsync_frame_cap_and_swap_interval)

I think for us to cover all these corner cases we need to measure actual time in the engine. For this to be viable we need to be able to keep physics running with a fixed timestep regardless of the engine time-step (DEF-3796). This work is ongoing but it’s a big task that we want to get right, and it’s not planned to be released in the next few releases.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 15, 2019, 11:04am UTC](https://forum.defold.com/t/animate-with-vsync/42002/10 "2019-02-15T11:04:13Z")

</div>

Amazing answer. Thanks!

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 16, 2019, 1:36pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/11 "2019-02-16T13:36:34Z")

</div>

Setting Vsync on and frame cap to 60:  
`msg.post("@system:", "set_vsync", { swap_interval = 1 } )`  
`msg.post("@system:", "set_update_frequency", { frequency = 60 } )`

Setting Vsync off and frame cap to 0:  
`msg.post("@system:", "set_vsync", { swap_interval = 0 } )`  
`msg.post("@system:", "set_update_frequency", { frequency = 0 } )`

Is this right? When turning off Vsync with the code above, I get very stuttery movement.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 18, 2019, 12:30pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/12 "2019-02-18T12:30:08Z")

</div>

Bump

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 18, 2019, 12:53pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/13 "2019-02-18T12:53:52Z")

</div>

> [@totebo](#):
>
> I get very stuttery movement

Can you expand on this? Is the timing off or are you just seeing tearing? Tearing would be an expected result when turning vsync off since drawing to the back buffer is no longer in sync with the monitor’s vblank.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 18, 2019, 1:06pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/14 "2019-02-18T13:06:53Z")

</div>

So when I say stuttery I actually mean frame rate drops to 5fps or so; things still work but update very slowly. I’m assuming the syntax I’m using is wrong, so would like to know how to set Vsync off and frame cap to 0 programmatically?

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 18, 2019, 1:17pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/15 "2019-02-18T13:17:53Z")

</div>

If you disable vsync in game.project instead, is the behaviour the same?

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 18, 2019, 1:39pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/16 "2019-02-18T13:39:14Z")

</div>

When I set it in game.project (and not in code) it works as expected. I’d rather not do this, though, because on most devices Vsync at 60fps would work fine, so I’d prefer to detect the edge cases and set those to Vsync off and frame cap 0.

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 18, 2019, 1:45pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/17 "2019-02-18T13:45:05Z")

</div>

Sure, I was just trying to figure out where the problem is 🙂

It seems you found a case where we don’t use the correct state in the engine when vsync/framcap is set in runtime. Could you try swapping the order of the messages? E.g. set update frequency first and then swap interval.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 18, 2019, 1:48pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/18 "2019-02-18T13:48:02Z")

</div>

Cool, will try that tomorrow!

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [February 18, 2019, 8:57pm UTC](https://forum.defold.com/t/animate-with-vsync/42002/19 "2019-02-18T20:57:24Z")

</div>

Same result I’m afraid. I tested with @britzl’s metrics extension, and the framerate drops and keeps dropping to around 1fps with this code:

`msg.post("@system:", "set_update_frequency", { frequency = 0 } )`  
`msg.post("@system:", "set_vsync", { swap_interval = 0 } )`

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [February 19, 2019, 7:53am UTC](https://forum.defold.com/t/animate-with-vsync/42002/20 "2019-02-19T07:53:51Z")

</div>

Hmm that sounds really strange, the above messages shouldn’t affect performance in that way at all. Are you doing lots of heavy-lifting in any script `update`? If you send me the project I can have a look.

[Next page](https://forum.defold.com/t/animate-with-vsync/42002.md?page=2)
