Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor player height adjustment #475

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions addons/godot-xr-tools/player/player_body.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const NEAR_GROUND_DISTANCE := 1.0
@export var enabled : bool = true: set = set_enabled

@export_group("Player setup")

## Automatically calibrate player body on next frame
@export var player_calibrate_height : bool = true

## Radius of the player body collider
@export var player_radius : float = 0.2: set = set_player_radius

Expand All @@ -56,7 +60,7 @@ const NEAR_GROUND_DISTANCE := 1.0
@export var player_height_min : float = 0.6

## Maximum player height
@export var player_height_max : float = 2.2
@export var player_height_max : float = 2.5

## Eyes forward offset from center of body in player_radius units
@export_range(0.0, 1.0) var eye_forward_offset : float = 0.5
Expand Down Expand Up @@ -200,6 +204,7 @@ func _ready():
_update_enabled()
_update_player_radius()


func set_enabled(new_value) -> void:
enabled = new_value
if is_inside_tree():
Expand Down Expand Up @@ -414,6 +419,13 @@ func slew_up(up: Vector3, slew: float) -> void:
# Update the origin
origin_node.global_transform = new_origin

## This method calibrates the players height on the assumption
## the player is in rest position
func calibrate_player_height():
var base_height = camera_node.transform.origin.y + (player_head_height * XRServer.world_scale)
var player_height = XRToolsUserSettings.player_height * XRServer.world_scale
player_height_offset = (player_height - base_height) / XRServer.world_scale

## This method sets or clears a named height override
func override_player_height(key, value: float = -1.0):
# Clear or set the override
Expand Down Expand Up @@ -462,10 +474,16 @@ func _estimate_body_forward_dir() -> Vector3:

# This method updates the player body to match the player position
func _update_body_under_camera():
# Initially calibration of player height
if player_calibrate_height:
calibrate_player_height()
player_calibrate_height = false

# Calculate the player height based on the camera position in the origin and the calibration
var player_height: float = clamp(
camera_node.transform.origin.y + player_head_height +
player_height_offset + XRToolsUserSettings.player_height_adjust,
camera_node.transform.origin.y
+ (player_head_height * XRServer.world_scale)
+ (player_height_offset * XRServer.world_scale),
player_height_min * XRServer.world_scale,
player_height_max * XRServer.world_scale)

Expand Down
27 changes: 17 additions & 10 deletions addons/godot-xr-tools/user_settings/user_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum WebXRPrimary {
}


@export_group("Input")

## User setting for snap-turn
@export var snap_turning : bool = true

Expand All @@ -21,8 +23,12 @@ enum WebXRPrimary {
## User setting for y axis dead zone
@export var x_axis_dead_zone : float = 0.2

## User setting for player height adjust
@export var player_height_adjust : float = 0.0: set = set_player_height_adjust
@export_group("Player")

## User setting for player height
@export var player_height : float = 1.85: set = set_player_height

@export_group("WebXR")

## User setting for WebXR primary
@export var webxr_primary : WebXRPrimary = WebXRPrimary.AUTO: set = set_webxr_primary
Expand All @@ -46,17 +52,18 @@ func _ready():

## Reset to default values
func reset_to_defaults() -> void:
# Reset to defaults
# Reset to defaults.
# Where applicable we obtain our project settings
snap_turning = XRTools.get_default_snap_turning()
y_axis_dead_zone = XRTools.get_y_axis_dead_zone()
x_axis_dead_zone = XRTools.get_x_axis_dead_zone()
player_height_adjust = 0.0
player_height = XRTools.get_player_standard_height()
webxr_primary = WebXRPrimary.AUTO
webxr_auto_primary = 0

## Set the player height adjust property
func set_player_height_adjust(new_value : float) -> void:
player_height_adjust = clamp(new_value, -1.0, 1.0)
## Set the player height property
func set_player_height(new_value : float) -> void:
player_height = clamp(new_value, 1.0, 2.5)

## Set the WebXR primary
func set_webxr_primary(new_value : WebXRPrimary) -> void:
Expand Down Expand Up @@ -88,7 +95,7 @@ func save() -> void:
"x_axis_dead_zone" : x_axis_dead_zone
},
"player" : {
"height_adjust" : player_height_adjust
"height" : player_height
},
"webxr" : {
"webxr_primary" : webxr_primary,
Expand Down Expand Up @@ -161,8 +168,8 @@ func _load() -> void:
# Parse our player settings
if settings.has("player"):
var player : Dictionary = settings["player"]
if player.has("height_adjust"):
player_height_adjust = player["height_adjust"]
if player.has("height"):
player_height = player["height"]

# Parse our WebXR settings
if settings.has("webxr"):
Expand Down
45 changes: 19 additions & 26 deletions addons/godot-xr-tools/user_settings/user_settings_ui.gd
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
extends TabContainer

@export_node_path("XRCamera3D") var camera
signal player_height_changed(new_height)

@export var player_head_height : float = 0.1
@onready var snap_turning_button = $Input/InputVBox/SnapTurning/SnapTurningCB
@onready var y_deadzone_slider = $Input/InputVBox/yAxisDeadZone/yAxisDeadZoneSlider
@onready var x_deadzone_slider = $Input/InputVBox/xAxisDeadZone/xAxisDeadZoneSlider
@onready var player_height_slider = $Player/PlayerVBox/PlayerHeight/PlayerHeightSlider
@onready var webxr_primary_button = $WebXR/WebXRVBox/WebXR/WebXRPrimary

func _update():
# Input
$Input/SnapTurning/SnapTurningCB.button_pressed = XRToolsUserSettings.snap_turning
$Input/yAxisDeadZone/yAxisDeadZoneSlider.value = XRToolsUserSettings.y_axis_dead_zone
$Input/xAxisDeadZone/xAxisDeadZoneSlider.value = XRToolsUserSettings.x_axis_dead_zone
snap_turning_button.button_pressed = XRToolsUserSettings.snap_turning
y_deadzone_slider.value = XRToolsUserSettings.y_axis_dead_zone
x_deadzone_slider.value = XRToolsUserSettings.x_axis_dead_zone

# Player
$Player/PlayerHeight/PlayerHeightSlider.value = XRToolsUserSettings.player_height_adjust
player_height_slider.value = XRToolsUserSettings.player_height

# WebXR
$WebXR/WebXR/WebXRPrimary.selected = XRToolsUserSettings.webxr_primary
webxr_primary_button.selected = XRToolsUserSettings.webxr_primary


# Called when the node enters the scene tree for the first time.
Expand All @@ -38,38 +42,27 @@ func _on_Reset_pressed():
if XRToolsUserSettings:
XRToolsUserSettings.reset_to_defaults()
_update()
emit_signal("player_height_changed", XRToolsUserSettings.player_height)


# Input settings changed
func _on_SnapTurningCB_pressed():
XRToolsUserSettings.snap_turning = $Input/SnapTurning/SnapTurningCB.button_pressed
XRToolsUserSettings.snap_turning = snap_turning_button.button_pressed


# Player settings changed
func _on_PlayerHeightSlider_drag_ended(_value_changed):
XRToolsUserSettings.player_height_adjust = $Player/PlayerHeight/PlayerHeightSlider.value


func _on_PlayerHeightStandard_pressed():
if camera.is_empty():
return

var camera_node = get_node_or_null(camera)
if !camera_node:
return

var base_height = camera_node.transform.origin.y + player_head_height
var height_adjust = XRTools.get_player_standard_height() - base_height
XRToolsUserSettings.player_height_adjust = height_adjust
$Player/PlayerHeight/PlayerHeightSlider.value = XRToolsUserSettings.player_height_adjust
XRToolsUserSettings.player_height = player_height_slider.value
emit_signal("player_height_changed", XRToolsUserSettings.player_height)


func _on_web_xr_primary_item_selected(index: int) -> void:
XRToolsUserSettings.webxr_primary = index


func _on_y_axis_dead_zone_slider_value_changed(value):
XRToolsUserSettings.y_axis_dead_zone = $Input/yAxisDeadZone/yAxisDeadZoneSlider.value
XRToolsUserSettings.y_axis_dead_zone = y_deadzone_slider.value

func _on_x_axis_dead_zone_slider_value_changed(value):
XRToolsUserSettings.x_axis_dead_zone = $Input/xAxisDeadZone/xAxisDeadZoneSlider.value

XRToolsUserSettings.x_axis_dead_zone = x_deadzone_slider.value

Loading