From a904d18eb102acd53dc7749bb2e99b526338c8d6 Mon Sep 17 00:00:00 2001 From: AmyLillya Date: Mon, 4 Sep 2023 19:16:49 -0300 Subject: [PATCH] added new node to ball that returns a random position offset from the collision normal; added Utilities autoload --- Autoload/Debug.tscn | 2 +- Autoload/Utilities.gd | 10 ++++++++++ Ball/ball.gd | 4 ++++ Ball/ball.tscn | 18 ++++++++++++++++-- Ball/ball_meshes.gd | 12 ++++-------- Ball/ball_random_vector_node.gd | 20 ++++++++++++++++++++ project.godot | 6 ++++++ 7 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 Autoload/Utilities.gd create mode 100644 Ball/ball_random_vector_node.gd diff --git a/Autoload/Debug.tscn b/Autoload/Debug.tscn index 2798344..9dcac25 100644 --- a/Autoload/Debug.tscn +++ b/Autoload/Debug.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=2 format=3 uid="uid://cpey4r5s1qsy8"] +[gd_scene load_steps=2 format=3 uid="uid://ycvavb1vjn0w"] [ext_resource type="Script" path="res://Autoload/Debug.gd" id="1_imoie"] diff --git a/Autoload/Utilities.gd b/Autoload/Utilities.gd new file mode 100644 index 0000000..8cc0af4 --- /dev/null +++ b/Autoload/Utilities.gd @@ -0,0 +1,10 @@ +extends Node + + +# Apply this small offset to the up vector in the look_at function to fix it +# not working when the target is perfectly aligned with the up vector +const LOOK_AT_OFFSET_FIX = Vector3(0.01, 1, 0.01) + + +func uniform_vector3(value : float): + return Vector3(value, value, value) diff --git a/Ball/ball.gd b/Ball/ball.gd index 3d1e75c..f55965d 100644 --- a/Ball/ball.gd +++ b/Ball/ball.gd @@ -77,3 +77,7 @@ func _after_collision(): # Unfreezes the ball and apply the bounced captured velocity to it freeze = false linear_velocity = bounced_velocity + + ### DEBUG + freeze = true +# queue_free() diff --git a/Ball/ball.tscn b/Ball/ball.tscn index e5bf631..1fd79a2 100644 --- a/Ball/ball.tscn +++ b/Ball/ball.tscn @@ -1,13 +1,17 @@ -[gd_scene load_steps=6 format=3 uid="uid://rdlvlyf0l1l"] +[gd_scene load_steps=8 format=3 uid="uid://rdlvlyf0l1l"] [ext_resource type="Script" path="res://Ball/ball.gd" id="1_oenn5"] [ext_resource type="PackedScene" uid="uid://lge7m41oi6pn" path="res://Ball/Meshes/ball_bouncing.glb" id="2_ccq7m"] [ext_resource type="PackedScene" uid="uid://b1embyb2knvho" path="res://Ball/Meshes/ball_flying.glb" id="4_fh5j8"] [ext_resource type="Script" path="res://Ball/ball_meshes.gd" id="4_llldm"] +[ext_resource type="Script" path="res://Ball/ball_random_vector_node.gd" id="5_j25qm"] [sub_resource type="SphereShape3D" id="SphereShape3D_ogc07"] radius = 0.3 +[sub_resource type="BoxMesh" id="BoxMesh_yno20"] +size = Vector3(0.2, 0.2, 0.2) + [node name="Ball" type="RigidBody3D" groups=["Balls"]] collision_layer = 4 custom_integrator = true @@ -28,10 +32,20 @@ one_shot = true [node name="Meshes" type="Node3D" parent="."] script = ExtResource("4_llldm") -[node name="RotationNode" type="Node3D" parent="Meshes"] +[node name="DirectionNode" type="Node3D" parent="Meshes"] [node name="BallBouncingMesh" parent="Meshes" instance=ExtResource("2_ccq7m")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.3) visible = false [node name="BallFlyingMesh" parent="Meshes" instance=ExtResource("4_fh5j8")] + +[node name="RandomVectorNode" type="Node3D" parent="."] +script = ExtResource("5_j25qm") + +[node name="VectorPosition" type="Node3D" parent="RandomVectorNode"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1) + +[node name="debugmesh" type="MeshInstance3D" parent="RandomVectorNode/VectorPosition"] +mesh = SubResource("BoxMesh_yno20") +skeleton = NodePath("../../..") diff --git a/Ball/ball_meshes.gd b/Ball/ball_meshes.gd index 3805acb..ec5123e 100644 --- a/Ball/ball_meshes.gd +++ b/Ball/ball_meshes.gd @@ -10,10 +10,6 @@ var is_bouncing = false var max_squish_xy = 0.5 # Starts at 0 var max_squish_z = 0.8 # From 0 to 1 -# Apply a small offset to the up vector in the look_at function to fix it -# not working when the target is perfectly aligned with the up vector -const LOOK_AT_OFFSET_FIX = Vector3(0.01, 1, 0.01) - func _ready(): ball.ball_collision_started.connect(_on_collision) @@ -26,11 +22,11 @@ func _process(delta): func _align_mesh_to_direction(): - # Align the RotationNode to the movement direction - $RotationNode.look_at(ball.captured_velocity + ball.global_position, LOOK_AT_OFFSET_FIX) + # Align the DirectionNode to the movement direction + $DirectionNode.look_at(ball.captured_velocity + ball.global_position, Utilities.LOOK_AT_OFFSET_FIX) # Convert the rotation euler into a quaternion - var euler_rotation = $RotationNode.rotation + var euler_rotation = $DirectionNode.rotation var quaternion_rotation : Quaternion quaternion_rotation = quaternion_rotation.from_euler(euler_rotation) @@ -44,7 +40,7 @@ func _on_collision(): is_bouncing = true # Align the Meshes node to the surface normal so the bouncing mesh is properly aligned - look_at(ball.collision_normal + ball.global_position, LOOK_AT_OFFSET_FIX) + look_at(ball.collision_normal + ball.global_position, Utilities.LOOK_AT_OFFSET_FIX) func _after_collision(): diff --git a/Ball/ball_random_vector_node.gd b/Ball/ball_random_vector_node.gd new file mode 100644 index 0000000..069fcf6 --- /dev/null +++ b/Ball/ball_random_vector_node.gd @@ -0,0 +1,20 @@ +extends Node3D + +@onready var ball = owner + + +func _ready(): + ball.ball_collision_started.connect(_get_random_offset) + + +func _get_random_offset(): + # Align rotation to collision normal + look_at(ball.collision_normal + ball.global_position, Utilities.LOOK_AT_OFFSET_FIX) + + # Compose a random vector + var x = randf_range(-0.5, 0.5) + var y = randf_range(-0.5, 0.5) + var z = randf_range(0, 1) + var random_vector = Vector3(x, y, z) + + $VectorPosition.position += random_vector diff --git a/project.godot b/project.godot index bba1a80..bac21c1 100644 --- a/project.godot +++ b/project.godot @@ -17,6 +17,7 @@ config/icon="res://icon.svg" [autoload] +Utilities="*res://Autoload/Utilities.gd" Debug="*res://Autoload/Debug.tscn" [input] @@ -57,6 +58,11 @@ debug_1={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":49,"key_label":0,"unicode":49,"echo":false,"script":null) ] } +debug_2={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":50,"key_label":0,"unicode":50,"echo":false,"script":null) +] +} [layer_names]