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:

  • XROrigin3D updates XRServer::world_origin only when the transform changes, which means XRServer::world_origin never has the interpolated value for the transform on each render frame. XRServer::world_origin is 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 active XROrigin3D transform and what XRServer has saved. This results in the following behavior:

https://github.com/user-attachments/assets/5e9d7764-7c03-4a29-bc98-de86d0451f9f

  • All XR nodes that have a XRPositionalTracker will update their position every time the "pose_changed" signal is triggered. This happens every time XRServer::_process gets 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

MRP.zip

Here is an MRP to test the tracker issue with a fake hand for anyone who wants to test without XR hardware:

tracking_mrp.zip

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 24 (24 by maintainers)

Most upvoted comments

Try this for fake_hand.gd:

extends Node3D

var offset = Vector3()

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED)
	physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	var tr = get_parent().get_global_transform_interpolated()
	tr.origin += offset
	global_transform = tr

var mouse_sensitivity = 0.002
func _input(event):
	if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		offset.x += event.relative.x * mouse_sensitivity
		offset.y += -event.relative.y * mouse_sensitivity

I’ll try and write more explanation tomorrow but should be enough to get started.

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:

Node3D *parent = Object::cast_to<Node3D>(get_parent());

if (parent && parent->is_physics_interpolated_and_enabled() && !is_set_as_top_level()) {
	set_global_transform(parent->get_global_transform_interpolated() * pose_offset);
}

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 just looking at it now, it’s likely I can sort this as a separate PR (i.e. the warnings and extra transforms).

I’m not sure if I understand what you mean by extra transforms 😅

Try this for fake_hand.gd:

extends Node3D

var offset = Vector3()

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_CAPTURED)
	physics_interpolation_mode = Node.PHYSICS_INTERPOLATION_MODE_OFF
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta: float) -> void:
	var tr = get_parent().get_global_transform_interpolated()
	tr.origin += offset
	global_transform = tr

var mouse_sensitivity = 0.002
func _input(event):
	if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
		offset.x += event.relative.x * mouse_sensitivity
		offset.y += -event.relative.y * mouse_sensitivity

I’ll try and write more explanation tomorrow but should be enough to get started.

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.

I’m not sure I exactly understand the problem, but it could be it’s the same as #103025 (comment) and the same solution (or kind of solution) may apply.

Generally if you are setting the world position of a node each frame based on the hand controllers, then you should be ignoring the parent transform, maybe by setting as top level. But maybe the solution above is what you need here if you simply want fast updates relative to the interpolated parent xform. It’s difficult to say without seeing an example of this working.

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 Node3D but 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:

Image It actually works:

https://github.com/user-attachments/assets/441ad413-9bec-4484-84ed-62d58609efda

However with this approach all childs of FollowHand must have interpolation disabled which it’s fine but it won’t work for XR because those nodes must be childs of a XROrigin3D, if we make XROrigin3D be 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 the CharacterController3D or whatever the parent is and XROrigin3D feels like a hacky workaround.

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, in Cameras we 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.

Cameras is 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 use NOTIFICATION_INTERNAL_PROCESS and NOTIFICATION_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.

Thank you for your feedback! My initial approach was actually calling set_process_internal and updating the transform of the XRServer on 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 missed Camera3D::_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 1 XROrigin3D node 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, in Cameras we 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.

Cameras is 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 use NOTIFICATION_INTERNAL_PROCESS and NOTIFICATION_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.