Skip to content

Commit

Permalink
Merge pull request #89 from Malcolmnixon/playerbody-collision
Browse files Browse the repository at this point in the history
Added collision layer and mask exported properties
  • Loading branch information
BastiaanOlij authored Mar 5, 2022
2 parents f0e25fe + 23715d5 commit cb53be2
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions addons/godot-xr-tools/assets/PlayerBody.gd
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ extends Node
##

## PlayerBody enabled flag
export var enabled := true setget set_enabled, get_enabled
export var enabled := true setget set_enabled

## Player radius
export var player_radius := 0.4
export var player_radius := 0.4 setget set_player_radius

## Player head height (distance between between camera and top of head)
export var player_head_height := 0.1
Expand All @@ -39,7 +39,13 @@ export var gravity := -9.8
export var push_rigid_bodies := true

## GroundPhysicsSettings to apply - can only be typed in Godot 4+
export (Resource) var physics = null setget set_physics, get_physics
export (Resource) var physics = null setget set_physics

# Set our collision layer
export (int, LAYERS_3D_PHYSICS) var collision_layer = 1 << 19 setget set_collision_layer

# Set our collision mask
export (int, LAYERS_3D_PHYSICS) var collision_mask = 1023 setget set_collision_mask

## ARVROrigin node
onready var origin_node := ARVRHelpers.get_arvr_origin(self)
Expand Down Expand Up @@ -94,9 +100,18 @@ func _ready():
_movement_providers = get_tree().get_nodes_in_group("movement_providers")
_movement_providers.sort_custom(SortProviderByOrder, "sort_by_order")

# Propagate defaults
_update_enabled()
_update_player_radius()
_update_collision_layer()
_update_collision_mask()

func set_enabled(new_value):
enabled = new_value
if is_inside_tree():
_update_enabled()

func _update_enabled() -> void:
# Update collision_shape
if _collision_node:
_collision_node.disabled = !enabled
Expand All @@ -105,16 +120,37 @@ func set_enabled(new_value):
if enabled:
set_physics_process(true)

func get_enabled():
return enabled
func set_player_radius(new_value: float) -> void:
player_radius = new_value
if is_inside_tree():
_update_player_radius()

func _update_player_radius() -> void:
if _collision_node and _collision_node.shape:
_collision_node.shape.radius = player_radius

func set_physics(new_value: Resource):
func set_physics(new_value: Resource) -> void:
# Save the property
physics = new_value
default_physics = _guaranteed_physics()

func get_physics() -> Resource:
return physics
func set_collision_layer(new_layer: int) -> void:
collision_layer = new_layer
if is_inside_tree():
_update_collision_layer()

func _update_collision_layer() -> void:
if kinematic_node:
kinematic_node.collision_layer = collision_layer

func set_collision_mask(new_mask: int) -> void:
collision_mask = new_mask
if is_inside_tree():
_update_collision_mask()

func _update_collision_mask() -> void:
if kinematic_node:
kinematic_node.collision_mask = collision_mask

func _physics_process(delta):
# Do not run physics if in the editor
Expand Down

0 comments on commit cb53be2

Please sign in to comment.