added new node to ball that returns a random position offset from the collision normal; added Utilities autoload

This commit is contained in:
AmyLillya 2023-09-04 19:16:49 -03:00
parent af1bdeb3ca
commit a904d18eb1
7 changed files with 61 additions and 11 deletions

View File

@ -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"]

10
Autoload/Utilities.gd Normal file
View File

@ -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)

View File

@ -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()

View File

@ -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("../../..")

View File

@ -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():

View File

@ -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

View File

@ -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]