garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/adr/029-geo-anchored-prim-storage.md

raw

ADR-029: Geo-Anchored Prim Storage


Metadata:


Context

Problem: Prim positions are stored and transmitted as a single absolute f32 Vec3 in "sim-space" metres measured from the world origin (0,0,0), at every layer — the Prim component's Bevy Transform, PrimDto.position on the wire, the position_x/y/z REAL columns read back into f32, and both SQLite databases (server and offline client). Prims are spawned with only (Prim, Transform) — no big_space GridCell, no region parenting (rendering.rs::spawn_prims) — so they render at grid cell (0,0,0) and their world position is the raw f32.

This does not fit the world-size coordinate system the client now uses:

  1. Precision. The world frame is a big_space i64 grid of 10 km cells with an f32 local Transform inside each cell (ADR-019), giving ~mm precision anywhere on the planet. A lone absolute f32 holds only ~7 significant digits, so a prim more than ~10–100 km from the origin degrades to decimetre/metre precision. Regions already dodge this — spawn_regions runs Grid::translation_to_grid and spawns them with a GridCell + ChildOf(root) — but prims do not.

  2. Reference frame. The world is no longer pinned to a fixed sim origin. Since the movable-OSM-anchor model (ADR-006 → ADR-020) and the planetary globe (ADR-025), the ground truth is a re-anchorable Web-Mercator frame whose origin is wherever the anchor currently sits: MapStream::geo_anchor() -> (lat, lng, zoom), and lat_lng_to_world(anchor, clat, clng) == (0, 0). A prim stored as absolute metres-from-origin has no tie to a lat/lng, so the moment the anchor moves — the entire point of the anchor-less direction — every prim's world position silently refers to a different place on Earth, with no stored geo coordinate to re-project it from.

Requirements:

Decision

Store each prim's horizontal placement as an absolute geographic coordinate (GeoAnchor { latitude, longitude }, f64) plus an altitude carried in position.y; derive the render-space GridCell + local Transform on the client from the current OSM anchor, and re-derive it whenever the anchor moves.

Prims are online-only and geo-anchored: the server is the sole authority for prim state, and every prim carries an absolute GeoAnchor.

Approach:

f64 lat/lng gives sub-millimetre resolution globally; the f32 local Transform stays cell-local (≤10 km) so it keeps mm precision — the same guarantee regions already have.

Rationale

Primary Reasoning:

  1. Geo is the anchor-independent invariant. Two clients with different anchors each convert their own local frame to the same lat/lng, and a re-anchor is a pure re-projection of an unchanged stored coordinate. Nothing else in the system survives an anchor move — this does.
  2. It matches how regions already work. Regions carry latitude/longitude and are rebased into the grid via translation_to_grid; this makes prims first-class citizens of the same anchored frame instead of an exception that only renders correctly while everything sits near the origin.
  3. Precision follows from cell-locality, not bigger floats. Keeping the f32 Transform cell-relative (via GridCell) is what ADR-019 already relies on; widening prim coordinates to f64 everywhere would fight big_space instead of using it.
  4. Decouples prims from region lifetime. The anchor-less direction expects fixed sim regions to go away; an absolute geo coordinate needs no region to be placed.

Alternatives Considered:

AlternativeProsConsRejected Because
Parent prims to their Region entity (ChildOf(region)), keep f32 region-relativeSmallest change; no protocol/schema churn; reuses region GridCellTies prims to region lifetime; still bound to the fixed-origin frame; breaks when regions are dropped for the movable anchorContradicts the anchor-less direction (ADR-020); doesn't survive re-anchor on its own
Widen position to f64 DVec3 absolute metresConceptually simple; planet-wide precisionStill frameless — absolute from which origin? A re-anchor still invalidates it; doubles wire/storage width; duplicates what big_space already does with i64 cellsSolves precision but not the reference-frame problem, which is the real blocker
Store GridCell (i64×3) + f32 local on the wireDirectly matches the render model; mm precisionCell indices are meaningless across a re-anchor (they're relative to the current origin); leaks a client rendering concept into the persisted authorityNot anchor-independent; couples storage to the floating-origin implementation

Consequences

Positive:

Negative:

Related