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

233 - add dead zone to user settings #468

Merged
merged 7 commits into from
Jul 22, 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
8 changes: 4 additions & 4 deletions addons/godot-xr-tools/functions/movement_direct.gd
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func physics_movement(_delta: float, player_body: XRToolsPlayerBody, _disabled:
if !_controller.get_is_active():
return

# Apply forwards/backwards ground control
player_body.ground_control_velocity.y += _controller.get_vector2(input_action).y * max_speed
## get input action with deadzone correction applied
var dz_input_action = XRToolsUserSettings.get_adjusted_vector2(_controller, input_action)

# Apply left/right ground control
player_body.ground_control_velocity.x += dz_input_action.x * max_speed
if strafe:
player_body.ground_control_velocity.x += _controller.get_vector2(input_action).x * max_speed
player_body.ground_control_velocity.y += dz_input_action.y * max_speed

# Clamp ground control
var length := player_body.ground_control_velocity.length()
Expand Down
16 changes: 16 additions & 0 deletions addons/godot-xr-tools/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ func _enter_tree():
"0.2,0.8,0.05",
0.7)

# Add input y_axis_dead_zone to the project settings
_define_project_setting(
"godot_xr_tools/input/y_axis_dead_zone",
TYPE_FLOAT,
PROPERTY_HINT_RANGE,
"0.0,0.5,0.01",
0.1)

# Add input x_axis_dead_zone to the project settings
_define_project_setting(
"godot_xr_tools/input/x_axis_dead_zone",
TYPE_FLOAT,
PROPERTY_HINT_RANGE,
"0.0,0.5,0.01",
0.2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it correct our default here is 0.2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when testing, I found useful adding a bit more deadzone in the x axis to avoid accidental rotation when moving forward...

the original issue suggested a value of 0.2. I'm not sure which value would be more convenient by default

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Owh, yeah that makes sense :)


# Add input snap turning dead-zone to the project settings
_define_project_setting(
"godot_xr_tools/input/snap_turning_deadzone",
Expand Down
36 changes: 33 additions & 3 deletions addons/godot-xr-tools/user_settings/user_settings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ enum WebXRPrimary {
## User setting for snap-turn
@export var snap_turning : bool = true

## User setting for y axis dead zone
@export var y_axis_dead_zone : float = 0.1

## 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

Expand Down Expand Up @@ -42,16 +48,16 @@ func _ready():
func reset_to_defaults() -> void:
# Reset to defaults
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
BastiaanOlij marked this conversation as resolved.
Show resolved Hide resolved
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 WebXR primary
func set_webxr_primary(new_value : WebXRPrimary) -> void:
webxr_primary = new_value
Expand All @@ -77,7 +83,9 @@ func save() -> void:
# Convert the settings to a dictionary
var settings := {
"input" : {
"default_snap_turning" : snap_turning
"default_snap_turning" : snap_turning,
"y_axis_dead_zone" : y_axis_dead_zone,
"x_axis_dead_zone" : x_axis_dead_zone
},
"player" : {
"height_adjust" : player_height_adjust
Expand Down Expand Up @@ -145,6 +153,10 @@ func _load() -> void:
var input : Dictionary = settings["input"]
if input.has("default_snap_turning"):
snap_turning = input["default_snap_turning"]
if input.has("y_axis_dead_zone"):
y_axis_dead_zone = input["y_axis_dead_zone"]
if input.has("x_axis_dead_zone"):
x_axis_dead_zone = input["x_axis_dead_zone"]

# Parse our player settings
if settings.has("player"):
Expand Down Expand Up @@ -177,3 +189,21 @@ func _on_webxr_vector2_changed(name: String, _vector: Vector2) -> void:
if webxr_auto_primary != 0:
# Let the developer know which one is chosen.
webxr_primary_changed.emit(webxr_auto_primary)

## Helper function to remap input vector with deadzone values
func get_adjusted_vector2(p_controller, p_input_action):
var vector = Vector2.ZERO
var original_vector = p_controller.get_vector2(p_input_action)

if abs(original_vector.y) > y_axis_dead_zone:
vector.y = remap(abs(original_vector.y), y_axis_dead_zone, 1, 0, 1)
if original_vector.y < 0:
vector.y *= -1

if abs(original_vector.x) > x_axis_dead_zone:
vector.x = remap(abs(original_vector.x), x_axis_dead_zone, 1, 0, 1)
if original_vector.x < 0:
vector.x *= -1

return vector

11 changes: 11 additions & 0 deletions addons/godot-xr-tools/user_settings/user_settings_ui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ extends TabContainer
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

# Player
$Player/PlayerHeight/PlayerHeightSlider.value = XRToolsUserSettings.player_height_adjust
Expand Down Expand Up @@ -62,3 +64,12 @@ func _on_PlayerHeightStandard_pressed():

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

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


32 changes: 32 additions & 0 deletions addons/godot-xr-tools/user_settings/user_settings_ui.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,36 @@ text = "Snap turning:"
[node name="SnapTurningCB" type="CheckBox" parent="Input/SnapTurning"]
layout_mode = 2

[node name="yAxisDeadZone" type="HBoxContainer" parent="Input"]
layout_mode = 2

[node name="Label" type="Label" parent="Input/yAxisDeadZone"]
layout_mode = 2
theme_override_font_sizes/font_size = 12
text = "Y axis dead zone"

[node name="yAxisDeadZoneSlider" type="HSlider" parent="Input/yAxisDeadZone"]
layout_mode = 2
size_flags_horizontal = 3
max_value = 0.5
step = 0.01
value = 0.1

[node name="xAxisDeadZone" type="HBoxContainer" parent="Input"]
layout_mode = 2

[node name="Label" type="Label" parent="Input/xAxisDeadZone"]
layout_mode = 2
theme_override_font_sizes/font_size = 12
text = "X axis dead zone"

[node name="xAxisDeadZoneSlider" type="HSlider" parent="Input/xAxisDeadZone"]
layout_mode = 2
size_flags_horizontal = 3
max_value = 0.5
step = 0.01
value = 0.2

[node name="HSeparator" type="HSeparator" parent="Input"]
layout_mode = 2

Expand Down Expand Up @@ -131,6 +161,8 @@ theme_override_font_sizes/font_size = 12
text = "Reset"

[connection signal="pressed" from="Input/SnapTurning/SnapTurningCB" to="." method="_on_SnapTurningCB_pressed"]
[connection signal="value_changed" from="Input/yAxisDeadZone/yAxisDeadZoneSlider" to="." method="_on_y_axis_dead_zone_slider_value_changed"]
[connection signal="value_changed" from="Input/xAxisDeadZone/xAxisDeadZoneSlider" to="." method="_on_x_axis_dead_zone_slider_value_changed"]
[connection signal="pressed" from="Input/Buttons/Save" to="." method="_on_Save_pressed"]
[connection signal="pressed" from="Input/Buttons/Reset" to="." method="_on_Reset_pressed"]
[connection signal="drag_ended" from="Player/PlayerHeight/PlayerHeightSlider" to="." method="_on_PlayerHeightSlider_drag_ended"]
Expand Down
40 changes: 40 additions & 0 deletions addons/godot-xr-tools/xr_tools.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ static func set_grip_threshold(p_threshold : float) -> void:

ProjectSettings.set_setting("godot_xr_tools/input/grip_threshold", p_threshold)

static func get_y_axis_dead_zone() -> float:
# can return null which is not a float, so don't type this!
var deadzone = 0.1

if ProjectSettings.has_setting("godot_xr_tools/input/y_axis_dead_zone"):
deadzone = ProjectSettings.get_setting("godot_xr_tools/input/y_axis_dead_zone")

if !(deadzone >= 0.0 and deadzone <= 0.5):
# out of bounds? reset to default
deadzone = 0.1

return deadzone

static func set_y_axis_dead_zone(p_deadzone : float) -> void:
if !(p_deadzone >= 0.0 and p_deadzone <= 0.5):
print("Deadzone out of bounds")
return

ProjectSettings.set_setting("godot_xr_tools/input/y_axis_dead_zone", p_deadzone)

static func get_x_axis_dead_zone() -> float:
# can return null which is not a float, so don't type this!
var deadzone = 0.2

if ProjectSettings.has_setting("godot_xr_tools/input/x_axis_dead_zone"):
deadzone = ProjectSettings.get_setting("godot_xr_tools/input/x_axis_dead_zone")

if !(deadzone >= 0.0 and deadzone <= 0.5):
# out of bounds? reset to default
deadzone = 0.2

return deadzone

static func set_x_axis_dead_zone(p_deadzone : float) -> void:
if !(p_deadzone >= 0.0 and p_deadzone <= 0.5):
print("Deadzone out of bounds")
return

ProjectSettings.set_setting("godot_xr_tools/input/x_axis_dead_zone", p_deadzone)


static func get_snap_turning_deadzone() -> float:
# can return null which is not a float, so don't type this!
Expand Down
Loading