extends RefCounted const KIND := "directional_boost" const SPEED_GAIN := 5.0 # One latch per ball and block. A small separation margin tolerates contact jitter. var active: Dictionary = {} var events: Array = [] func integrate(state: PhysicsDirectBodyState3D, radius: float) -> void: var touching: Dictionary = {} for i in range(state.get_contact_count()): var collider: Node = state.get_contact_collider_object(i) if collider == null or collider.get_meta("piece_type", "") != KIND:continue var block: Node = collider while block != null and not block.has_meta("boost_bounds"):block = block.get_parent() if block == null:continue var contact: Vector3 = state.get_contact_collider_position(i) var normal := (state.transform.origin - contact).normalized() var local: Vector3 = block.to_local(contact) var bounds: AABB = block.get_meta("boost_bounds") if normal.y < .9 or absf(local.y - bounds.end.y) > .025:continue touching[block.get_instance_id()] = block for id in active.keys(): if touching.has(id):continue var block = active[id] if not is_instance_valid(block):active.erase(id);continue var local: Vector3 = block.to_local(state.transform.origin) var bounds: AABB = block.get_meta("boost_bounds") if local.x < bounds.position.x-.025 or local.x > bounds.end.x+.025 or local.z < bounds.position.z-.025 or local.z > bounds.end.z+.025 or absf(local.y-bounds.end.y-radius) > .06: active.erase(id) # Stable order for the rare case of simultaneously touching two top faces. var blocks: Array = touching.values() blocks.sort_custom(func(a,b):return str(a.get_meta("record_id",a.name)) < str(b.get_meta("record_id",b.name))) for block in blocks: var id: int = block.get_instance_id() if active.has(id):continue active[id] = block var before := state.linear_velocity var direction: Vector3 = block.global_basis * Vector3(block.get_meta("boost_direction")) direction.y = 0;direction = direction.normalized() var speed := Vector2(before.x,before.z).length() + SPEED_GAIN var after := direction * speed after.y = before.y state.linear_velocity = after events.append({"blockId":str(block.get_meta("record_id",block.name)),"before":before,"after":after,"tick":Engine.get_physics_frames()})