Translating input velocity (pixels / second) into world units

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TranquilMarmot

I’m getting mouse dragging velocity like so:

var mouse_dragging = false


func _input(event):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		mouse_dragging = event.pressed

	if event is InputEventMouseMotion and mouse_dragging:
		_pan(-event.velocity.x / 5000, -event.velocity.y / 5000)

My _pan function moves the camera along the X/Z plane in world units.

Is there a way to convert event.velocity (which is in pixels per second) into world units?

Maybe I’m better off doing a ray-cast of event.position to get world coordinates somehow?

World units depends on which depth you expect the pan to be applied. It’s like you are dragging a 3D plane facing the camera, but that plane could be at different distance from the camera, which means different speeds would be needed to match the speed of the mouse.

If you want panning speed to depend on the surface that the mouse cursor would hit in 3D, then you have to do a raycast to determine how far it is from the camera.
Then you would calculate a factor from pixels/s into m/s.

Instinctively I would project the previous and current mouse’s coordinates at that distance, then calculate the vector between the two and pan by that amount. But maybe there is a smarter way to do this? I bet it just depends on fov and distance somewhat, maybe also near

Zylann | 2023-07-06 16:28