diff --git a/addons/godot-xr-tools/VERSIONS.md b/addons/godot-xr-tools/VERSIONS.md index 204cb186..a2c3330b 100644 --- a/addons/godot-xr-tools/VERSIONS.md +++ b/addons/godot-xr-tools/VERSIONS.md @@ -5,6 +5,7 @@ - Moved flight logic from Function_Direct_movement to Function_Flight_movement - Added option to disable player sliding on slopes - Added support for remote grabbing +- Moved turning logic from Function_Direct_movement to Function_Turn_movement # 2.3.0 - Added vignette diff --git a/addons/godot-xr-tools/functions/Function_Direct_movement.gd b/addons/godot-xr-tools/functions/Function_Direct_movement.gd index db9b6af7..15d2642b 100644 --- a/addons/godot-xr-tools/functions/Function_Direct_movement.gd +++ b/addons/godot-xr-tools/functions/Function_Direct_movement.gd @@ -11,8 +11,6 @@ extends MovementProvider ## attached to the players ARVROrigin. ## ## The following types of direct movement are supported: -## - Snap turning -## - Smooth turning ## - Slewing ## - Forwards and backwards motion ## @@ -20,89 +18,38 @@ extends MovementProvider ## controllers to provide different types of direct movement. ## -enum MOVEMENT_TYPE { MOVE_AND_ROTATE, MOVE_AND_STRAFE } ## Movement provider order export var order := 10 -## Use smooth rotation (may cause motion sickness) -export var smooth_rotation := false - -## Smooth turn speed in radians per second -export var smooth_turn_speed := 2.0 - -## Seconds per step (at maximum turn rate) -export var step_turn_delay := 0.2 - -## Step turn angle in degrees -export var step_turn_angle := 20.0 - ## Movement speed export var max_speed := 10.0 -## Type of movement to perform -export (MOVEMENT_TYPE) var move_type = MOVEMENT_TYPE.MOVE_AND_ROTATE +## Enable player strafing +export var strafe := false -# Turn step accumulator -var _turn_step := 0.0 # Controller node onready var _controller : ARVRController = get_parent() + # Perform jump movement func physics_movement(delta: float, player_body: PlayerBody): # Skip if the controller isn't active if !_controller.get_is_active(): return - # Handle rotation - if move_type == MOVEMENT_TYPE.MOVE_AND_ROTATE: - _perform_player_rotation(delta, player_body) - # Apply forwards/backwards ground control player_body.ground_control_velocity.y += _controller.get_joystick_axis(1) * max_speed # Apply left/right ground control - if move_type == MOVEMENT_TYPE.MOVE_AND_STRAFE: + if strafe: player_body.ground_control_velocity.x += _controller.get_joystick_axis(0) * max_speed # Clamp ground control player_body.ground_control_velocity.x = clamp(player_body.ground_control_velocity.x, -max_speed, max_speed) player_body.ground_control_velocity.y = clamp(player_body.ground_control_velocity.y, -max_speed, max_speed) -# Perform rotation based on the players rotation controller input -func _perform_player_rotation(delta: float, player_body: PlayerBody): - var left_right := _controller.get_joystick_axis(0) - - if abs(left_right) <= 0.1: - # Not turning - _turn_step = 0.0 - return - - # Handle smooth rotation - if smooth_rotation: - _rotate_player(player_body, smooth_turn_speed * delta * left_right) - return - - # Update the next turn-step delay - _turn_step -= abs(left_right) * delta - if _turn_step >= 0.0: - return - - # Turn one step in the requested direction - _turn_step = step_turn_delay - _rotate_player(player_body, deg2rad(step_turn_angle) * sign(left_right)) - -# Rotate the origin node around the camera -func _rotate_player(player_body: PlayerBody, angle: float): - var t1 := Transform() - var t2 := Transform() - var rot := Transform() - - t1.origin = -player_body.camera_node.transform.origin - t2.origin = player_body.camera_node.transform.origin - rot = rot.rotated(Vector3(0.0, -1.0, 0.0), angle) - player_body.origin_node.transform = (player_body.origin_node.transform * t2 * rot * t1).orthonormalized() # This method verifies the MovementProvider has a valid configuration. func _get_configuration_warning(): diff --git a/addons/godot-xr-tools/functions/Function_Direct_movement.tscn b/addons/godot-xr-tools/functions/Function_Direct_movement.tscn index 383678cb..d981d495 100644 --- a/addons/godot-xr-tools/functions/Function_Direct_movement.tscn +++ b/addons/godot-xr-tools/functions/Function_Direct_movement.tscn @@ -4,4 +4,8 @@ [node name="Function_Direct_movement" type="Node" groups=["movement_providers"]] script = ExtResource( 1 ) -controller = null +smooth_rotation = false +smooth_turn_speed = 2.0 +step_turn_delay = 0.2 +step_turn_angle = 20.0 +move_type = 0 diff --git a/addons/godot-xr-tools/functions/Function_Turn_movement.gd b/addons/godot-xr-tools/functions/Function_Turn_movement.gd new file mode 100644 index 00000000..b5ac6a30 --- /dev/null +++ b/addons/godot-xr-tools/functions/Function_Turn_movement.gd @@ -0,0 +1,90 @@ +tool +class_name Function_TurnMovement +extends MovementProvider + +## +## Movement Provider for Turning +## +## @desc: +## This script provides turning support for the player. This script works +## with the PlayerBody attached to the players ARVROrigin. +## +## The following types of turning are supported: +## - Snap turning +## - Smooth turning +## + + +## Movement provider order +export var order := 5 + +## Use smooth rotation (may cause motion sickness) +export var smooth_rotation := false + +## Smooth turn speed in radians per second +export var smooth_turn_speed := 2.0 + +## Seconds per step (at maximum turn rate) +export var step_turn_delay := 0.2 + +## Step turn angle in degrees +export var step_turn_angle := 20.0 + + +# Turn step accumulator +var _turn_step := 0.0 + + +# Controller node +onready var _controller : ARVRController = get_parent() + + +# Perform jump movement +func physics_movement(delta: float, player_body: PlayerBody): + # Skip if the controller isn't active + if !_controller.get_is_active(): + return + + # Read the left/right joystick axis + var left_right := _controller.get_joystick_axis(0) + if abs(left_right) <= 0.1: + # Not turning + _turn_step = 0.0 + return + + # Handle smooth rotation + if smooth_rotation: + _rotate_player(player_body, smooth_turn_speed * delta * left_right) + return + + # Update the next turn-step delay + _turn_step -= abs(left_right) * delta + if _turn_step >= 0.0: + return + + # Turn one step in the requested direction + _turn_step = step_turn_delay + _rotate_player(player_body, deg2rad(step_turn_angle) * sign(left_right)) + + +# Rotate the origin node around the camera +func _rotate_player(player_body: PlayerBody, angle: float): + var t1 := Transform() + var t2 := Transform() + var rot := Transform() + + t1.origin = -player_body.camera_node.transform.origin + t2.origin = player_body.camera_node.transform.origin + rot = rot.rotated(Vector3(0.0, -1.0, 0.0), angle) + player_body.origin_node.transform = (player_body.origin_node.transform * t2 * rot * t1).orthonormalized() + + +# This method verifies the MovementProvider has a valid configuration. +func _get_configuration_warning(): + # Check the controller node + var test_controller = get_parent() + if !test_controller or !test_controller is ARVRController: + return "Unable to find ARVR Controller node" + + # Call base class + return ._get_configuration_warning() diff --git a/addons/godot-xr-tools/functions/Function_Turn_movement.tscn b/addons/godot-xr-tools/functions/Function_Turn_movement.tscn new file mode 100644 index 00000000..b636b6d2 --- /dev/null +++ b/addons/godot-xr-tools/functions/Function_Turn_movement.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://addons/godot-xr-tools/functions/Function_Turn_movement.gd" type="Script" id=1] + +[node name="Function_Turn_movement" type="Node" groups=["movement_providers"]] +script = ExtResource( 1 )