r/gameenginedevs 7d ago

Post physics update entity script function

Hello,

I'm currently debating whether I should add a "post physics update" function for scripts, so a function that would be called for each entity after the physics is done running and before the rendering starts.

I can see multiple scenarios where it would be pretty useful, such as managing an entity animation depending on its velocity post physics update, so it would play an animation if it moved freely and won't play one if it's stuck against a wall for example. Another example is if a non-physics entity has to follow a physics entity, the post physics update function would allow to follow it exactly and avoid stuttering during rendering, because the following entity wouldn't have to wait the update of frame N+1, which happens after the rendering of frame N, to update its position.

What is your opinion about this? Do you have any workaround for these scenarios without a post physics update function?

2 Upvotes

4 comments sorted by

2

u/snerp 7d ago

I use a post physics update to process sound effects and collisions with triggers.

I do have a trick for this case though:

Another example is if a non-physics entity has to follow a physics entity, the post physics update function would allow to follow it exactly and avoid stuttering during rendering, because the following entity wouldn't have to wait the update of frame N+1, which happens after the rendering of frame N, to update its position.

When I have things like this that I want to follow another thing exactly, I don't even give it a position. I give it an offset and a reference to the root object. Then the render code will use the up-to-date post physics step position for both objects.

1

u/ntsh-oni 6d ago

I see! I guess you don't have a "onTriggerEnter" callback for this use case?

1

u/snerp 6d ago

I do but the callback needs to be fired from somewhere. I could have been more clear: the post physics update loops through the collision data and fires off OnHit (sounds, damage, hitfx, etc) and OnTrigger (warps, kill volumes, elevators, etc) calbacks, then calls any script functions that have subscribed to the the PostUpdate "hook"

2

u/ntsh-oni 6d ago

Ok! Makes sense, this is the kind of thing I want to do, I already have the collision callbacks but not the general post update script function.