Compare commits

...

2 Commits

7 changed files with 59 additions and 12 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"]

11
Autoload/Utilities.gd Normal file
View File

@ -0,0 +1,11 @@
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)
# Compose an uniform Vector3 from a float value
func uniform_vec3(value : float):
return Vector3(value, value, value)

View File

@ -20,7 +20,6 @@ func _physics_process(delta):
_get_collision(delta)
### DEBUG
# Delete balls that escape the level
if global_position.length() > 20.0:
@ -77,3 +76,8 @@ 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,9 +1,10 @@
[gd_scene load_steps=6 format=3 uid="uid://rdlvlyf0l1l"]
[gd_scene load_steps=7 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
@ -28,10 +29,16 @@ 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)

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,23 @@
extends Node3D
@onready var ball = owner
var random_offset : Vector3
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
random_offset = $VectorPosition.position

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]