Platypus - losing ground, physics needs more time? (SOLVED)

Hi!

With Platypus I have issues, that I still can’t fix - when switching focus from the game windows/browser tab, when I get back the character is falling down through the collision shapes.

Sometimes it happens also in the beginning. But not always. I suspect that collisions are not ready yet and raycast is not working, but the update is still lowering the position of game object :confused:

It is not happening with Platypus example, and I guess my level is too big or post-process is too heavy. Though, I optimised it a lot, the game hangs for a while anyway when switching focus back and the issue with falling down happens.

Why do you think is happening?
What can I do to prevent it?

At start:
Honeycam%202021-03-04%2011-32-56

Switching windows (build from Editor, the same happens with Windows build and HTML5 build):
Honeycam%202021-03-04%2011-28-35

Focus was on another windows when started - switching back:
Honeycam%202021-03-04%2011-26-32

Poor guy, forever falling, still looking cool. :sunglasses:

What are your vsync and frame cap settings? It might be that the physics skip frames and that’s why you’re seeing this behaviour. I’ve noticed this happening when vsync is disabled.

2 Likes

Wow! It really works! I have indeed disabled Vsync and just enabled it and was trying for a while to reproduce it, but it never happened! In Platypus example Vsync is also enabled (like in default game.project)

Thank you so much @totebo! :wink:

2 Likes

Yay! Happy to have helped Mr Cool from his eternal downfall.

2 Likes

The problem is probably that you are getting a really large dt when the game has been in the background for a while.

I believe this will be solved in the next release, right @JCash?

2 Likes

Well, the fix was still on the Switch branch, but you got a PR here, and we can add it to the beta.

5 Likes

Wow, this is a bug that I solved just yesterday.

My workaround is to disable dynamic collision objects in the scene’s init method, and then turn them on after a while.

To solve bugs with physics after window switching I pause the physics world by setting the timestep to zero on the focus lost event and set it back to 1 on the focus gained event.

function on_window_event(self, event, data)
  if event = window.WINDOW_EVENT_FOCUS_LOST then 
    msg.post("#gameproxy", "set_time_step", { factor = 0, mode = 1 })
  elseif event == window.WINDOW_EVENT_FOCUS_GAINED then
    msg.post("#gameproxy", "set_time_step", { factor = 1, mode = 1 })
  end
end

function init(self)
  window.set_listener(on_window_event)
end

Of course, if VSYNC works, that’s cool! :sunglasses:

5 Likes