Explorar o código

Prevent multi life removal on colinear collision

Was due to the same contact reported on sequencing frames.

The solution is to register a new collision only when the contact normal
is different enough from the previous one.
DricomDragon %!s(int64=3) %!d(string=hai) anos
pai
achega
57a15677cd
Modificáronse 1 ficheiros con 14 adicións e 3 borrados
  1. 14 3
      godot/scripts/Ball.gd

+ 14 - 3
godot/scripts/Ball.gd

@@ -10,6 +10,10 @@ signal kace_contact
 const brick_layer = 2
 const interrupt_speed = 700
 const cell_size = Vector2(20, 10)
+const redundant_ceil = 0.9 # 0.5 < x < 1.0
+
+
+var last_normal:Vector2 = Vector2(0.0, 0.0)
 
 
 func interrupt_ball():
@@ -23,12 +27,19 @@ func go_down_unstopped():
 	set_collision_mask_bit(brick_layer, false)
 
 
+func filter_new_contact(new_normal):
+	var is_new_contact = last_normal.dot(new_normal) < redundant_ceil
+	last_normal = new_normal
+	return is_new_contact
+
+
 func _integrate_forces(state :  Physics2DDirectBodyState):
 	for i in range(state.get_contact_count()):
-		var contact_pos = state.get_contact_local_position(i)
 		var contact_normal = state.get_contact_local_normal(i)
-		var correct_pos = contact_pos - contact_normal * cell_size
-		emit_signal("kace_contact", correct_pos)
+		if filter_new_contact(contact_normal):
+			var contact_pos = state.get_contact_local_position(i)
+			var correct_pos = contact_pos - contact_normal * cell_size
+			emit_signal("kace_contact", correct_pos)
 
 
 func _on_Launcher_interrupt_wave():