Skip to content

Commit

Permalink
Merge pull request #226 from BastiaanOlij/poke_feature
Browse files Browse the repository at this point in the history
Implement poke feature and demo
  • Loading branch information
BastiaanOlij authored Oct 26, 2022
2 parents cf815b1 + 098b7b1 commit 6a9390f
Show file tree
Hide file tree
Showing 14 changed files with 485 additions and 22 deletions.
1 change: 1 addition & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Use value based grip input with threshold
- Improved pointer demo supporting left hand with switching
- Enhanced pointer laser visibility options for colliding with targets.
- Implement poke feature (finger interaction)

# 3.0.0
- Included demo project with test scenes to evaluate features
Expand Down
112 changes: 112 additions & 0 deletions addons/godot-xr-tools/assets/poke.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
tool
class_name XRToolsPoke
extends Spatial

export var enabled = true setget set_enabled
export var radius = 0.005 setget set_radius
export var teleport_distance : float = 0.1 setget set_teleport_distance
export var color : Color = Color(0.8, 0.8, 1.0, 0.5) setget set_color

var is_ready = false
var material : SpatialMaterial
var target : Node ## Node we last started touching
var last_collided_at : Vector3

func set_enabled(new_enabled : bool) -> void:
enabled = new_enabled
if is_ready:
_update_enabled()

func _update_enabled():
$PokeBody/CollisionShape.disabled = !enabled

func set_radius(new_radius : float) -> void:
radius = new_radius
if is_ready:
_update_radius()

func _update_radius() -> void:
var shape : SphereShape = $PokeBody/CollisionShape.shape
if shape:
shape.radius = radius

var mesh : SphereMesh = $PokeBody/MeshInstance.mesh
if mesh:
mesh.radius = radius
mesh.height = radius * 2.0

if material:
$PokeBody/MeshInstance.set_surface_material(0, material)

func set_teleport_distance(new_distance : float) -> void:
teleport_distance = new_distance
if is_ready:
_update_set_teleport_distance()

func _update_set_teleport_distance() -> void:
$PokeBody.teleport_distance = teleport_distance

func set_color(new_color : Color) -> void:
color = new_color
if is_ready:
_update_color()

func _update_color() -> void:
if material:
material.albedo_color = color

# Called when the node enters the scene tree for the first time.
func _ready():
# Set as top level ensures we're placing this object in global space
$PokeBody.set_as_toplevel(true)

is_ready = true
material = SpatialMaterial.new()
material.flags_unshaded = true
material.flags_transparent = true

_update_enabled()
_update_radius()
_update_set_teleport_distance()
_update_color()

func _process(_delta):
if is_instance_valid(target):
var new_at = $PokeBody.global_transform.origin

if target.has_signal("pointer_moved"):
target.emit_signal("pointer_moved", last_collided_at, new_at)
elif target.has_method("pointer_moved"):
target.pointer_moved(last_collided_at, new_at)

last_collided_at = new_at
else:
set_process(false)


func _on_PokeBody_body_entered(body):
# We are going to poke this body at our current position.
# This will be slightly above the object but since this
# mostly targets Viewport2Din3D, this will work

if body.has_signal("pointer_pressed"):
target = body
last_collided_at = $PokeBody.global_transform.origin
target.emit_signal("pointer_pressed", last_collided_at)
elif body.has_method("pointer_pressed"):
target = body
last_collided_at = $PokeBody.global_transform.origin
target.pointer_pressed(last_collided_at)

if target:
set_process(true)

func _on_PokeBody_body_exited(body):
if body.has_signal("pointer_released"):
body.emit_signal("pointer_released", last_collided_at)
elif body.has_method("pointer_released"):
body.pointer_released(last_collided_at)

# if we were tracking this target, clear it
if target == body:
target = null
42 changes: 42 additions & 0 deletions addons/godot-xr-tools/assets/poke.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://addons/godot-xr-tools/assets/poke.gd" type="Script" id=1]
[ext_resource path="res://addons/godot-xr-tools/assets/poke_body.gd" type="Script" id=2]

[sub_resource type="SphereShape" id=1]
resource_local_to_scene = true
radius = 0.005

[sub_resource type="SphereMesh" id=2]
resource_local_to_scene = true
radius = 0.005
height = 0.01
radial_segments = 32
rings = 16

[sub_resource type="SpatialMaterial" id=3]
flags_transparent = true
flags_unshaded = true
albedo_color = Color( 0.8, 0.8, 1, 0.5 )

[node name="Poke" type="Spatial"]
script = ExtResource( 1 )

[node name="PokeBody" type="RigidBody" parent="."]
collision_layer = 131072
collision_mask = 65535
gravity_scale = 0.0
custom_integrator = true
contacts_reported = 1
contact_monitor = true
script = ExtResource( 2 )

[node name="CollisionShape" type="CollisionShape" parent="PokeBody"]
shape = SubResource( 1 )

[node name="MeshInstance" type="MeshInstance" parent="PokeBody"]
mesh = SubResource( 2 )
material/0 = SubResource( 3 )

[connection signal="body_entered" from="PokeBody" to="." method="_on_PokeBody_body_entered"]
[connection signal="body_exited" from="PokeBody" to="." method="_on_PokeBody_body_exited"]
23 changes: 23 additions & 0 deletions addons/godot-xr-tools/assets/poke_body.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
extends RigidBody

# distance at which we teleport our poke body
export var teleport_distance : float = 0.2

func _integrate_forces(state: PhysicsDirectBodyState):
# get the position of our parent that we are following
var following_transform = get_parent().global_transform

# see how much we need to move
var delta_movement = following_transform.origin - state.transform.origin
var delta_length = delta_movement.length()

if delta_length > teleport_distance:
# teleport our poke body to its new location
state.angular_velocity = Vector3()
state.linear_velocity = Vector3()
state.transform.origin = following_transform.origin
else:
# trigger physics to move our body in one step
state.angular_velocity = Vector3()
state.linear_velocity = delta_movement / state.step
state.integrate_forces()
6 changes: 6 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://addons/godot-xr-tools/assets/player_body.gd"
}, {
"base": "Spatial",
"class": "XRToolsPoke",
"language": "GDScript",
"path": "res://addons/godot-xr-tools/assets/poke.gd"
}, {
"base": "Area",
"class": "XRToolsSnapZone",
"language": "GDScript",
Expand Down Expand Up @@ -275,6 +280,7 @@ _global_script_class_icons={
"XRToolsPhysicsHand": "",
"XRToolsPickable": "",
"XRToolsPlayerBody": "res://addons/godot-xr-tools/editor/icons/body.svg",
"XRToolsPoke": "",
"XRToolsSnapZone": "",
"XRToolsVelocityAverager": "",
"XRToolsVelocityAveragerLinear": "",
Expand Down
25 changes: 25 additions & 0 deletions scenes/main_menu/main_menu_level.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tool
extends SceneBase

func _update_demo_positions() -> void:
var count = $Demos.get_child_count()
if count > 1:
var angle = 2.0 * PI / count
for i in count:
var t = Transform()
t.origin = Vector3(0.0, 0.0, -7.0)
t = t.rotated(Vector3.UP, angle * i)

$Demos.get_child(i).transform = t


func _ready():
_update_demo_positions()


func _on_Demos_child_entered_tree(_node):
_update_demo_positions()


func _on_Demos_child_exiting_tree(_node):
_update_demo_positions()
60 changes: 38 additions & 22 deletions scenes/main_menu/main_menu_level.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=23 format=2]
[gd_scene load_steps=26 format=2]

[ext_resource path="res://scenes/scene_base.tscn" type="PackedScene" id=1]
[ext_resource path="res://addons/godot-xr-tools/assets/right_hand.tscn" type="PackedScene" id=2]
Expand All @@ -22,8 +22,13 @@
[ext_resource path="res://scenes/pointer_demo/pointer_demo.png" type="Texture" id=20]
[ext_resource path="res://scenes/pickable_demo/pickable_demo.tscn" type="PackedScene" id=21]
[ext_resource path="res://scenes/pickable_demo/pickable_demo.png" type="Texture" id=22]
[ext_resource path="res://scenes/main_menu/main_menu_level.gd" type="Script" id=23]
[ext_resource path="res://scenes/poke_demo/poke_demo.tscn" type="PackedScene" id=24]
[ext_resource path="res://scenes/poke_demo/poke_demo.png" type="Texture" id=25]

[node name="MainMenuLevel" instance=ExtResource( 1 )]
script = ExtResource( 23 )
environment = null

[node name="LeftHand" parent="ARVROrigin/LeftHand" index="0" instance=ExtResource( 3 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.03, -0.05, 0.15 )
Expand All @@ -50,44 +55,55 @@ smooth_rotation = true

[node name="BasicMap" parent="." index="1" instance=ExtResource( 4 )]

[node name="ToBasicMovementDemo" parent="." index="2" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, -6 )
scene_base = NodePath("..")
[node name="Demos" type="Spatial" parent="." index="2"]

[node name="ToBasicMovementDemo" parent="Demos" index="0" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -7 )
scene_base = NodePath("../..")
scene = ExtResource( 7 )
title = ExtResource( 10 )

[node name="ToTeleportDemo" parent="." index="3" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -8, 0, -6 )
scene_base = NodePath("..")
[node name="ToTeleportDemo" parent="Demos" index="1" instance=ExtResource( 9 )]
transform = Transform( 0.707107, 0, 0.707107, 0, 1, 0, -0.707107, 0, 0.707107, -4.94975, 0, -4.94975 )
scene_base = NodePath("../..")
scene = ExtResource( 11 )
title = ExtResource( 12 )

[node name="ToClimbingGlidingDemo" parent="." index="4" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -4, 0, -6 )
scene_base = NodePath("..")
[node name="ToClimbingGlidingDemo" parent="Demos" index="2" instance=ExtResource( 9 )]
transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -7, 0, 3.0598e-07 )
scene_base = NodePath("../..")
scene = ExtResource( 14 )
title = ExtResource( 13 )

[node name="ToGrapplingDemo" parent="." index="5" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -6 )
scene_base = NodePath("..")
[node name="ToGrapplingDemo" parent="Demos" index="3" instance=ExtResource( 9 )]
transform = Transform( -0.707107, 0, 0.707107, 0, 1, 0, -0.707107, 0, -0.707107, -4.94975, 0, 4.94975 )
scene_base = NodePath("../..")
scene = ExtResource( 15 )
title = ExtResource( 16 )

[node name="ToInteractablesDemo" parent="." index="6" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 4, 0, -6 )
scene_base = NodePath("..")
[node name="ToInteractablesDemo" parent="Demos" index="4" instance=ExtResource( 9 )]
transform = Transform( -1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 6.11959e-07, 0, 7 )
scene_base = NodePath("../..")
scene = ExtResource( 18 )
title = ExtResource( 17 )

[node name="ToPointerDemo" parent="." index="7" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 8, 0, -6 )
scene_base = NodePath("..")
[node name="ToPointerDemo" parent="Demos" index="5" instance=ExtResource( 9 )]
transform = Transform( -0.707107, 0, -0.707107, 0, 1, 0, 0.707107, 0, -0.707107, 4.94975, 0, 4.94975 )
scene_base = NodePath("../..")
scene = ExtResource( 19 )
title = ExtResource( 20 )

[node name="ToPickableDemo" parent="." index="8" instance=ExtResource( 9 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 12, 0, -6 )
scene_base = NodePath("..")
[node name="ToPickableDemo" parent="Demos" index="6" instance=ExtResource( 9 )]
transform = Transform( 1.19249e-08, 0, -1, 0, 1, 0, 1, 0, 1.19249e-08, 7, 0, -8.34742e-08 )
scene_base = NodePath("../..")
scene = ExtResource( 21 )
title = ExtResource( 22 )

[node name="ToPokeDemo" parent="Demos" index="7" instance=ExtResource( 9 )]
transform = Transform( 0.707107, 0, -0.707107, 0, 1, 0, 0.707107, 0, 0.707107, 4.94975, 0, -4.94975 )
scene_base = NodePath("../..")
scene = ExtResource( 24 )
title = ExtResource( 25 )

[connection signal="child_entered_tree" from="Demos" to="." method="_on_Demos_child_entered_tree"]
[connection signal="child_exiting_tree" from="Demos" to="." method="_on_Demos_child_exiting_tree"]
4 changes: 4 additions & 0 deletions scenes/pickable_demo/pickable_demo.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

[node name="LeftPhysicsHand" parent="ARVROrigin/LeftHand" index="0" instance=ExtResource( 3 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.03, -0.05, 0.15 )
collision_layer = 131072
margin = 0.004

[node name="MovementDirect" parent="ARVROrigin/LeftHand" index="1" instance=ExtResource( 7 )]
enabled = true
Expand All @@ -37,6 +39,8 @@ ranged_collision_mask = 4

[node name="RightPhysicsHand" parent="ARVROrigin/RightHand" index="0" instance=ExtResource( 4 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.03, -0.05, 0.15 )
collision_layer = 131072
margin = 0.004

[node name="MovementDirect" parent="ARVROrigin/RightHand" index="1" instance=ExtResource( 7 )]
enabled = true
Expand Down
Loading

0 comments on commit 6a9390f

Please sign in to comment.