The Godot Community Forums are back up and running! Please read the the announcement post for important information on what happened. Thanks!

Reconstructing past physics states

AzedaxenAzedaxen Posts: 50Member

I'm trying to make lag compensation system for my authoritative server project. Every object that moves over the network keeps a log of where it previously was in past game logic updates, up to the past second. Additionally, I made a function that can interpolate between game logic updates (since clients will see the objects interpolated between the otherwise slow game logic updates)

I thought I could write a "rewind" function that just tells all the networked entities to go back to where they were at a certain timestamp, then perform the necessary physics calculation, update the game accordingly, then return the objects to where they're supposed to be. However, because of how physics works, it seems like it's not very straightforward to just write a single function that does this. I tried something like:

func past_check():
  var box_orig_pos = box.translation
  box.translation.x = 0
  box.translation = box_orig_pos 

This was just meant as a simple test to see if it was possible to do what I'm describing in a single frame, but the print statement always prints false, so it doesn't seem quite so simple.

Any ideas for how to do this? Am I going to have to find a way to stagger the function over more than one update so that the collision checks will work properly? I'd also prefer to avoid having to alter the position of the body directly, since it's likely that many rewind checks will need to happen frequently. Can I use a world or something like that?


  • TwistedTwiglegTwistedTwigleg Posts: 3,129Admin

    You'll need to run this code in _physics_process, as otherwise the physics engine may or may not take the changes into account. I'd recommend having the function cache the data and mark a boolean as dirty, and then in _physics_process check if the boolean is dirty and perform the necessary rewind functionality using the cached data.

    I have no idea how helpful it will be or not, but this post on comes up when Googling "Godot network rewind", so maybe it will help?

    (My network programming skill is very limited though, so please take this with a grain of salt!)

  • AzedaxenAzedaxen Posts: 50Member

    Thanks for the suggestion, but I'm having a hard time getting this to work even in physics process. When is physics process called in relation to process?

    And yes, that thread helps quite a bit, I am taking the timestamps approach. I am also using the Gabriel Gambetta article, it's great because it's engine-agnostic.

  • klaasklaas Posts: 13Member

    _physics_process is called on a fixed fps. _process is called whenever a frame can be rendered. So they both are totaly unrelated in there order and frequency.
    When fiddeling with physics, have a look at the _integrate_forces method there you can overwrite the physics calculation results

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file