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

Added Function_Turn_movement provider #121

Merged
merged 1 commit into from
May 3, 2022
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
1 change: 1 addition & 0 deletions addons/godot-xr-tools/VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 4 additions & 57 deletions addons/godot-xr-tools/functions/Function_Direct_movement.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,98 +11,45 @@ extends MovementProvider
## attached to the players ARVROrigin.
##
## The following types of direct movement are supported:
## - Snap turning
## - Smooth turning
## - Slewing
## - Forwards and backwards motion
##
## The player may have multiple direct movement nodes attached to different
## 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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
90 changes: 90 additions & 0 deletions addons/godot-xr-tools/functions/Function_Turn_movement.gd
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions addons/godot-xr-tools/functions/Function_Turn_movement.tscn
Original file line number Diff line number Diff line change
@@ -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 )