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:
parent
ef47daea6d
commit
e7cb1d907a
Binary file not shown.
|
After Width: | Height: | Size: 2.8 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 512 KiB |
|
|
@ -66,8 +66,19 @@ export default class Terrain {
|
|||
const length = Math.hypot(dx, dy);
|
||||
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 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 body = this.scene.matter.add.rectangle(midX, midY, length, TERRAIN_DEPTH, {
|
||||
|
|
|
|||
Loading…
Reference in New Issue