Skip to content

Commit

Permalink
Implement grip threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Oct 25, 2022
1 parent f319f15 commit 836aff6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 31 deletions.
3 changes: 3 additions & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 3.1.0
- Use value based grip input with threshold

# 3.0.0
- Included demo project with test scenes to evaluate features
- Standardized class naming convention for all scripts to "XRTools<PascalCaseName>"
Expand Down
57 changes: 27 additions & 30 deletions addons/godot-xr-tools/functions/function_pickup.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,17 @@ signal has_picked_up(what)
signal has_dropped


# enum our buttons, should find a way to put this more central
enum Buttons {
VR_BUTTON_BY = 1,
VR_GRIP = 2,
VR_BUTTON_3 = 3,
VR_BUTTON_4 = 4,
VR_BUTTON_5 = 5,
VR_BUTTON_6 = 6,
VR_BUTTON_AX = 7,
VR_BUTTON_8 = 8,
VR_BUTTON_9 = 9,
VR_BUTTON_10 = 10,
VR_BUTTON_11 = 11,
VR_BUTTON_12 = 12,
VR_BUTTON_13 = 13,
VR_PAD = 14,
VR_TRIGGER = 15
}

# Constant for worst-case grab distance
const MAX_GRAB_DISTANCE2: float = 1000000.0


## Grip controller button
export (Buttons) var pickup_button_id = Buttons.VR_GRIP
export (XRTools.Axis) var pickup_axis_id = XRTools.Axis.VR_GRIP_AXIS
onready var grip_threshold = XRTools.get_grip_threshold()
var grip_pressed = false

## Action controller button
export (Buttons) var action_button_id = Buttons.VR_TRIGGER
export (XRTools.Buttons) var action_button_id = XRTools.Buttons.VR_TRIGGER

## Grab distance
export var grab_distance : float = 0.3 setget _set_grab_distance
Expand Down Expand Up @@ -136,6 +119,15 @@ func _process(delta):
if !_controller.get_is_active():
return

# Handle our grip
var grip_value = _controller.get_joystick_axis(pickup_axis_id)
if (grip_pressed and grip_value < (grip_threshold - 0.1)):
grip_pressed = false
_on_grip_release()
elif (!grip_pressed and grip_value > (grip_threshold + 0.1)):
grip_pressed = true
_on_grip_pressed()

# Calculate average velocity
if is_instance_valid(picked_up_object) and picked_up_object.is_picked_up():
# Average velocity of picked up object
Expand Down Expand Up @@ -341,17 +333,22 @@ func _pick_up_object(target: Spatial) -> void:


func _on_button_pressed(p_button) -> void:
if p_button == pickup_button_id:
if is_instance_valid(picked_up_object) and !picked_up_object.press_to_hold:
drop_object()
elif is_instance_valid(closest_object):
_pick_up_object(closest_object)
elif p_button == action_button_id:
if p_button == action_button_id:
if is_instance_valid(picked_up_object) and picked_up_object.has_method("action"):
picked_up_object.action()


func _on_button_release(p_button) -> void:
if p_button == pickup_button_id:
if is_instance_valid(picked_up_object) and picked_up_object.press_to_hold:
drop_object()
pass


func _on_grip_pressed() -> void:
if is_instance_valid(picked_up_object) and !picked_up_object.press_to_hold:
drop_object()
elif is_instance_valid(closest_object):
_pick_up_object(closest_object)


func _on_grip_release() -> void:
if is_instance_valid(picked_up_object) and picked_up_object.press_to_hold:
drop_object()
81 changes: 80 additions & 1 deletion addons/godot-xr-tools/plugin.gd
Original file line number Diff line number Diff line change
@@ -1,4 +1,83 @@
tool
extends EditorPlugin
class_name XRTools

# We don't have any tool elements here yet

# enum our axis
enum Axis {
VR_PRIMARY_X_AXIS = 0,
VR_PRIMARY_Y_AXIS = 1,
VR_SECONDARY_X_AXIS = 6,
VR_SECONDARY_Y_AXIS = 7,
VR_TRIGGER_AXIS = 2,
VR_GRIP_AXIS = 4
}


# enum our buttons
enum Buttons {
VR_BUTTON_BY = 1,
VR_GRIP = 2,
VR_BUTTON_3 = 3,
VR_BUTTON_4 = 4,
VR_BUTTON_5 = 5,
VR_BUTTON_6 = 6,
VR_BUTTON_AX = 7,
VR_BUTTON_8 = 8,
VR_BUTTON_9 = 9,
VR_BUTTON_10 = 10,
VR_BUTTON_11 = 11,
VR_BUTTON_12 = 12,
VR_BUTTON_13 = 13,
VR_PAD = 14,
VR_TRIGGER = 15
}


static func get_grip_threshold() -> float:
# can return null which is not a float, so don't type this!
var threshold = ProjectSettings.get_setting("godot_xr_tools/input/grip_threshold")

if threshold == null:
# plugin disabled or setting not saved, return our default.
threshold = 0.7
if !(threshold >= 0.2 and threshold <= 0.8):
# out of bounds? reset to default
threshold = 0.7

return threshold

static func set_grip_threshold(p_threshold : float) -> void:
if !(p_threshold >= 0.2 and p_threshold <= 0.8):
print("Threshold out of bounds")
return

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

func _define_project_setting(p_name : String, p_type : int, p_hint : int = PROPERTY_HINT_NONE , p_hint_string : String = "", p_default_val = "") -> void:
# p_default_val can be any type!!

if !ProjectSettings.has_setting(p_name):
ProjectSettings.set_setting(p_name, p_default_val)

var property_info : Dictionary = {
"name" : p_name,
"type" : p_type,
"hint" : p_hint,
"hint_string" : p_hint_string
}

ProjectSettings.add_property_info(property_info)
ProjectSettings.set_initial_value(p_name, p_default_val)


func _enter_tree():
# our plugin is loaded

# provide meta data for our project settings
_define_project_setting("godot_xr_tools/input/grip_threshold", TYPE_REAL, PROPERTY_HINT_RANGE, "0.2,0.8,0.05", 0.7)


func _exit_tree():
# our plugin is turned off
pass
6 changes: 6 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://assets/meshes/teleport/teleport.gd"
}, {
"base": "EditorPlugin",
"class": "XRTools",
"language": "GDScript",
"path": "res://addons/godot-xr-tools/plugin.gd"
}, {
"base": "Spatial",
"class": "XRToolsClimbable",
"language": "GDScript",
Expand Down Expand Up @@ -233,6 +238,7 @@ _global_script_class_icons={
"ARVRHelpers": "",
"SceneBase": "",
"Teleport": "",
"XRTools": "",
"XRToolsClimbable": "res://addons/godot-xr-tools/editor/icons/hand.svg",
"XRToolsFallDamage": "",
"XRToolsFunctionPickup": "res://addons/godot-xr-tools/editor/icons/function.svg",
Expand Down

0 comments on commit 836aff6

Please sign in to comment.