From 22f17fdbff12660cb27afdd7bd1efa2f63447078 Mon Sep 17 00:00:00 2001 From: AmyLillya Date: Sun, 3 Sep 2023 12:11:48 -0300 Subject: [PATCH] added a cap to the collision angle factor so the ball doesn't bounce at very shallow angles --- Ball/ball.gd | 40 ++++++++++++++++++++++++---------------- Ball/ball.tscn | 1 + 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/Ball/ball.gd b/Ball/ball.gd index 85968f0..ae34fe3 100644 --- a/Ball/ball.gd +++ b/Ball/ball.gd @@ -9,7 +9,6 @@ var bounced_velocity : Vector3 var just_collided = false var total_bounce_duration = 0.5 -var gravity = Vector3(0, -10, 0) func _physics_process(delta): @@ -35,22 +34,31 @@ func _get_collision(delta): # 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 < 0.25: + 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(): diff --git a/Ball/ball.tscn b/Ball/ball.tscn index 5c71a40..24fd9fe 100644 --- a/Ball/ball.tscn +++ b/Ball/ball.tscn @@ -10,6 +10,7 @@ radius = 0.3 [node name="Ball" type="RigidBody3D"] collision_layer = 4 +custom_integrator = true max_contacts_reported = 1 contact_monitor = true can_sleep = false