added a cap to the collision angle factor so the ball doesn't bounce at very shallow angles

This commit is contained in:
AmyLillya 2023-09-03 12:11:48 -03:00
parent 35f17991e8
commit 22f17fdbff
2 changed files with 25 additions and 16 deletions

View File

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

View File

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