-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from BastiaanOlij/poke_feature
Implement poke feature and demo
- Loading branch information
Showing
14 changed files
with
485 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.