godot: Physics interpolation is broken on XR nodes and XRServer
Tested versions
- Reproducible in v4.4 (0e3dbba6a)
System information
Godot v4.4.rc.mono (0e3dbba6a) - Windows 11 (build 26100) - Multi-window, 1 monitor - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 4090 (NVIDIA; 32.0.15.7247) - AMD Ryzen 9 7950X3D 16-Core Processor (32 threads)
Issue description
Physics interpolation is broken in XR in general, causing the following issues:
XROrigin3DupdatesXRServer::world_originonly when the transform changes, which meansXRServer::world_originnever has the interpolated value for the transform on each render frame.XRServer::world_originis used to know where the camera is in the world and to render properly, if it isn’t also interpolated then there’s is a mismatch between the activeXROrigin3Dtransform and whatXRServerhas saved. This results in the following behavior:
https://github.com/user-attachments/assets/5e9d7764-7c03-4a29-bc98-de86d0451f9f
- All XR nodes that have a
XRPositionalTrackerwill update their position every time the"pose_changed"signal is triggered. This happens every timeXRServer::_processgets called because it asks the active XR interface to poll events and it happens every render frame. Tracked nodes being updated every frame make sense because if we instead poll events on each physics frame and interpolate them the motion sickness on something like the HMD would be unreal, and you don’t want your hand trackers to lag behind your real hands. However, when interpolation is enabled the engine really doesn’t want you to be updating your transforms on render frame and this causes the childs of those tracked nodes to jitter a bit.
Here is an example in the attached MRP, when the “hand” is interpolated, it follows the parent just fine but the childs aren’t smooth at all (also spams in the console the warning that an interpolated transform was set outside physics process)
https://github.com/user-attachments/assets/4f32e267-3b35-41c2-8659-c5fbe2b96330
Disabling interpolation in the hand results in really smooth child movement but then when it comes to following the parent everything falls apart:
https://github.com/user-attachments/assets/51271cb6-32ee-4773-b4d9-4141c55fd1a9
I created a WIP pull request (https://github.com/godotengine/godot/pull/103233) with a fix for the XRServer::world_origin interpolation where it works as expected:
https://github.com/user-attachments/assets/107ca829-afc1-4875-827d-a34c7602ee83
However for the second issue I’m out of ideas about how to proceed. Disabling interpolation on the tracked node makes it jitter because it will now follow the non interpolated transform of the parent even if the parent is set as interpolated.
Steps to reproduce
Enable interpolation in a project and make XROrigin3D a child of any physics object
Minimal reproduction project (MRP)
It can be reproduced on the official openxr_character_centric_movement demo, modifying start_vr.gd to remove the line Engine.physics_ticks_per_second = current_refresh_rate so the physics FPS can be different than the target framerate
https://github.com/godotengine/godot-demo-projects/tree/master/xr/openxr_character_centric_movement
However here I include a modified version that just adds a moving platform where you can easily see the problem
Here is an MRP to test the tracker issue with a fake hand for anyone who wants to test without XR hardware:
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 24 (24 by maintainers)
It works, thank you! I was currently testing another approach for the same concept but looks like mine didn’t really work, I will try to adapt this to work internally and report the result here
EDIT: The approach I was trying didn’t quite work when moving the parent, I was setting the global transform directly in
XRNode3D::_pose_changed. If instead the we save the pose transform and apply it as an offset to the parent interpolated transform on every internal process it works fine:Left is with the fix applied, right is how it currently works.
https://github.com/user-attachments/assets/7a8f530a-b50e-44ce-b8bf-b5608645c2f8
Sadly it’s extremely hard to notice on video, it’s something that is only apparent when you’re using the VR headset but the left cube moves as smooth as you would expect while the right cube jitters quite a bit. Tomorrow I will update the PR with this changes and I guess it will be ready for review.
@Asaduji see #103331 .
I’m not absolutely sure yet whether it will cover all cases yet but it is pretty simple and does what should be required to remove the extra notifications.
It seems fine for the initial placement problem you were having above, but needs some thorough testing.
Hopefully that (or similar) will work in conjunction with your PR.
I’m not sure if I understand what you mean by extra transforms 😅
Try this for
fake_hand.gd:I’ll try and write more explanation tomorrow but should be enough to get started.
I updated the issue with a visual explanation and also attached an MRP for anyone who doesn’t have XR hardware. I’m aware that the behavior is actually the intended behavior for any normal
Node3Dbut XR tracked nodes must update their transform on every render frame otherwise it would be unusable. In the attached MRP I also added a node with the script of the comment you mentioned but doesn’t seem to have any effect, maybe I did it wrong.EDIT: If you rearrange the scene tree to look like this:
https://github.com/user-attachments/assets/441ad413-9bec-4484-84ed-62d58609efda
However with this approach all childs of
FollowHandmust have interpolation disabled which it’s fine but it won’t work for XR because those nodes must be childs of aXROrigin3D, if we makeXROrigin3Dbe the one following the interpolated transform of its parent instead it will work, but will defeat the whole point of the first fix. Needing to add a helper node in between theCharacterController3Dor whatever the parent is andXROrigin3Dfeels like a hacky workaround.Thank you for your feedback! My initial approach was actually calling
set_process_internaland updating the transform of theXRServeron each render frame with the interpolated value, basically what you just said of doing it in the client side. However, after taking a look at other implementations I thought It would be more appreciated to keep it similar to those as they seemed more performant than forcing an update every frame. I completely missedCamera3D::_update_camera😅 I agree that doing it similar to what the camera does is better here as it will be way easier to maintain and should work perfectly fine. We can only have 1XROrigin3Dnode active at a time so only 1 would be processing internally, meaning the performance gain of doing it on the server side would be unnoticeable.EDIT: I modified the PR to use this approach instead. The second issue still persists and maybe someone with physics experience can give us some advice.
I don’t have any XR devices so can’t seem to run the MRP, but a function that can probably help you is: https://docs.godotengine.org/en/3.6/tutorials/physics/interpolation/advanced_physics_interpolation.html#get-global-transform-interpolated
Regarding the PR, you can do physics interpolation either client (scene) side or on the server. Sometimes we do it on the client side and sometimes server side.
Client side is often easier to implement (and indeed the mechanism is already built in to help do this via
get_global_transform_interpolated(), although care has to be taken over priming). This function is primarily for users, but can also be used by core. Although having said that, inCameraswe do this manually as it gets around priming issues - it depends whether the node will always be active or will be switched on and off.Camerasis a good example to see how they do it if you want to try client side, probably the 3D camera as it is slightly simpler. You may want to useNOTIFICATION_INTERNAL_PROCESSandNOTIFICATION_INTERNAL_PHYSICS_PROCESS.The reason for doing it server side is, in most cases, for efficiency. So for e.g. large number of canvas items, or 3d nodes, we do the interpolation on the server, because it potentially is more efficient by e.g. avoiding lots of calls to
set_transform()across the command queue on every frame, and is especially more efficient when the frame rate is higher than the tick rate.On the other hand for single special items that there are only one or two of in a scene at once (e.g.
Cameras) we do sometimes do the interpolation client side, as there is no significant loss to efficiency, and it keeps the server side code as simple as possible (indeed the overall code complexity is less).I suspect for your XR origin it may be less complex to do the interpolation client side, and keep the server updated each frame. But doing it server side as in the PR isn’t necessarily “wrong”, it’s just worth considering that you can do it the other way around. Ultimately it’s up to the XR maintainers which way they prefer (and in some cases it only works sensibly one way around).
For targets on the client side you will also often want to use
get_global_transform_interpolated()so you get a predicted interpolated position, instead of the physics tick position. This is the technique used for IK, where IK targets are evaluated each frame.