fix(terrain): align physics body with drawn terrain line

Correct the rectangle center offset calculation in Terrain to account
for both X and Y components perpendicular to each segment. Previously,
only the Y offset was applied, causing physics bodies to shift sideways
on sloped terrain. This fix resolves the bus visibly riding above or
through the ground on non-flat segments.

Also adds background and logo assets.
This commit is contained in:
Brian Fertig 2026-08-20 13:16:57 -06:00
parent ef47daea6d
commit e7cb1d907a
3 changed files with 12 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 MiB

BIN
assets/backgrounds/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 512 KiB

View File

@ -66,8 +66,19 @@ export default class Terrain {
const length = Math.hypot(dx, dy); const length = Math.hypot(dx, dy);
if (length < 1) continue; if (length < 1) continue;
// The rectangle's top edge needs to sit exactly on the a-b line, which
// means offsetting its center from the segment's own midpoint by
// TERRAIN_DEPTH/2 in the direction PERPENDICULAR to the segment - both
// an X and a Y component (-sin/cos of the segment's angle), not just
// straight down in world Y. Omitting the X term (as a prior version of
// this did) left every non-flat segment's physics body shifted
// sideways from its drawn line by TERRAIN_DEPTH/2 * sin(angle) - with
// TERRAIN_DEPTH deliberately huge (deep enough nothing ever tunnels
// through the bottom), that shift is large even at modest slopes,
// which is exactly what made the bus visibly ride above/through the
// drawn ground on anything but flat terrain.
const angle = Math.atan2(dy, dx); const angle = Math.atan2(dy, dx);
const midX = (a.x + b.x) / 2; const midX = (a.x + b.x) / 2 - (TERRAIN_DEPTH / 2) * Math.sin(angle);
const midY = (a.y + b.y) / 2 + (TERRAIN_DEPTH / 2) * Math.cos(angle); const midY = (a.y + b.y) / 2 + (TERRAIN_DEPTH / 2) * Math.cos(angle);
const body = this.scene.matter.add.rectangle(midX, midY, length, TERRAIN_DEPTH, { const body = this.scene.matter.add.rectangle(midX, midY, length, TERRAIN_DEPTH, {