#126: asteroids are only created off screen

The guard was a 420px bubble around the fleet, which is smaller than the
window - so a rock could and did materialise in plain sight, several hundred
pixels away but well inside the view. "Not on the fleet" was never the rule
the player notices; "not in view" is.

The keep-out area is now the visible world rect, grown by a margin so a rock
placed just outside does not drift into view on the frame it was created.
The old bubble survives as a FLOOR under it: what to use when the viewport
cannot be measured - no camera yet on a scene's first frame, or a headless
run with no window - and for a heavily zoomed-in view where the screen is
narrower than the distance a rock needs to be safe.

Candidates inside are pushed out through the nearest edge rather than
dropped, so the field keeps its density around the fleet instead of thinning
out exactly where the player is looking.

Measured: keep-out 2200x2200 over a 1920 screen, nearest asteroid 1899 units
out where it used to be allowed at 420.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Loïc Wisniewski 2026-08-24 20:57:55 +02:00
commit ddc3de595e
2 changed files with 100 additions and 38 deletions

View file

@ -7,10 +7,17 @@ extends Node2D
@export var check_interval: float = 1.0
@export var spawn_radius_chunks: int = 2
# No asteroid may be created within this distance of the fleet. At cruise
# speed this is well over a second of warning, so anything that reaches the
# fleet was visible on its way in.
# #126: no asteroid may be created anywhere the player can SEE. It used to be
# a fixed bubble around the fleet, which was smaller than the screen - so a
# rock could and did materialise in plain view, several hundred pixels from
# the fleet but well inside the window. The real rule is the visible rect,
# and this radius is now only the floor under it: what to use when the
# viewport cannot be measured (no camera yet on the very first frame of a
# scene, or a headless run with no window at all).
@export var safe_spawn_radius: float = 420.0
# Pushed this far past the edge of the visible rect, so a rock placed just
# outside does not drift into view on the same frame it was created.
@export var offscreen_margin: float = 140.0
@export var despawn_radius_chunks: int = 4
@export var asteroids_per_chunk: int = 6
@export var min_speed: float = 12.0
@ -67,31 +74,61 @@ func _chebyshev_chunk_distance(a: Vector2i, b: Vector2i) -> int:
return max(abs(a.x - b.x), abs(a.y - b.y))
func _safe_position(candidate: Vector2, centre: Vector2) -> Vector2:
# Nothing may materialise on top of the fleet. Chunks are populated around
# wherever the fleet happens to be - including the chunk it is standing in
# - so without this an asteroid could appear directly on a ship and
# destroy it with no chance to react. That is worst on the frame a scene
# loads: arriving back from a planet could cost a ship instantly.
if candidate.distance_to(centre) >= safe_spawn_radius:
# The area a new asteroid must stay out of: whatever is on screen, or the
# fallback bubble around the fleet when there is no screen to measure.
#
# Chunks are populated around wherever the fleet happens to be, including the
# chunk it is standing in, so without this a rock could appear directly on a
# ship - worst on the frame a scene loads, where arriving back from a planet
# could cost a vessel instantly. #126 widens that from "not on the fleet" to
# "not in view", which is the same rule the player actually notices.
func _keep_out_rect(centre: Vector2) -> Rect2:
var fallback := Rect2(centre - Vector2(safe_spawn_radius, safe_spawn_radius),
Vector2(safe_spawn_radius, safe_spawn_radius) * 2.0)
if get_viewport() == null:
return fallback
var visible_rect: Rect2 = ViewUtils.get_visible_world_rect(self)
if visible_rect.size.x < 1.0 or visible_rect.size.y < 1.0:
return fallback
visible_rect = visible_rect.grow(offscreen_margin)
# Never smaller than the old bubble: on a heavily zoomed-in view the
# screen can be narrower than the distance a rock needs to be safe.
return visible_rect.merge(fallback)
func _safe_position(candidate: Vector2, keep_out: Rect2) -> Vector2:
if not keep_out.has_point(candidate):
return candidate
# Push it out to the edge of the bubble rather than dropping it, so the
# field keeps its density near the fleet instead of thinning out.
# Pushed out through the nearest edge rather than dropped, so the field
# keeps its density around the fleet instead of thinning out where the
# player is looking.
var centre: Vector2 = keep_out.get_center()
var away: Vector2 = candidate - centre
if away.length() < 0.001:
if absf(away.x) < 0.001 and absf(away.y) < 0.001:
away = Vector2.RIGHT.rotated(randf() * TAU)
return centre + away.normalized() * safe_spawn_radius
var half: Vector2 = keep_out.size * 0.5
# The smallest scaling that puts the point on the boundary along `away`:
# whichever axis it would leave through first decides.
var scale_x: float = half.x / absf(away.x) if absf(away.x) > 0.001 else INF
var scale_y: float = half.y / absf(away.y) if absf(away.y) > 0.001 else INF
var factor: float = minf(scale_x, scale_y)
if is_inf(factor):
return centre + Vector2(half.x + 1.0, 0.0)
return centre + away * factor * 1.02
func _spawn_chunk_asteroids(chunk: Vector2i) -> void:
var origin: Vector2 = WorldGen.chunk_origin(chunk)
var spawned: Array = []
var fleet_centre: Vector2 = fleet.get_centroid()
# Measured once per chunk rather than per asteroid: it cannot change
# between two rocks placed in the same frame, and it is a viewport
# transform rather than a field read.
var keep_out: Rect2 = _keep_out_rect(fleet.get_centroid())
for i in range(asteroids_per_chunk):
var local_pos: Vector2 = Vector2(randf() * WorldGen.CHUNK_SIZE, randf() * WorldGen.CHUNK_SIZE)
var asteroid: Node2D = asteroid_scene.instantiate()
asteroid.position = _safe_position(origin + local_pos, fleet_centre)
asteroid.position = _safe_position(origin + local_pos, keep_out)
var angle: float = randf() * TAU
var speed: float = randf_range(min_speed, max_speed)
asteroid.velocity = Vector2(cos(angle), sin(angle)) * speed