A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(tilemap): tile-collision queries honor the cell's flip flags
esenginedc6c2b858ac2
diff --git a/sdk/src/tilemap/tileQuery.ts b/sdk/src/tilemap/tileQuery.tsindex 2ee841935..687ab4565 100644--- a/sdk/src/tilemap/tileQuery.ts+++ b/sdk/src/tilemap/tileQuery.ts@@ -13,7 +13,8 @@* collision data) returns null/false rather than throwing.*/import { TilemapAPI } from './tilemapAPI';-import { tileIdOf } from './tileBits';+import { tileIdOf, tileFlagsOf } from './tileBits';+import { oneWayNormalWorld } from './tiledLoader';import type { ResolvedTileCollision } from './tilesetResolve';import type { Entity } from '../types';@@ -34,6 +35,40 @@ export function _bindTileCollisionLookup(const PLAIN_BOX: ResolvedTileCollision = { shape: { type: 'box' } };+/**+ * Reorient a resolved tile collision by the cell's flip flags — the query twin of+ * the spawn path ({@link oneWayNormalWorld} + polygonLocalVerts), so a gameplay+ * query of a flipped collidable tile agrees with the collider physics actually+ * spawns. A box is flip-symmetric, so only polygon / circle geometry and a+ * one-way normal are transformed; the shared table entry is never mutated.+ */+function flipTileCollision(+ rc: ResolvedTileCollision, flipH: boolean, flipV: boolean, flipD: boolean,+): ResolvedTileCollision {+ // Normalized ([0,1], x-right / y-down) point flip, mirroring polygonLocalVerts.+ const flipPt = (sx: number, syDown: number): [number, number] => {+ let s = sx;+ let t = 1 - syDown; // to y-up+ if (flipV) t = 1 - t;+ if (flipH) s = 1 - s;+ if (flipD) { const tmp = s; s = t; t = tmp; }+ return [s, 1 - t]; // back to y-down+ };+ let shape = rc.shape;+ if (shape.type === 'polygon') {+ shape = { type: 'polygon', points: shape.points.map(([x, y]) => flipPt(x, y)) };+ } else if (shape.type === 'circle') {+ const [cx, cy] = flipPt(shape.cx, shape.cy);+ shape = { type: 'circle', cx, cy, r: shape.r };+ }+ const out: ResolvedTileCollision = { ...rc, shape };+ if (rc.oneWay) {+ const n = oneWayNormalWorld(rc.oneWay.nx, rc.oneWay.ny, flipH, flipV, flipD);+ out.oneWay = { nx: n.x + 0, ny: n.y + 0 }; // +0 normalizes a negated -0 to +0+ }+ return out;+}+/*** The resolved collision of the tile at grid cell (x, y) on `layer` — a plain* solid box, a rich shape (polygon / circle / one-way / sensor / material), or@@ -42,10 +77,16 @@ const PLAIN_BOX: ResolvedTileCollision = { shape: { type: 'box' } };export function tileCollisionAt(layer: Entity | number, x: number, y: number): ResolvedTileCollision | null {const table = lookup_?.(layer as number);if (!table) return null;- const id = tileIdOf(TilemapAPI.getTile(layer as Entity, x, y));+ const raw = TilemapAPI.getTile(layer as Entity, x, y);+ const id = tileIdOf(raw);if (id === 0) return null;- if (table.boxIds.has(id)) return PLAIN_BOX;- return table.shapes.get(id) ?? null;+ if (table.boxIds.has(id)) return PLAIN_BOX; // a box is flip-symmetric+ const rc = table.shapes.get(id);+ if (!rc) return null;+ // Apply the cell's flips so the queried shape / one-way normal matches the+ // collider the spawn path builds for the same flipped tile.+ const f = tileFlagsOf(raw);+ return (f.flipH || f.flipV || f.flipD) ? flipTileCollision(rc, f.flipH, f.flipV, f.flipD) : rc;}/**diff --git a/sdk/tests/tile-query.test.ts b/sdk/tests/tile-query.test.tsindex 8ac924498..282872d62 100644--- a/sdk/tests/tile-query.test.ts+++ b/sdk/tests/tile-query.test.ts@@ -6,6 +6,7 @@*/import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';import { tileCollisionAt, isTileSolid, _bindTileCollisionLookup, type LayerCollisionTable } from '../src/tilemap/tileQuery';+import type { ResolvedTileCollision } from '../src/tilemap/tilesetResolve';import { TilemapAPI } from '../src/tilemap/tilemapAPI';import { encodeTile } from '../src/tilemap/tileBits';@@ -47,6 +48,34 @@ describe('tileCollisionAt / isTileSolid', () => {expect(tileCollisionAt(0, 3, 0)?.oneWay).toEqual({ nx: 0, ny: 1 });});+ it('applies cell flips to a one-way normal, matching the spawned collider', () => {+ // Tile 4 is a solid-top one-way platform (normal {0,1}); a vertical flip must+ // put the solid side on the bottom (normal {0,-1}), as the spawn path does.+ grid.set('0,0', encodeTile(4, { flipH: false, flipV: true, flipD: false }));+ expect(tileCollisionAt(0, 0, 0)?.oneWay).toEqual({ nx: 0, ny: -1 });+ // A horizontal flip leaves a vertical normal unchanged.+ grid.set('1,0', encodeTile(4, { flipH: true, flipV: false, flipD: false }));+ expect(tileCollisionAt(0, 1, 0)?.oneWay).toEqual({ nx: 0, ny: 1 });+ // The shared table entry is not mutated by the flip.+ expect(TABLE.shapes.get(4)?.oneWay).toEqual({ nx: 0, ny: 1 });+ });++ it('flips polygon geometry and a circle centre in normalized space', () => {+ _bindTileCollisionLookup(() => ({+ boxIds: new Set<number>(),+ shapes: new Map<number, ResolvedTileCollision>([+ [5, { shape: { type: 'polygon', points: [[0, 1], [1, 1], [0, 0]] } }],+ [6, { shape: { type: 'circle', cx: 0.25, cy: 0.75, r: 0.2 } }],+ ]),+ }));+ // flipH mirrors x (y-down normalized): [0,1]→[1,1], [1,1]→[0,1], [0,0]→[1,0].+ grid.set('0,0', encodeTile(5, { flipH: true, flipV: false, flipD: false }));+ expect(tileCollisionAt(0, 0, 0)?.shape).toEqual({ type: 'polygon', points: [[1, 1], [0, 1], [1, 0]] });+ // circle centre (0.25,0.75) flipH → (0.75,0.75).+ grid.set('1,0', encodeTile(6, { flipH: true, flipV: false, flipD: false }));+ expect(tileCollisionAt(0, 1, 0)?.shape).toEqual({ type: 'circle', cx: 0.75, cy: 0.75, r: 0.2 });+ });+it('returns null/false when no lookup is bound or the layer has no table', () => {_bindTileCollisionLookup(null);expect(tileCollisionAt(0, 0, 0)).toBeNull();