Compare commits

...

7 Commits

12 changed files with 133 additions and 268 deletions

11
Autoload/Debug.gd Normal file
View File

@ -0,0 +1,11 @@
extends Node
func _process(delta):
if Input.is_action_just_pressed("debug_1"):
print("total balls: ", get_total_ball_count())
func get_total_ball_count():
var total_balls : int = get_tree().get_nodes_in_group("Balls").size()
return total_balls

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

@ -1,63 +0,0 @@
[gd_resource type="VisualShader" load_steps=9 format=3 uid="uid://ny01m62ohegv"]
[sub_resource type="VisualShaderNodeVectorOp" id="VisualShaderNodeVectorOp_sbl4o"]
operator = 8
[sub_resource type="VisualShaderNodeVectorCompose" id="VisualShaderNodeVectorCompose_7ys52"]
default_input_values = [0, 0.0, 1, 0.0, 2, 1.0]
[sub_resource type="VisualShaderNodeVectorDecompose" id="VisualShaderNodeVectorDecompose_o0c6e"]
default_input_values = [0, Vector2(0, 0)]
op_type = 0
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_5rytf"]
input_name = "screen_uv"
[sub_resource type="VisualShaderNodeVectorOp" id="VisualShaderNodeVectorOp_nwrdd"]
default_input_values = [0, Vector2(0, 0), 1, Vector2(0.5, 0.5)]
op_type = 0
operator = 1
[sub_resource type="VisualShaderNodeVectorFunc" id="VisualShaderNodeVectorFunc_711ug"]
[sub_resource type="VisualShaderNodeInput" id="VisualShaderNodeInput_fou4c"]
input_name = "normal"
[sub_resource type="VisualShaderNodeVectorOp" id="VisualShaderNodeVectorOp_2wva2"]
operator = 2
[resource]
code = "shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_lambert, specular_schlick_ggx;
void fragment() {
// Input:7
vec3 n_out7p0 = NORMAL;
// Output:0
ALBEDO = n_out7p0;
}
"
nodes/fragment/3/node = SubResource("VisualShaderNodeInput_5rytf")
nodes/fragment/3/position = Vector2(-1780, 340)
nodes/fragment/5/node = SubResource("VisualShaderNodeVectorOp_nwrdd")
nodes/fragment/5/position = Vector2(-1480, 400)
nodes/fragment/6/node = SubResource("VisualShaderNodeVectorFunc_711ug")
nodes/fragment/6/position = Vector2(-920, 400)
nodes/fragment/7/node = SubResource("VisualShaderNodeInput_fou4c")
nodes/fragment/7/position = Vector2(-880, 200)
nodes/fragment/9/node = SubResource("VisualShaderNodeVectorOp_2wva2")
nodes/fragment/9/position = Vector2(-400, 200)
nodes/fragment/10/node = SubResource("VisualShaderNodeVectorOp_sbl4o")
nodes/fragment/10/position = Vector2(-200, 260)
nodes/fragment/11/node = SubResource("VisualShaderNodeVectorCompose_7ys52")
nodes/fragment/11/position = Vector2(-1120, 400)
nodes/fragment/12/node = SubResource("VisualShaderNodeVectorDecompose_o0c6e")
nodes/fragment/12/position = Vector2(-1320, 400)
nodes/fragment/connections = PackedInt32Array(3, 0, 5, 0, 7, 0, 9, 0, 9, 0, 10, 0, 6, 0, 10, 1, 5, 0, 11, 0, 5, 0, 12, 0, 12, 1, 11, 1, 11, 0, 6, 0, 3, 0, 9, 1, 7, 0, 0, 0)

View File

@ -1,35 +0,0 @@
[gd_scene load_steps=5 format=3 uid="uid://rdlvlyf0l1l"]
[ext_resource type="Script" path="res://Ball/ball.gd" id="1_oenn5"]
[sub_resource type="BoxShape3D" id="BoxShape3D_b4pqe"]
size = Vector3(0.5, 0.5, 0.5)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_tdblm"]
albedo_color = Color(0.117647, 0.427451, 0.611765, 1)
roughness = 0.42
[sub_resource type="SphereMesh" id="SphereMesh_mq1q1"]
radius = 0.3
height = 0.6
[node name="Ball" type="RigidBody3D"]
collision_layer = 4
custom_integrator = true
max_contacts_reported = 1
contact_monitor = true
can_sleep = false
lock_rotation = true
linear_damp_mode = 1
angular_damp_mode = 1
script = ExtResource("1_oenn5")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("BoxShape3D_b4pqe")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
material_override = SubResource("StandardMaterial3D_tdblm")
mesh = SubResource("SphereMesh_mq1q1")
[node name="TimerCollision" type="Timer" parent="."]
one_shot = true

View File

@ -1,36 +0,0 @@
[gd_scene load_steps=6 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"]
[sub_resource type="SphereShape3D" id="SphereShape3D_ogc07"]
radius = 0.3
[node name="Ball" type="RigidBody3D"]
collision_layer = 4
custom_integrator = true
max_contacts_reported = 1
contact_monitor = true
can_sleep = false
lock_rotation = true
linear_damp_mode = 1
angular_damp_mode = 1
script = ExtResource("1_oenn5")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("SphereShape3D_ogc07")
[node name="TimerCollision" type="Timer" parent="."]
one_shot = true
[node name="Meshes" type="Node3D" parent="."]
script = ExtResource("4_llldm")
[node name="BallBouncingMesh" parent="Meshes" instance=ExtResource("2_ccq7m")]
visible = false
[node name="BallFlyingMesh" parent="Meshes" instance=ExtResource("4_fh5j8")]
[node name="RotationNode" type="Node3D" parent="Meshes"]

View File

@ -1,40 +0,0 @@
[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="Shader" uid="uid://ny01m62ohegv" path="res://Ball/Meshes/ball_visual_shader.tres" id="2_wbu40"]
[ext_resource type="PackedScene" uid="uid://dn8tw0i6t3701" path="res://Ball/ball_mesh_bouncing.tscn" id="3_3pvgr"]
[sub_resource type="SphereShape3D" id="SphereShape3D_ogc07"]
radius = 0.3
[sub_resource type="ShaderMaterial" id="ShaderMaterial_qwhgq"]
render_priority = 0
shader = ExtResource("2_wbu40")
[sub_resource type="SphereMesh" id="SphereMesh_mq1q1"]
radius = 0.3
height = 0.6
[node name="Ball" type="RigidBody3D"]
collision_layer = 4
custom_integrator = true
max_contacts_reported = 1
contact_monitor = true
can_sleep = false
lock_rotation = true
linear_damp_mode = 1
angular_damp_mode = 1
script = ExtResource("1_oenn5")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
shape = SubResource("SphereShape3D_ogc07")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
visible = false
material_override = SubResource("ShaderMaterial_qwhgq")
mesh = SubResource("SphereMesh_mq1q1")
[node name="TimerCollision" type="Timer" parent="."]
one_shot = true
[node name="BallBouncingMesh" parent="." instance=ExtResource("3_3pvgr")]

View File

@ -9,7 +9,7 @@ var bounced_velocity : Vector3
var just_collided = false
var total_bounce_duration = 0.5
var gravity = Vector3(0, -10, 0)
var bounce_factor_cap = 0.25
func _physics_process(delta):
@ -18,8 +18,16 @@ func _physics_process(delta):
captured_velocity = linear_velocity
_get_collision(delta)
### DEBUG
# Delete balls that escape the level
if global_position.length() > 20.0:
print("ball deleted, total: ", Debug.get_total_ball_count())
queue_free()
var collision_position : Vector3
var collision_normal : Vector3
var collision_angle_factor : float
func _get_collision(delta):
@ -30,27 +38,37 @@ func _get_collision(delta):
# A collision happens
if collision:
# Get collision data stuff
collision_position = collision.get_position()
collision_normal = collision.get_normal()
bounced_velocity = captured_velocity.bounce(collision_normal)
# Get the dot product between the surface normal and the ball direction on hit
collision_angle_factor = collision_normal.dot(captured_velocity.normalized() * -1)
# Set the duration of the timer based on the collision angle
$TimerCollision.wait_time = total_bounce_duration * collision_angle_factor
$TimerCollision.start()
# Cap the factor for collision angles that are too shallow
if collision_angle_factor < bounce_factor_cap:
collision_angle_factor = 0
# Send the signal after getting the data to avoid having zeroed values
just_collided = true
emit_signal("ball_collision_started")
# Completely stop the ball in place
linear_velocity = Vector3.ZERO
freeze = true
# Move the ball again after the timer runs out
# The if check prevents the timeout function from trying to connect every frame
if not $TimerCollision.timeout.is_connected(_after_collision):
$TimerCollision.timeout.connect(_after_collision)
# Bounce back immediately if factor is 0
if collision_angle_factor == 0:
_after_collision()
# Do the sticky behaviour otherwise
else:
# Set the duration of the timer based on the collision angle
$TimerCollision.wait_time = total_bounce_duration * collision_angle_factor
$TimerCollision.start()
# Send the signal after getting the data to avoid having zeroed values
just_collided = true
emit_signal("ball_collision_started")
# Completely stop the ball in place
linear_velocity = Vector3.ZERO
freeze = true
# Move the ball again after the timer runs out
# The if check prevents the timeout function from trying to connect every frame
if not $TimerCollision.timeout.is_connected(_after_collision):
$TimerCollision.timeout.connect(_after_collision)
func _after_collision():
@ -60,3 +78,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,15 +1,17 @@
[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
[node name="Ball" type="RigidBody3D"]
[node name="Ball" type="RigidBody3D" groups=["Balls"]]
collision_layer = 4
custom_integrator = true
max_contacts_reported = 1
contact_monitor = true
can_sleep = false
@ -27,10 +29,17 @@ one_shot = true
[node name="Meshes" type="Node3D" parent="."]
script = ExtResource("4_llldm")
[node name="DirectionNode" type="Node3D" parent="Meshes"]
[node name="RotationNode" 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)
@ -21,16 +17,16 @@ func _ready():
func _process(delta):
_align_mesh_to_direction()
_align_flying_mesh_to_direction()
_squish_animation()
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)
func _align_flying_mesh_to_direction():
# 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)
@ -43,17 +39,30 @@ func _on_collision():
$BallBouncingMesh.visible = true
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)
_align_bouncing_mesh_to_surface()
func _after_collision():
$BallFlyingMesh.visible = true
$BallBouncingMesh.visible = false
is_bouncing = false
func _align_bouncing_mesh_to_surface():
# Align position to the surface
var bone_position = ball.collision_position - ball.global_position
skeleton_bouncing.set_bone_pose_position(1, bone_position)
# Reset the Meshes node rotation
rotation = Vector3.ZERO
# Align rotation to the surface normal
$RotationNode.look_at(ball.collision_normal + ball.global_position, Utilities.LOOK_AT_OFFSET_FIX)
# Convert the rotation euler into a quaternion
var euler_rotation = $RotationNode.rotation
var quaternion_rotation : Quaternion
quaternion_rotation = quaternion_rotation.from_euler(euler_rotation)
# Rotate the bone
skeleton_bouncing.set_bone_pose_rotation(1, quaternion_rotation)
func _squish_animation():

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

@ -1,62 +0,0 @@
[gd_scene load_steps=8 format=3 uid="uid://b06nx3ofndgjy"]
[ext_resource type="Texture2D" uid="uid://onahsd5he406" path="res://64x64_orange_grey.png" id="1_4apc2"]
[ext_resource type="PackedScene" uid="uid://ggl4tqcb5dbc" path="res://Player/player.tscn" id="1_n0nhi"]
[ext_resource type="Script" path="res://Misc/debug_cone.gd" id="3_nsiny"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_bwr1w"]
albedo_texture = ExtResource("1_4apc2")
uv1_scale = Vector3(0.5, 0.5, 0.5)
uv1_triplanar = true
texture_filter = 2
[sub_resource type="PlaneMesh" id="PlaneMesh_6jpek"]
size = Vector2(3, 8)
[sub_resource type="ConvexPolygonShape3D" id="ConvexPolygonShape3D_iauoa"]
points = PackedVector3Array(1.5, 0, 4, -1.5, 0, 4, 1.5, 0, -4, -1.5, 0, -4)
[sub_resource type="CylinderMesh" id="CylinderMesh_modx0"]
top_radius = 0.0
bottom_radius = 0.3
height = 0.6
[node name="World" type="Node3D"]
[node name="Level" type="Node3D" parent="."]
[node name="CSGBox3D" type="CSGBox3D" parent="Level"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 4, 0)
material_override = SubResource("StandardMaterial3D_bwr1w")
use_collision = true
size = Vector3(12, 8, 12)
[node name="CSGBox3D2" type="CSGBox3D" parent="Level/CSGBox3D"]
operation = 2
size = Vector3(11.8, 7.8, 11.8)
[node name="DiagonalWall" type="StaticBody3D" parent="Level"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -5, 0, -5)
[node name="MeshInstance3D" type="MeshInstance3D" parent="Level/DiagonalWall"]
transform = Transform3D(0.707107, 0.707107, -3.09086e-08, 0, -4.37114e-08, -1, -0.707107, 0.707107, -3.09086e-08, 0, 4, 0)
mesh = SubResource("PlaneMesh_6jpek")
[node name="CollisionShape3D" type="CollisionShape3D" parent="Level/DiagonalWall"]
transform = Transform3D(0.707107, 0.707107, -3.09086e-08, 0, -4.37114e-08, -1, -0.707107, 0.707107, -3.09086e-08, 0, 4, 0)
shape = SubResource("ConvexPolygonShape3D_iauoa")
[node name="Player" parent="." instance=ExtResource("1_n0nhi")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.2, 0)
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(0.482579, -0.637736, -0.600342, 0, -0.685437, 0.728132, -0.875852, -0.351381, -0.330778, 0, 0, 0)
[node name="DebugCone" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -4.5, 0.5, 4.5)
script = ExtResource("3_nsiny")
[node name="DebugConeMesh" type="MeshInstance3D" parent="DebugCone"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
mesh = SubResource("CylinderMesh_modx0")
skeleton = NodePath("../..")

View File

@ -15,6 +15,11 @@ run/main_scene="res://World/world.tscn"
config/features=PackedStringArray("4.1", "Forward Plus")
config/icon="res://icon.svg"
[autoload]
Utilities="*res://Autoload/Utilities.gd"
Debug="*res://Autoload/Debug.gd"
[input]
move_left={
@ -48,6 +53,16 @@ shoot={
, 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":69,"key_label":0,"unicode":101,"echo":false,"script":null)
]
}
debug_1={
"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":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]