ecspresso 0.16.1 → 0.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/plugins/ai/detection.js +2 -2
- package/dist/plugins/ai/detection.js.map +3 -3
- package/dist/plugins/ai/flocking.js +2 -2
- package/dist/plugins/ai/flocking.js.map +7 -6
- package/dist/plugins/physics/collision.js +2 -2
- package/dist/plugins/physics/collision.js.map +6 -5
- package/dist/plugins/physics/collision3D.js +2 -2
- package/dist/plugins/physics/collision3D.js.map +8 -7
- package/dist/plugins/physics/physics2D.js +2 -2
- package/dist/plugins/physics/physics2D.js.map +6 -5
- package/dist/plugins/physics/physics3D.js +2 -2
- package/dist/plugins/physics/physics3D.js.map +6 -5
- package/dist/plugins/spatial/spatial-index.js +2 -2
- package/dist/plugins/spatial/spatial-index.js.map +4 -4
- package/dist/plugins/spatial/spatial-index3D.js +2 -2
- package/dist/plugins/spatial/spatial-index3D.js.map +4 -4
- package/dist/query-cache.d.ts +7 -7
- package/dist/utils/layer-bit-registry.d.ts +17 -0
- package/dist/utils/narrowphase.d.ts +31 -6
- package/dist/utils/narrowphase3D.d.ts +10 -0
- package/dist/utils/spatial-hash.d.ts +49 -14
- package/dist/utils/spatial-hash3D.d.ts +49 -14
- package/package.json +1 -1
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/plugins/physics/collision.ts", "../src/utils/narrowphase.ts", "../src/plugins/physics/collision3D.ts", "../src/utils/narrowphase3D.ts"],
|
|
3
|
+
"sources": ["../src/plugins/physics/collision.ts", "../src/utils/layer-bit-registry.ts", "../src/utils/narrowphase.ts", "../src/plugins/physics/collision3D.ts", "../src/utils/narrowphase3D.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"/**\n * Collision Plugin for ECSpresso\n *\n * Provides layer-based collision detection with events.\n * Uses worldTransform for position (world-space collision).\n * Supports AABB and circle colliders.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { TransformWorldConfig } from '../spatial/transform';\nimport { fillBaseColliderInfo, detectCollisions, AABB_SHAPE, type Contact, type BaseColliderInfo } from '../../utils/narrowphase';\nimport type { SpatialIndex } from '../../utils/spatial-hash';\n\n// ==================== Component Types ====================\n\n/**\n * Axis-Aligned Bounding Box collider.\n */\nexport interface AABBCollider {\n\t/** Width of the bounding box */\n\twidth: number;\n\t/** Height of the bounding box */\n\theight: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Circle collider.\n */\nexport interface CircleCollider {\n\t/** Radius of the circle */\n\tradius: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Collision layer configuration.\n */\nexport interface CollisionLayer<L extends string = never> {\n\t/** The layer this entity belongs to */\n\tlayer: L;\n\t/** Layers this entity can collide with */\n\tcollidesWith: readonly L[];\n}\n\n/**\n * Component types provided by the collision plugin.\n * Included automatically via `.withPlugin(createCollisionPlugin())`.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createCollisionPlugin())\n * .withComponentTypes<{ sprite: Sprite; enemy: boolean }>()\n * .build();\n * ```\n */\nexport interface CollisionComponentTypes<L extends string = never> {\n\taabbCollider: AABBCollider;\n\tcircleCollider: CircleCollider;\n\tcollisionLayer: CollisionLayer<L>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when two entities collide.\n *\n * Normal components are flattened (`normalX`/`normalY`) rather than nested\n * in a sub-object to avoid a per-event allocation in the collision hot path.\n */\nexport interface CollisionEvent<L extends string = never> {\n\t/** First entity in the collision */\n\tentityA: number;\n\t/** Second entity in the collision */\n\tentityB: number;\n\t/** Layer of the first entity */\n\tlayerA: L;\n\t/** Layer of the second entity */\n\tlayerB: L;\n\t/** Contact normal X, pointing from entityA toward entityB */\n\tnormalX: number;\n\t/** Contact normal Y, pointing from entityA toward entityB */\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Event types provided by the collision plugin.\n */\nexport interface CollisionEventTypes<L extends string = never> {\n\tcollision: CollisionEvent<L>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the collision plugin.\n */\nexport interface CollisionPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {\n\t/** Name of the collision event (default: 'collision') */\n\tcollisionEventName?: string;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create an AABB collider component.\n *\n * @param width Width of the bounding box\n * @param height Height of the bounding box\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * });\n * ```\n */\nexport function createAABBCollider(\n\twidth: number,\n\theight: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { aabbCollider: AABBCollider } {\n\tconst collider: AABBCollider = { width, height };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { aabbCollider: collider };\n}\n\n/**\n * Create a circle collider component.\n *\n * @param radius Radius of the circle\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createCircleCollider(25),\n * });\n * ```\n */\nexport function createCircleCollider(\n\tradius: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { circleCollider: CircleCollider } {\n\tconst collider: CircleCollider = { radius };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { circleCollider: collider };\n}\n\n/**\n * Create a collision layer component.\n *\n * @param layer The layer this entity belongs to\n * @param collidesWith Layers this entity can collide with\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...createCollisionLayer('player', ['enemy', 'obstacle']),\n * });\n * ```\n */\nexport function createCollisionLayer<L extends string>(\n\tlayer: L,\n\tcollidesWith: readonly L[]\n): Pick<CollisionComponentTypes<L>, 'collisionLayer'> {\n\treturn {\n\t\tcollisionLayer: { layer, collidesWith },\n\t};\n}\n\n/**\n * Layer factory result from defineCollisionLayers.\n */\nexport type LayerFactories<T extends Record<string, readonly string[]>> = {\n\t[K in keyof T]: () => Pick<CollisionComponentTypes<Extract<keyof T, string>>, 'collisionLayer'>;\n};\n\n/**\n * Extract layer names from a `defineCollisionLayers` result for use with\n * `createCollisionPairHandler`'s `L` type parameter.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n * ```\n */\nexport type LayersOf<T> = Extract<keyof T, string>;\n\n/**\n * Define collision layer relationships and get factory functions.\n *\n * @param rules Object mapping layer names to arrays of layers they collide with\n * @returns Object with factory functions for each layer\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({\n * player: ['enemy', 'enemyProjectile'],\n * playerProjectile: ['enemy'],\n * enemy: ['playerProjectile'],\n * enemyProjectile: ['player'],\n * });\n *\n * // Usage\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\n/**\n * Validates that all `collidesWith` values reference actual layer keys.\n * Catches typos at compile time.\n */\ntype ValidateCollidesWith<T> = {\n\t[K in keyof T]: T[K] extends readonly (infer V)[]\n\t\t? [V] extends [Extract<keyof T, string>] ? T[K] : readonly Extract<keyof T, string>[]\n\t\t: never;\n};\n\nexport function defineCollisionLayers<const T extends Record<string, readonly string[]>>(\n\trules: T & ValidateCollidesWith<T>\n): LayerFactories<T> {\n\ttype L = Extract<keyof T, string>;\n\tconst factories = {} as LayerFactories<T>;\n\n\tfor (const layer of Object.keys(rules) as Array<L>) {\n\t\tconst collidesWith = rules[layer] as readonly L[];\n\t\tfactories[layer] = () => createCollisionLayer<L>(layer, collidesWith);\n\t}\n\n\treturn factories;\n}\n\n// ==================== Collision Pair Handler ====================\n\n/**\n * Callback for a collision pair handler.\n *\n * @param firstEntityId Entity belonging to the first layer in the pair key\n * @param secondEntityId Entity belonging to the second layer in the pair key\n * @param ecs The ECS world instance (passed through from the subscriber)\n */\nexport type CollisionPairCallback<W = unknown> = (\n\tfirstEntityId: number,\n\tsecondEntityId: number,\n\tecs: W,\n) => void;\n\ninterface PairEntry<W> {\n\tcallback: CollisionPairCallback<W>;\n\tswapped: boolean;\n}\n\nfunction parsePairKey(key: string): [string, string] {\n\tconst colonIndex = key.indexOf(':');\n\tif (colonIndex === -1) {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": must contain a colon separator (e.g. \"player:enemy\")`);\n\t}\n\tconst layerA = key.slice(0, colonIndex);\n\tconst layerB = key.slice(colonIndex + 1);\n\tif (layerA === '' || layerB === '') {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": layer names must not be empty`);\n\t}\n\treturn [layerA, layerB];\n}\n\n/**\n * Create a collision pair handler that routes collision events to\n * layer-pair-specific callbacks.\n *\n * Registering `\"a:b\"` automatically handles both `(layerA=a, layerB=b)` and\n * `(layerA=b, layerB=a)`. Entity arguments are swapped to match the declared\n * key order. If both `\"a:b\"` and `\"b:a\"` are explicitly registered, each gets\n * its own handler with no implicit reverse.\n *\n * @typeParam W - The ECS world type (e.g. `ECSpresso<C, E, R>`). Defaults to `unknown`.\n * @typeParam L - Union of valid layer names. Defaults to `string`.\n * Provide specific layer names for compile-time key validation:\n * `createCollisionPairHandler<ECS, keyof typeof layers>({...})`\n *\n * @param pairs Object mapping `\"layerA:layerB\"` keys to callbacks\n * @returns A dispatch function to call with collision event data and ECS instance\n *\n * @example\n * ```typescript\n * // Basic usage:\n * const handler = createCollisionPairHandler<ECS>({\n * 'playerProjectile:enemy': (projectileId, enemyId, ecs) => {\n * ecs.commands.removeEntity(projectileId);\n * },\n * });\n *\n * // With layer name validation:\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n *\n * ecs.eventBus.subscribe('collision', (data) => handler({ data, ecs }));\n * ```\n */\nexport function createCollisionPairHandler<W = unknown, L extends string = string>(\n\tpairs: { [K in `${L}:${L}`]?: CollisionPairCallback<W> }\n): (ctx: { data: CollisionEvent<L>; ecs: W }) => void;\nexport function createCollisionPairHandler<W = unknown>(\n\tpairs: Record<string, CollisionPairCallback<W> | undefined>\n): (ctx: { data: CollisionEvent<string>; ecs: W }) => void {\n\tconst lookup = new Map<string, PairEntry<W>>();\n\tconst explicitKeys = new Set<string>();\n\n\t// First pass: collect all explicit keys\n\tfor (const key of Object.keys(pairs)) {\n\t\tparsePairKey(key); // validate\n\t\texplicitKeys.add(key);\n\t}\n\n\t// Second pass: build lookup with forward + conditional reverse entries\n\tfor (const key of Object.keys(pairs)) {\n\t\tconst [layerA, layerB] = parsePairKey(key);\n\t\tconst callback = pairs[key];\n\t\tif (!callback) continue;\n\n\t\t// Forward entry\n\t\tlookup.set(key, { callback, swapped: false });\n\n\t\t// Reverse entry (only if the reverse key wasn't explicitly registered\n\t\t// and it's not a self-collision where forward === reverse)\n\t\tconst reverseKey = `${layerB}:${layerA}`;\n\t\tif (reverseKey !== key && !explicitKeys.has(reverseKey)) {\n\t\t\tlookup.set(reverseKey, { callback, swapped: true });\n\t\t}\n\t}\n\n\treturn function collisionPairDispatch({ data: event, ecs }: { data: CollisionEvent<string>; ecs: W }): void {\n\t\tconst entry = lookup.get(event.layerA + ':' + event.layerB);\n\t\tif (!entry) return;\n\n\t\tif (entry.swapped) {\n\t\t\tentry.callback(event.entityB, event.entityA, ecs);\n\t\t} else {\n\t\t\tentry.callback(event.entityA, event.entityB, ecs);\n\t\t}\n\t};\n}\n\n// ==================== Dependency Types ====================\n\n// ==================== Module-level Collision Callback ====================\n\ninterface CollisionEventBus<L extends string> {\n\tpublish(event: 'collision', data: CollisionEvent<L>): void;\n}\n\n/**\n * Module-level reusable collision event. Subscribers must consume\n * synchronously — same contract as the shared narrowphase Contact.\n */\nconst _collisionEvent: CollisionEvent<string> = {\n\tentityA: 0, entityB: 0, layerA: '', layerB: '',\n\tnormalX: 0, normalY: 0, depth: 0,\n};\n\nfunction onCollisionDetected<L extends string>(\n\ta: BaseColliderInfo<L>,\n\tb: BaseColliderInfo<L>,\n\tcontact: Contact,\n\teventBus: CollisionEventBus<L>,\n): void {\n\t_collisionEvent.entityA = a.entityId;\n\t_collisionEvent.entityB = b.entityId;\n\t_collisionEvent.layerA = a.layer;\n\t_collisionEvent.layerB = b.layer;\n\t_collisionEvent.normalX = contact.normalX;\n\t_collisionEvent.normalY = contact.normalY;\n\t_collisionEvent.depth = contact.depth;\n\teventBus.publish('collision', _collisionEvent as CollisionEvent<L>);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a collision plugin for ECSpresso.\n *\n * This plugin provides:\n * - Collision detection between entities with colliders\n * - AABB-AABB, circle-circle, and AABB-circle collision\n * - Layer-based filtering for collision pairs\n * - Deduplication of A-B / B-A collisions\n * - Automatic broadphase acceleration when spatialIndex resource is present\n *\n * Uses worldTransform for position (world-space collision detection).\n * The `layers` parameter is required for type inference — at runtime the\n * plugin does not consume it.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * const ecs = ECSpresso\n * .create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createCollisionPlugin({ layers }))\n * .build();\n *\n * // Entity with collision\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\nexport function createCollisionPlugin<L extends string, G extends string = 'physics'>(\n\toptions: CollisionPluginOptions<G> & { layers: LayerFactories<Record<L, readonly string[]>> }\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 0,\n\t\tphase = 'postUpdate',\n\t} = options;\n\n\treturn definePlugin('collision')\n\t\t.withComponentTypes<CollisionComponentTypes<L>>()\n\t\t.withEventTypes<CollisionEventTypes<L>>()\n\t\t.withLabels<'collision-detection'>()\n\t\t.withGroups<G>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Grow-only pool of BaseColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: BaseColliderInfo<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, BaseColliderInfo<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tworld\n\t\t\t\t.addSystem('collision-detection')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['worldTransform', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t// TODO(perf): collider shape is discovered via two ecs.getComponent\n\t\t\t\t\t// calls per entity per frame because the query can't express\n\t\t\t\t\t// \"aabbCollider OR circleCollider\". Splitting into two queries\n\t\t\t\t\t// (aabb-bearing, circle-bearing) would eliminate these lookups at\n\t\t\t\t\t// the cost of two pool-fill passes. Keep in sync with collision3D.\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { worldTransform, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabbCollider');\n\t\t\t\t\t\tconst circle = aabb ? undefined : ecs.getComponent(entity.id, 'circleCollider');\n\t\t\t\t\t\tif (!aabb && !circle) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: worldTransform.x,\n\t\t\t\t\t\t\t\ty: worldTransform.y,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tshape: AABB_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, worldTransform.x, worldTransform.y,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, circle,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex>('spatialIndex');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions(colliderPool, count, broadphaseMap, cachedSI, onCollisionDetected<L>, ecs.eventBus);\n\t\t\t\t});\n\t\t});\n}\n\n",
|
|
6
|
-
"/**\n * Shared Narrowphase Module\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline used by both the collision plugin (event-only) and\n * the physics2D plugin (impulse response).\n */\n\nimport type { SpatialIndex } from './spatial-hash';\n\n// ==================== Contact ====================\n\n/**\n * Contact result from a narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact {\n\tnormalX: number;\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact passed down from `detectCollisions` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact = { normalX: 0, normalY: 0, depth: 0 };\n\n// ==================== BaseColliderInfo ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo layout. */\nexport const AABB_SHAPE = 0;\nexport const CIRCLE_SHAPE = 1;\nexport type ColliderShape = typeof AABB_SHAPE | typeof CIRCLE_SHAPE;\n\n/**\n * Minimum collider data shared by collision and physics bundles.\n *\n * Flat layout (no nested `aabb` / `circle` sub-objects): the `shape`\n * discriminator tells you whether to read `halfWidth`/`halfHeight`\n * (AABB) or `radius` (Circle). Unused fields are set to 0.\n *\n * This shape is pool-friendly — all fields are assigned in place each\n * frame without allocating nested objects.\n */\nexport interface BaseColliderInfo<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\tshape: ColliderShape;\n\thalfWidth: number;\n\thalfHeight: number;\n\tradius: number;\n}\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB and circle colliders, AABB wins and only\n * the AABB offset is applied. This matches the dispatch precedence in\n * `computeContact`; the previous implementation stacked both offsets,\n * which was a bug.\n */\nexport function fillBaseColliderInfo<L extends string>(\n\tinfo: BaseColliderInfo<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb: { width: number; height: number; offsetX?: number; offsetY?: number } | undefined,\n\tcircle: { radius: number; offsetX?: number; offsetY?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\n\tif (aabb) {\n\t\tinfo.x = x + (aabb.offsetX ?? 0);\n\t\tinfo.y = y + (aabb.offsetY ?? 0);\n\t\tinfo.shape = AABB_SHAPE;\n\t\tinfo.halfWidth = aabb.width / 2;\n\t\tinfo.halfHeight = aabb.height / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (circle) {\n\t\tinfo.x = x + (circle.offsetX ?? 0);\n\t\tinfo.y = y + (circle.offsetY ?? 0);\n\t\tinfo.shape = CIRCLE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.radius = circle.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex | undefined {\n\treturn tryGetResource<SpatialIndex>('spatialIndex');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB-AABB contact into `out`. Returns `true` if the shapes\n * overlap (out was filled), `false` otherwise.\n */\nexport function computeAABBvsAABB(\n\tax: number, ay: number, ahw: number, ahh: number,\n\tbx: number, by: number, bhw: number, bhh: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\n\tif (overlapX <= 0 || overlapY <= 0) return false;\n\n\tif (overlapX < overlapY) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\tout.normalX = 0;\n\tout.normalY = dy >= 0 ? 1 : -1;\n\tout.depth = overlapY;\n\treturn true;\n}\n\nexport function computeCircleVsCircle(\n\tax: number, ay: number, ar: number,\n\tbx: number, by: number, br: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst distSq = dx * dx + dy * dy;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\nexport function computeAABBvsCircle(\n\taabbX: number, aabbY: number, ahw: number, ahh: number,\n\tcircleX: number, circleY: number, radius: number,\n\tout: Contact,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(circleX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(circleY, aabbY + ahh));\n\n\tconst dx = circleX - closestX;\n\tconst dy = circleY - closestY;\n\tconst distSq = dx * dx + dy * dy;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Circle center inside AABB\n\tif (distSq === 0) {\n\t\tconst pushLeft = (circleX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - circleX);\n\t\tconst pushUp = (circleY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - circleY);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = -1; out.depth = pushUp + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact(a: BaseColliderInfo, b: BaseColliderInfo, out: Contact): boolean {\n\tif (a.shape === AABB_SHAPE && b.shape === AABB_SHAPE) {\n\t\treturn computeAABBvsAABB(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === CIRCLE_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeCircleVsCircle(\n\t\t\ta.x, a.y, a.radius,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeAABBvsCircle(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Circle, b is AABB — compute as AABB-vs-Circle, then flip normal in place\n\tif (!computeAABBvsCircle(\n\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\ta.x, a.y, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable set for broadphase candidates. */\nconst _broadphaseCandidates = new Set<number>();\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame. Unused by the brute-force path but still required so that\n * typed reuse is the default, not an opt-in.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex | undefined,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] Collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB_SHAPE ? a.halfHeight : a.radius;\n\n\t\t_broadphaseCandidates.clear();\n\t\tspatialIndex.queryRectInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH,\n\t\t\ta.x + aHalfW, a.y + aHalfH,\n\t\t\t_broadphaseCandidates,\n\t\t);\n\n\t\t// TODO(perf): mirrors narrowphase3D — dense grids add every candidate\n\t\t// (including `a` itself and lower-ID entities) to the set before the\n\t\t// filter below discards ~half of them. Emitting only pairs with larger\n\t\t// IDs at query time would remove the post-hoc filter. Keep in sync with\n\t\t// whatever fix lands in narrowphase3D.\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tif (bId <= a.entityId) continue;\n\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n",
|
|
7
|
-
"/**\n * Collision 3D Plugin for ECSpresso\n *\n * Provides layer-based 3D collision detection with events.\n * Uses worldTransform3D for position (world-space collision).\n * Supports AABB3D and sphere colliders.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { Transform3DWorldConfig } from '../spatial/transform3D';\nimport {\n\tfillBaseColliderInfo3D,\n\tdetectCollisions3D,\n\tAABB3D_SHAPE,\n\ttype Contact3D,\n\ttype BaseColliderInfo3D,\n} from '../../utils/narrowphase3D';\nimport type { SpatialIndex3D } from '../../utils/spatial-hash3D';\nimport {\n\tdefineCollisionLayers,\n\tcreateCollisionPairHandler,\n\tcreateCollisionLayer,\n\ttype CollisionLayer,\n\ttype LayerFactories,\n\ttype LayersOf,\n\ttype CollisionPairCallback,\n} from './collision';\n\n// Re-export dimension-agnostic layer utilities so consumers only need one import\nexport { defineCollisionLayers, createCollisionPairHandler, createCollisionLayer };\nexport type { CollisionLayer, LayerFactories, LayersOf, CollisionPairCallback };\n\n// Re-export collider shapes (defined in spatial-index3D to avoid duplication)\nexport type { AABB3DCollider, SphereCollider } from '../spatial/spatial-index3D';\nimport type { AABB3DCollider, SphereCollider } from '../spatial/spatial-index3D';\n\n// ==================== Component Types ====================\n\n/**\n * Component types provided by the collision3D plugin.\n */\nexport interface Collision3DComponentTypes<L extends string = never> {\n\taabb3DCollider: AABB3DCollider;\n\tsphereCollider: SphereCollider;\n\tcollisionLayer: CollisionLayer<L>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when two 3D entities collide.\n *\n * Normal components are flattened to avoid per-event allocation in the hot path.\n */\nexport interface Collision3DEvent<L extends string = never> {\n\tentityA: number;\n\tentityB: number;\n\tlayerA: L;\n\tlayerB: L;\n\t/** Contact normal X, pointing from entityA toward entityB */\n\tnormalX: number;\n\t/** Contact normal Y, pointing from entityA toward entityB */\n\tnormalY: number;\n\t/** Contact normal Z, pointing from entityA toward entityB */\n\tnormalZ: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Event types provided by the collision3D plugin.\n */\nexport interface Collision3DEventTypes<L extends string = never> {\n\tcollision3D: Collision3DEvent<L>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the collision3D plugin.\n */\nexport interface Collision3DPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {}\n\n// ==================== Helper Functions ====================\n\nexport function createAABB3DCollider(\n\twidth: number,\n\theight: number,\n\tdepth: number,\n\toffsetX?: number,\n\toffsetY?: number,\n\toffsetZ?: number,\n): { aabb3DCollider: AABB3DCollider } {\n\tconst collider: AABB3DCollider = { width, height, depth };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\tif (offsetZ !== undefined) collider.offsetZ = offsetZ;\n\treturn { aabb3DCollider: collider };\n}\n\nexport function createSphereCollider(\n\tradius: number,\n\toffsetX?: number,\n\toffsetY?: number,\n\toffsetZ?: number,\n): { sphereCollider: SphereCollider } {\n\tconst collider: SphereCollider = { radius };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\tif (offsetZ !== undefined) collider.offsetZ = offsetZ;\n\treturn { sphereCollider: collider };\n}\n\n// ==================== Module-level Collision Callback ====================\n\ninterface Collision3DEventBus<L extends string> {\n\tpublish(event: 'collision3D', data: Collision3DEvent<L>): void;\n}\n\n/**\n * Module-level reusable collision event. Subscribers must consume synchronously —\n * same contract as the shared narrowphase Contact3D.\n */\nconst _collisionEvent: Collision3DEvent<string> = {\n\tentityA: 0, entityB: 0, layerA: '', layerB: '',\n\tnormalX: 0, normalY: 0, normalZ: 0, depth: 0,\n};\n\nfunction onCollisionDetected3D<L extends string>(\n\ta: BaseColliderInfo3D<L>,\n\tb: BaseColliderInfo3D<L>,\n\tcontact: Contact3D,\n\teventBus: Collision3DEventBus<L>,\n): void {\n\t_collisionEvent.entityA = a.entityId;\n\t_collisionEvent.entityB = b.entityId;\n\t_collisionEvent.layerA = a.layer;\n\t_collisionEvent.layerB = b.layer;\n\t_collisionEvent.normalX = contact.normalX;\n\t_collisionEvent.normalY = contact.normalY;\n\t_collisionEvent.normalZ = contact.normalZ;\n\t_collisionEvent.depth = contact.depth;\n\teventBus.publish('collision3D', _collisionEvent as Collision3DEvent<L>);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a 3D collision plugin for ECSpresso.\n *\n * Provides layer-based collision detection between entities with 3D colliders,\n * publishing `collision3D` events on contact. Supports AABB3D-AABB3D,\n * sphere-sphere, and AABB3D-sphere tests. Automatically uses the\n * `spatialIndex3D` resource for broadphase when present.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * const ecs = ECSpresso\n * .create()\n * .withPlugin(createTransform3DPlugin())\n * .withPlugin(createCollision3DPlugin({ layers }))\n * .build();\n *\n * ecs.eventBus.subscribe('collision3D', (data) => {\n * console.log(data.entityA, data.entityB, data.normalZ);\n * });\n * ```\n */\nexport function createCollision3DPlugin<L extends string, G extends string = 'physics'>(\n\toptions: Collision3DPluginOptions<G> & { layers: LayerFactories<Record<L, readonly string[]>> },\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 0,\n\t\tphase = 'postUpdate',\n\t} = options;\n\n\treturn definePlugin('collision3D')\n\t\t.withComponentTypes<Collision3DComponentTypes<L>>()\n\t\t.withEventTypes<Collision3DEventTypes<L>>()\n\t\t.withLabels<'collision3D-detection'>()\n\t\t.withGroups<G>()\n\t\t.requires<Transform3DWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Grow-only pool of BaseColliderInfo3D slots reused across frames.\n\t\t\tconst colliderPool: BaseColliderInfo3D<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, BaseColliderInfo3D<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex3D | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tworld\n\t\t\t\t.addSystem('collision3D-detection')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['worldTransform3D', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t// TODO(perf): collider shape is discovered via two ecs.getComponent\n\t\t\t\t\t// calls per entity per frame because the query can't express\n\t\t\t\t\t// \"aabb3DCollider OR sphereCollider\". Splitting into two queries\n\t\t\t\t\t// (aabb-bearing, sphere-bearing) would eliminate these lookups at\n\t\t\t\t\t// the cost of two pool-fill passes. Keep in sync with physics3D.\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { worldTransform3D, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabb3DCollider');\n\t\t\t\t\t\tconst sphere = aabb ? undefined : ecs.getComponent(entity.id, 'sphereCollider');\n\t\t\t\t\t\tif (!aabb && !sphere) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: worldTransform3D.x,\n\t\t\t\t\t\t\t\ty: worldTransform3D.y,\n\t\t\t\t\t\t\t\tz: worldTransform3D.z,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tshape: AABB3D_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\thalfDepth: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, worldTransform3D.x, worldTransform3D.y, worldTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, sphere,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex3D>('spatialIndex3D');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions3D(colliderPool, count, broadphaseMap, cachedSI, onCollisionDetected3D<L>, ecs.eventBus);\n\t\t\t\t});\n\t\t});\n}\n",
|
|
8
|
-
"/**\n * Shared Narrowphase Module — 3D\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline for 3D collider pairs (AABB3D + Sphere).\n *\n * Mirrors the 2D narrowphase (`narrowphase.ts`) with an added Z axis.\n */\n\nimport type { SpatialIndex3D } from './spatial-hash3D';\n\n// ==================== Contact3D ====================\n\n/**\n * Contact result from a 3D narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions3D` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact3D {\n\tnormalX: number;\n\tnormalY: number;\n\tnormalZ: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact3D passed down from `detectCollisions3D` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact3D = { normalX: 0, normalY: 0, normalZ: 0, depth: 0 };\n\n// ==================== BaseColliderInfo3D ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo3D layout. */\nexport const AABB3D_SHAPE = 0;\nexport const SPHERE_SHAPE = 1;\nexport type ColliderShape3D = typeof AABB3D_SHAPE | typeof SPHERE_SHAPE;\n\n/**\n * Minimum collider data shared by 3D collision and physics bundles.\n *\n * Flat layout (no nested sub-objects): the `shape` discriminator tells you\n * whether to read `halfWidth`/`halfHeight`/`halfDepth` (AABB3D) or `radius`\n * (Sphere). Unused fields are set to 0.\n *\n * Pool-friendly — all fields are assigned in place each frame.\n */\nexport interface BaseColliderInfo3D<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tz: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\tshape: ColliderShape3D;\n\thalfWidth: number;\n\thalfHeight: number;\n\thalfDepth: number;\n\tradius: number;\n}\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo3D` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB3D and sphere colliders, AABB3D wins and only\n * the AABB3D offset is applied.\n */\nexport function fillBaseColliderInfo3D<L extends string>(\n\tinfo: BaseColliderInfo3D<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tz: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb3D: { width: number; height: number; depth: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n\tsphere: { radius: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\n\tif (aabb3D) {\n\t\tinfo.x = x + (aabb3D.offsetX ?? 0);\n\t\tinfo.y = y + (aabb3D.offsetY ?? 0);\n\t\tinfo.z = z + (aabb3D.offsetZ ?? 0);\n\t\tinfo.shape = AABB3D_SHAPE;\n\t\tinfo.halfWidth = aabb3D.width / 2;\n\t\tinfo.halfHeight = aabb3D.height / 2;\n\t\tinfo.halfDepth = aabb3D.depth / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (sphere) {\n\t\tinfo.x = x + (sphere.offsetX ?? 0);\n\t\tinfo.y = y + (sphere.offsetY ?? 0);\n\t\tinfo.z = z + (sphere.offsetZ ?? 0);\n\t\tinfo.shape = SPHERE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.halfDepth = 0;\n\t\tinfo.radius = sphere.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex3D resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex3D or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex3D(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex3D | undefined {\n\treturn tryGetResource<SpatialIndex3D>('spatialIndex3D');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB3D-vs-AABB3D contact into `out`. Returns `true` if the\n * shapes overlap (out was filled), `false` otherwise.\n *\n * Resolves along the axis with minimum penetration depth.\n */\nexport function computeAABB3DvsAABB3D(\n\tax: number, ay: number, az: number, ahw: number, ahh: number, ahd: number,\n\tbx: number, by: number, bz: number, bhw: number, bhh: number, bhd: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\tconst overlapZ = (ahd + bhd) - Math.abs(dz);\n\n\tif (overlapX <= 0 || overlapY <= 0 || overlapZ <= 0) return false;\n\n\tif (overlapX <= overlapY && overlapX <= overlapZ) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\n\tif (overlapY <= overlapZ) {\n\t\tout.normalX = 0;\n\t\tout.normalY = dy >= 0 ? 1 : -1;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapY;\n\t\treturn true;\n\t}\n\n\tout.normalX = 0;\n\tout.normalY = 0;\n\tout.normalZ = dz >= 0 ? 1 : -1;\n\tout.depth = overlapZ;\n\treturn true;\n}\n\n/**\n * Write a sphere-vs-sphere contact into `out`. Returns `true` if the\n * spheres overlap.\n */\nexport function computeSphereVsSphere(\n\tax: number, ay: number, az: number, ar: number,\n\tbx: number, by: number, bz: number, br: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\n/**\n * Write an AABB3D-vs-sphere contact into `out`. Returns `true` if the\n * shapes overlap.\n *\n * Uses closest-point-on-AABB to sphere center. When the sphere center\n * is inside the AABB, resolves along the axis with minimum push distance.\n */\nexport function computeAABB3DvsSphere(\n\taabbX: number, aabbY: number, aabbZ: number, ahw: number, ahh: number, ahd: number,\n\tsphereX: number, sphereY: number, sphereZ: number, radius: number,\n\tout: Contact3D,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(sphereX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(sphereY, aabbY + ahh));\n\tconst closestZ = Math.max(aabbZ - ahd, Math.min(sphereZ, aabbZ + ahd));\n\n\tconst dx = sphereX - closestX;\n\tconst dy = sphereY - closestY;\n\tconst dz = sphereZ - closestZ;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Sphere center inside AABB — resolve along minimum push axis\n\tif (distSq === 0) {\n\t\tconst pushLeft = (sphereX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - sphereX);\n\t\tconst pushUp = (sphereY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - sphereY);\n\t\tconst pushFront = (sphereZ - (aabbZ - ahd));\n\t\tconst pushBack = ((aabbZ + ahd) - sphereZ);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown, pushFront, pushBack);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.normalZ = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.normalZ = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.normalZ = 0; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushUp) {\n\t\t\tout.normalX = 0; out.normalY = -1; out.normalZ = 0; out.depth = pushUp + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushBack) {\n\t\t\tout.normalX = 0; out.normalY = 0; out.normalZ = 1; out.depth = pushBack + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = 0; out.normalZ = -1; out.depth = pushFront + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact3D(a: BaseColliderInfo3D, b: BaseColliderInfo3D, out: Contact3D): boolean {\n\tif (a.shape === AABB3D_SHAPE && b.shape === AABB3D_SHAPE) {\n\t\treturn computeAABB3DvsAABB3D(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === SPHERE_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeSphereVsSphere(\n\t\t\ta.x, a.y, a.z, a.radius,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB3D_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeAABB3DvsSphere(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Sphere, b is AABB3D — compute as AABB3D-vs-Sphere, then flip normal\n\tif (!computeAABB3DvsSphere(\n\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\ta.x, a.y, a.z, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\tout.normalZ = -out.normalZ;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable set for broadphase candidates. */\nconst _broadphaseCandidates = new Set<number>();\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic 3D collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions3D<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D | undefined,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] 3D collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndex3DPlugin() alongside your collision or physics3D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB3D_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB3D_SHAPE ? a.halfHeight : a.radius;\n\t\tconst aHalfD = a.shape === AABB3D_SHAPE ? a.halfDepth : a.radius;\n\n\t\t_broadphaseCandidates.clear();\n\t\tspatialIndex.queryBoxInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH, a.z - aHalfD,\n\t\t\ta.x + aHalfW, a.y + aHalfH, a.z + aHalfD,\n\t\t\t_broadphaseCandidates,\n\t\t);\n\n\t\t// TODO(perf): dense grids add every candidate (including `a` itself and\n\t\t// all lower-ID entities) to the set before the filter below discards ~half\n\t\t// of them. Emitting only pairs with larger IDs at query time — e.g. cells\n\t\t// as sorted arrays, or inserting entries in id-ascending order and having\n\t\t// the grid skip entries with id <= query-entity-id — would remove the\n\t\t// post-hoc filter and halve the Set churn for dense scenes.\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tif (bId <= a.entityId) continue;\n\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (!a.collidesWith.includes(b.layer) && !b.collidesWith.includes(a.layer)) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n"
|
|
5
|
+
"/**\n * Collision Plugin for ECSpresso\n *\n * Provides layer-based collision detection with events.\n * Uses worldTransform for position (world-space collision).\n * Supports AABB and circle colliders.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { TransformWorldConfig } from '../spatial/transform';\nimport { fillBaseColliderInfo, detectCollisions, createBroadphaseScratch, AABB_SHAPE, type Contact, type BaseColliderInfo } from '../../utils/narrowphase';\nimport type { SpatialIndex } from '../../utils/spatial-hash';\n\n// ==================== Component Types ====================\n\n/**\n * Axis-Aligned Bounding Box collider.\n */\nexport interface AABBCollider {\n\t/** Width of the bounding box */\n\twidth: number;\n\t/** Height of the bounding box */\n\theight: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Circle collider.\n */\nexport interface CircleCollider {\n\t/** Radius of the circle */\n\tradius: number;\n\t/** X offset from entity position (default: 0) */\n\toffsetX?: number;\n\t/** Y offset from entity position (default: 0) */\n\toffsetY?: number;\n}\n\n/**\n * Collision layer configuration.\n */\nexport interface CollisionLayer<L extends string = never> {\n\t/** The layer this entity belongs to */\n\tlayer: L;\n\t/** Layers this entity can collide with */\n\tcollidesWith: readonly L[];\n}\n\n/**\n * Component types provided by the collision plugin.\n * Included automatically via `.withPlugin(createCollisionPlugin())`.\n *\n * @example\n * ```typescript\n * const ecs = ECSpresso.create()\n * .withPlugin(createCollisionPlugin())\n * .withComponentTypes<{ sprite: Sprite; enemy: boolean }>()\n * .build();\n * ```\n */\nexport interface CollisionComponentTypes<L extends string = never> {\n\taabbCollider: AABBCollider;\n\tcircleCollider: CircleCollider;\n\tcollisionLayer: CollisionLayer<L>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when two entities collide.\n *\n * Normal components are flattened (`normalX`/`normalY`) rather than nested\n * in a sub-object to avoid a per-event allocation in the collision hot path.\n */\nexport interface CollisionEvent<L extends string = never> {\n\t/** First entity in the collision */\n\tentityA: number;\n\t/** Second entity in the collision */\n\tentityB: number;\n\t/** Layer of the first entity */\n\tlayerA: L;\n\t/** Layer of the second entity */\n\tlayerB: L;\n\t/** Contact normal X, pointing from entityA toward entityB */\n\tnormalX: number;\n\t/** Contact normal Y, pointing from entityA toward entityB */\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Event types provided by the collision plugin.\n */\nexport interface CollisionEventTypes<L extends string = never> {\n\tcollision: CollisionEvent<L>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the collision plugin.\n */\nexport interface CollisionPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {\n\t/** Name of the collision event (default: 'collision') */\n\tcollisionEventName?: string;\n}\n\n// ==================== Helper Functions ====================\n\n/**\n * Create an AABB collider component.\n *\n * @param width Width of the bounding box\n * @param height Height of the bounding box\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * });\n * ```\n */\nexport function createAABBCollider(\n\twidth: number,\n\theight: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { aabbCollider: AABBCollider } {\n\tconst collider: AABBCollider = { width, height };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { aabbCollider: collider };\n}\n\n/**\n * Create a circle collider component.\n *\n * @param radius Radius of the circle\n * @param offsetX X offset from entity position\n * @param offsetY Y offset from entity position\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createCircleCollider(25),\n * });\n * ```\n */\nexport function createCircleCollider(\n\tradius: number,\n\toffsetX?: number,\n\toffsetY?: number\n): { circleCollider: CircleCollider } {\n\tconst collider: CircleCollider = { radius };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\treturn { circleCollider: collider };\n}\n\n/**\n * Create a collision layer component.\n *\n * @param layer The layer this entity belongs to\n * @param collidesWith Layers this entity can collide with\n * @returns Component object suitable for spreading into spawn()\n *\n * @example\n * ```typescript\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...createCollisionLayer('player', ['enemy', 'obstacle']),\n * });\n * ```\n */\nexport function createCollisionLayer<L extends string>(\n\tlayer: L,\n\tcollidesWith: readonly L[]\n): Pick<CollisionComponentTypes<L>, 'collisionLayer'> {\n\treturn {\n\t\tcollisionLayer: { layer, collidesWith },\n\t};\n}\n\n/**\n * Layer factory result from defineCollisionLayers.\n */\nexport type LayerFactories<T extends Record<string, readonly string[]>> = {\n\t[K in keyof T]: () => Pick<CollisionComponentTypes<Extract<keyof T, string>>, 'collisionLayer'>;\n};\n\n/**\n * Extract layer names from a `defineCollisionLayers` result for use with\n * `createCollisionPairHandler`'s `L` type parameter.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n * ```\n */\nexport type LayersOf<T> = Extract<keyof T, string>;\n\n/**\n * Define collision layer relationships and get factory functions.\n *\n * @param rules Object mapping layer names to arrays of layers they collide with\n * @returns Object with factory functions for each layer\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({\n * player: ['enemy', 'enemyProjectile'],\n * playerProjectile: ['enemy'],\n * enemy: ['playerProjectile'],\n * enemyProjectile: ['player'],\n * });\n *\n * // Usage\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\n/**\n * Validates that all `collidesWith` values reference actual layer keys.\n * Catches typos at compile time.\n */\ntype ValidateCollidesWith<T> = {\n\t[K in keyof T]: T[K] extends readonly (infer V)[]\n\t\t? [V] extends [Extract<keyof T, string>] ? T[K] : readonly Extract<keyof T, string>[]\n\t\t: never;\n};\n\nexport function defineCollisionLayers<const T extends Record<string, readonly string[]>>(\n\trules: T & ValidateCollidesWith<T>\n): LayerFactories<T> {\n\ttype L = Extract<keyof T, string>;\n\tconst factories = {} as LayerFactories<T>;\n\n\tfor (const layer of Object.keys(rules) as Array<L>) {\n\t\tconst collidesWith = rules[layer] as readonly L[];\n\t\tfactories[layer] = () => createCollisionLayer<L>(layer, collidesWith);\n\t}\n\n\treturn factories;\n}\n\n// ==================== Collision Pair Handler ====================\n\n/**\n * Callback for a collision pair handler.\n *\n * @param firstEntityId Entity belonging to the first layer in the pair key\n * @param secondEntityId Entity belonging to the second layer in the pair key\n * @param ecs The ECS world instance (passed through from the subscriber)\n */\nexport type CollisionPairCallback<W = unknown> = (\n\tfirstEntityId: number,\n\tsecondEntityId: number,\n\tecs: W,\n) => void;\n\ninterface PairEntry<W> {\n\tcallback: CollisionPairCallback<W>;\n\tswapped: boolean;\n}\n\nfunction parsePairKey(key: string): [string, string] {\n\tconst colonIndex = key.indexOf(':');\n\tif (colonIndex === -1) {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": must contain a colon separator (e.g. \"player:enemy\")`);\n\t}\n\tconst layerA = key.slice(0, colonIndex);\n\tconst layerB = key.slice(colonIndex + 1);\n\tif (layerA === '' || layerB === '') {\n\t\tthrow new Error(`Invalid collision pair key \"${key}\": layer names must not be empty`);\n\t}\n\treturn [layerA, layerB];\n}\n\n/**\n * Create a collision pair handler that routes collision events to\n * layer-pair-specific callbacks.\n *\n * Registering `\"a:b\"` automatically handles both `(layerA=a, layerB=b)` and\n * `(layerA=b, layerB=a)`. Entity arguments are swapped to match the declared\n * key order. If both `\"a:b\"` and `\"b:a\"` are explicitly registered, each gets\n * its own handler with no implicit reverse.\n *\n * @typeParam W - The ECS world type (e.g. `ECSpresso<C, E, R>`). Defaults to `unknown`.\n * @typeParam L - Union of valid layer names. Defaults to `string`.\n * Provide specific layer names for compile-time key validation:\n * `createCollisionPairHandler<ECS, keyof typeof layers>({...})`\n *\n * @param pairs Object mapping `\"layerA:layerB\"` keys to callbacks\n * @returns A dispatch function to call with collision event data and ECS instance\n *\n * @example\n * ```typescript\n * // Basic usage:\n * const handler = createCollisionPairHandler<ECS>({\n * 'playerProjectile:enemy': (projectileId, enemyId, ecs) => {\n * ecs.commands.removeEntity(projectileId);\n * },\n * });\n *\n * // With layer name validation:\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * type Layer = LayersOf<typeof layers>;\n * const handler = createCollisionPairHandler<ECS, Layer>({\n * 'player:enemy': (playerId, enemyId, ecs) => { ... },\n * });\n *\n * ecs.eventBus.subscribe('collision', (data) => handler({ data, ecs }));\n * ```\n */\nexport function createCollisionPairHandler<W = unknown, L extends string = string>(\n\tpairs: { [K in `${L}:${L}`]?: CollisionPairCallback<W> }\n): (ctx: { data: CollisionEvent<L>; ecs: W }) => void;\nexport function createCollisionPairHandler<W = unknown>(\n\tpairs: Record<string, CollisionPairCallback<W> | undefined>\n): (ctx: { data: CollisionEvent<string>; ecs: W }) => void {\n\tconst lookup = new Map<string, PairEntry<W>>();\n\tconst explicitKeys = new Set<string>();\n\n\t// First pass: collect all explicit keys\n\tfor (const key of Object.keys(pairs)) {\n\t\tparsePairKey(key); // validate\n\t\texplicitKeys.add(key);\n\t}\n\n\t// Second pass: build lookup with forward + conditional reverse entries\n\tfor (const key of Object.keys(pairs)) {\n\t\tconst [layerA, layerB] = parsePairKey(key);\n\t\tconst callback = pairs[key];\n\t\tif (!callback) continue;\n\n\t\t// Forward entry\n\t\tlookup.set(key, { callback, swapped: false });\n\n\t\t// Reverse entry (only if the reverse key wasn't explicitly registered\n\t\t// and it's not a self-collision where forward === reverse)\n\t\tconst reverseKey = `${layerB}:${layerA}`;\n\t\tif (reverseKey !== key && !explicitKeys.has(reverseKey)) {\n\t\t\tlookup.set(reverseKey, { callback, swapped: true });\n\t\t}\n\t}\n\n\treturn function collisionPairDispatch({ data: event, ecs }: { data: CollisionEvent<string>; ecs: W }): void {\n\t\tconst entry = lookup.get(event.layerA + ':' + event.layerB);\n\t\tif (!entry) return;\n\n\t\tif (entry.swapped) {\n\t\t\tentry.callback(event.entityB, event.entityA, ecs);\n\t\t} else {\n\t\t\tentry.callback(event.entityA, event.entityB, ecs);\n\t\t}\n\t};\n}\n\n// ==================== Dependency Types ====================\n\n// ==================== Module-level Collision Callback ====================\n\ninterface CollisionEventBus<L extends string> {\n\tpublish(event: 'collision', data: CollisionEvent<L>): void;\n}\n\n/**\n * Module-level reusable collision event. Subscribers must consume\n * synchronously — same contract as the shared narrowphase Contact.\n */\nconst _collisionEvent: CollisionEvent<string> = {\n\tentityA: 0, entityB: 0, layerA: '', layerB: '',\n\tnormalX: 0, normalY: 0, depth: 0,\n};\n\nfunction onCollisionDetected<L extends string>(\n\ta: BaseColliderInfo<L>,\n\tb: BaseColliderInfo<L>,\n\tcontact: Contact,\n\teventBus: CollisionEventBus<L>,\n): void {\n\t_collisionEvent.entityA = a.entityId;\n\t_collisionEvent.entityB = b.entityId;\n\t_collisionEvent.layerA = a.layer;\n\t_collisionEvent.layerB = b.layer;\n\t_collisionEvent.normalX = contact.normalX;\n\t_collisionEvent.normalY = contact.normalY;\n\t_collisionEvent.depth = contact.depth;\n\teventBus.publish('collision', _collisionEvent as CollisionEvent<L>);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a collision plugin for ECSpresso.\n *\n * This plugin provides:\n * - Collision detection between entities with colliders\n * - AABB-AABB, circle-circle, and AABB-circle collision\n * - Layer-based filtering for collision pairs\n * - Deduplication of A-B / B-A collisions\n * - Automatic broadphase acceleration when spatialIndex resource is present\n *\n * Uses worldTransform for position (world-space collision detection).\n * The `layers` parameter is required for type inference — at runtime the\n * plugin does not consume it.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * const ecs = ECSpresso\n * .create()\n * .withPlugin(createTransformPlugin())\n * .withPlugin(createCollisionPlugin({ layers }))\n * .build();\n *\n * // Entity with collision\n * ecs.spawn({\n * ...createTransform(100, 200),\n * ...createAABBCollider(50, 30),\n * ...layers.player(),\n * });\n * ```\n */\nexport function createCollisionPlugin<L extends string, G extends string = 'physics'>(\n\toptions: CollisionPluginOptions<G> & { layers: LayerFactories<Record<L, readonly string[]>> }\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 0,\n\t\tphase = 'postUpdate',\n\t} = options;\n\n\treturn definePlugin('collision')\n\t\t.withComponentTypes<CollisionComponentTypes<L>>()\n\t\t.withEventTypes<CollisionEventTypes<L>>()\n\t\t.withLabels<'collision-detection'>()\n\t\t.withGroups<G>()\n\t\t.requires<TransformWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Grow-only pool of BaseColliderInfo slots reused across frames.\n\t\t\t// Steady-state: zero allocations per frame once the pool is warm.\n\t\t\tconst colliderPool: BaseColliderInfo<L>[] = [];\n\t\t\tconst broadphaseScratch = createBroadphaseScratch<BaseColliderInfo<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tworld\n\t\t\t\t.addSystem('collision-detection')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['worldTransform', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t// TODO(perf): collider shape is discovered via two ecs.getComponent\n\t\t\t\t\t// calls per entity per frame because the query can't express\n\t\t\t\t\t// \"aabbCollider OR circleCollider\". Splitting into two queries\n\t\t\t\t\t// (aabb-bearing, circle-bearing) would eliminate these lookups at\n\t\t\t\t\t// the cost of two pool-fill passes. Keep in sync with collision3D.\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { worldTransform, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabbCollider');\n\t\t\t\t\t\tconst circle = aabb ? undefined : ecs.getComponent(entity.id, 'circleCollider');\n\t\t\t\t\t\tif (!aabb && !circle) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: worldTransform.x,\n\t\t\t\t\t\t\t\ty: worldTransform.y,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tlayerBit: 0,\n\t\t\t\t\t\t\t\tcollidesWithMask: 0,\n\t\t\t\t\t\t\t\tshape: AABB_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, worldTransform.x, worldTransform.y,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, circle,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex>('spatialIndex');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions(colliderPool, count, broadphaseScratch, cachedSI, onCollisionDetected<L>, ecs.eventBus);\n\t\t\t\t});\n\t\t});\n}\n\n",
|
|
6
|
+
"/**\n * Lazy monotonic registry mapping layer name → unique bit. Lets pair\n * filtering use a single `(a.collidesWithMask & b.layerBit)` check\n * instead of `Array.includes` on every collision pair.\n *\n * One registry per dimension (2D and 3D) — user-defined layer namespaces\n * are independent, so bits should not be shared across systems.\n *\n * Maximum 32 layers per registry (one per bit in a 32-bit signed int).\n * Crossing the limit throws on the next `getLayerBit` call.\n */\nexport interface LayerBitRegistry {\n\tgetLayerBit(layer: string): number;\n\t/** OR of `getLayerBit` for every entry. Cached by array reference. */\n\tgetCollidesWithMask(collidesWith: readonly string[]): number;\n}\n\nexport function createLayerBitRegistry(label: string): LayerBitRegistry {\n\tconst layerBits = new Map<string, number>();\n\tconst maskCache = new WeakMap<readonly string[], number>();\n\tlet nextBit = 1;\n\n\tfunction getLayerBit(layer: string): number {\n\t\tconst existing = layerBits.get(layer);\n\t\tif (existing !== undefined) return existing;\n\t\tif (nextBit === 0) {\n\t\t\tthrow new Error(\n\t\t\t\t`[ecspresso] ${label} layer bitmask overflow: more than 32 distinct layers registered`,\n\t\t\t);\n\t\t}\n\t\tconst bit = nextBit;\n\t\tlayerBits.set(layer, bit);\n\t\t// `<<= 1` rolls 1<<31 to 0, which is detected on the next call.\n\t\tnextBit <<= 1;\n\t\treturn bit;\n\t}\n\n\tfunction getCollidesWithMask(collidesWith: readonly string[]): number {\n\t\tconst cached = maskCache.get(collidesWith);\n\t\tif (cached !== undefined) return cached;\n\t\tlet mask = 0;\n\t\tfor (let i = 0; i < collidesWith.length; i++) {\n\t\t\tmask |= getLayerBit(collidesWith[i]!);\n\t\t}\n\t\tmaskCache.set(collidesWith, mask);\n\t\treturn mask;\n\t}\n\n\treturn { getLayerBit, getCollidesWithMask };\n}\n",
|
|
7
|
+
"/**\n * Shared Narrowphase Module\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline used by both the collision plugin (event-only) and\n * the physics2D plugin (impulse response).\n */\n\nimport type { SpatialIndex } from './spatial-hash';\nimport { createLayerBitRegistry } from './layer-bit-registry';\n\n// ==================== Contact ====================\n\n/**\n * Contact result from a narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact {\n\tnormalX: number;\n\tnormalY: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact passed down from `detectCollisions` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact = { normalX: 0, normalY: 0, depth: 0 };\n\n// ==================== BaseColliderInfo ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo layout. */\nexport const AABB_SHAPE = 0;\nexport const CIRCLE_SHAPE = 1;\nexport type ColliderShape = typeof AABB_SHAPE | typeof CIRCLE_SHAPE;\n\n/**\n * Minimum collider data shared by collision and physics bundles.\n *\n * Flat layout (no nested `aabb` / `circle` sub-objects): the `shape`\n * discriminator tells you whether to read `halfWidth`/`halfHeight`\n * (AABB) or `radius` (Circle). Unused fields are set to 0.\n *\n * This shape is pool-friendly — all fields are assigned in place each\n * frame without allocating nested objects.\n */\nexport interface BaseColliderInfo<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\t/**\n\t * Bit assigned to `layer` from the lazy layer registry. Populated by\n\t * `fillBaseColliderInfo`. Used together with `collidesWithMask` to\n\t * replace per-pair `Array.includes` layer checks with a single AND.\n\t */\n\tlayerBit: number;\n\t/** OR of `getLayerBit` for every entry in `collidesWith`. */\n\tcollidesWithMask: number;\n\tshape: ColliderShape;\n\thalfWidth: number;\n\thalfHeight: number;\n\tradius: number;\n}\n\n// ==================== Layer Bit Registry ====================\n\nconst _layerRegistry = createLayerBitRegistry('Collision');\nexport const getLayerBit = _layerRegistry.getLayerBit;\nexport const getCollidesWithMask = _layerRegistry.getCollidesWithMask;\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB and circle colliders, AABB wins and only\n * the AABB offset is applied. This matches the dispatch precedence in\n * `computeContact`; the previous implementation stacked both offsets,\n * which was a bug.\n */\nexport function fillBaseColliderInfo<L extends string>(\n\tinfo: BaseColliderInfo<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb: { width: number; height: number; offsetX?: number; offsetY?: number } | undefined,\n\tcircle: { radius: number; offsetX?: number; offsetY?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\tinfo.layerBit = getLayerBit(layer);\n\tinfo.collidesWithMask = getCollidesWithMask(collidesWith);\n\n\tif (aabb) {\n\t\tinfo.x = x + (aabb.offsetX ?? 0);\n\t\tinfo.y = y + (aabb.offsetY ?? 0);\n\t\tinfo.shape = AABB_SHAPE;\n\t\tinfo.halfWidth = aabb.width / 2;\n\t\tinfo.halfHeight = aabb.height / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (circle) {\n\t\tinfo.x = x + (circle.offsetX ?? 0);\n\t\tinfo.y = y + (circle.offsetY ?? 0);\n\t\tinfo.shape = CIRCLE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.radius = circle.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex | undefined {\n\treturn tryGetResource<SpatialIndex>('spatialIndex');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB-AABB contact into `out`. Returns `true` if the shapes\n * overlap (out was filled), `false` otherwise.\n */\nexport function computeAABBvsAABB(\n\tax: number, ay: number, ahw: number, ahh: number,\n\tbx: number, by: number, bhw: number, bhh: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\n\tif (overlapX <= 0 || overlapY <= 0) return false;\n\n\tif (overlapX < overlapY) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\tout.normalX = 0;\n\tout.normalY = dy >= 0 ? 1 : -1;\n\tout.depth = overlapY;\n\treturn true;\n}\n\nexport function computeCircleVsCircle(\n\tax: number, ay: number, ar: number,\n\tbx: number, by: number, br: number,\n\tout: Contact,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst distSq = dx * dx + dy * dy;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\nexport function computeAABBvsCircle(\n\taabbX: number, aabbY: number, ahw: number, ahh: number,\n\tcircleX: number, circleY: number, radius: number,\n\tout: Contact,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(circleX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(circleY, aabbY + ahh));\n\n\tconst dx = circleX - closestX;\n\tconst dy = circleY - closestY;\n\tconst distSq = dx * dx + dy * dy;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Circle center inside AABB\n\tif (distSq === 0) {\n\t\tconst pushLeft = (circleX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - circleX);\n\t\tconst pushUp = (circleY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - circleY);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = -1; out.depth = pushUp + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact(a: BaseColliderInfo, b: BaseColliderInfo, out: Contact): boolean {\n\tif (a.shape === AABB_SHAPE && b.shape === AABB_SHAPE) {\n\t\treturn computeAABBvsAABB(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === CIRCLE_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeCircleVsCircle(\n\t\t\ta.x, a.y, a.radius,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB_SHAPE && b.shape === CIRCLE_SHAPE) {\n\t\treturn computeAABBvsCircle(\n\t\t\ta.x, a.y, a.halfWidth, a.halfHeight,\n\t\t\tb.x, b.y, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Circle, b is AABB — compute as AABB-vs-Circle, then flip normal in place\n\tif (!computeAABBvsCircle(\n\t\tb.x, b.y, b.halfWidth, b.halfHeight,\n\t\ta.x, a.y, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\nconst _broadphaseCandidates: number[] = [];\n\n/**\n * Per-caller scratch for the broadphase entityId → collider lookup.\n *\n * Dense `arr` indexed by entityId, paired with a `gen` stamp array that marks\n * which slots are live this call. Bumping `current` invalidates all prior\n * entries without clearing — replaces the per-frame `Map.clear()` + N\n * `Map.set()` allocation churn that a `Map<number, I>` would incur.\n *\n * Owned per plugin instance (alongside its `colliderPool`), so concurrent\n * worlds don't share state and `I` stays fully typed without erasure.\n */\nexport interface BroadphaseScratch<I extends BaseColliderInfo> {\n\tarr: (I | undefined)[];\n\tgen: number[];\n\tcurrent: number;\n}\n\nexport function createBroadphaseScratch<I extends BaseColliderInfo>(): BroadphaseScratch<I> {\n\treturn { arr: [], gen: [], current: 0 };\n}\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `scratch` is a caller-owned `BroadphaseScratch<I>` used by the broadphase\n * path as an entityId → collider lookup. Allocate it once per plugin instance\n * and pass the same reference every call.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tscratch: BroadphaseScratch<I>,\n\tspatialIndex: SpatialIndex | undefined,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, scratch, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] Collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo, C>(\n\tcolliders: I[],\n\tcount: number,\n\tscratch: BroadphaseScratch<I>,\n\tspatialIndex: SpatialIndex,\n\tonContact: (a: I, b: I, contact: Contact, context: C) => void,\n\tcontext: C,\n): void {\n\tconst arr = scratch.arr;\n\tconst stamps = scratch.gen;\n\tconst gen = ++scratch.current;\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tconst id = c.entityId;\n\t\tarr[id] = c;\n\t\tstamps[id] = gen;\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB_SHAPE ? a.halfHeight : a.radius;\n\n\t\t_broadphaseCandidates.length = 0;\n\t\tspatialIndex.queryRectInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH,\n\t\t\ta.x + aHalfW, a.y + aHalfH,\n\t\t\t_broadphaseCandidates,\n\t\t\ta.entityId,\n\t\t);\n\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tif (stamps[bId] !== gen) continue;\n\t\t\tconst b = arr[bId];\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n",
|
|
8
|
+
"/**\n * Collision 3D Plugin for ECSpresso\n *\n * Provides layer-based 3D collision detection with events.\n * Uses worldTransform3D for position (world-space collision).\n * Supports AABB3D and sphere colliders.\n */\n\nimport { definePlugin, type BasePluginOptions } from 'ecspresso';\nimport type { Transform3DWorldConfig } from '../spatial/transform3D';\nimport {\n\tfillBaseColliderInfo3D,\n\tdetectCollisions3D,\n\tAABB3D_SHAPE,\n\ttype Contact3D,\n\ttype BaseColliderInfo3D,\n} from '../../utils/narrowphase3D';\nimport type { SpatialIndex3D } from '../../utils/spatial-hash3D';\nimport {\n\tdefineCollisionLayers,\n\tcreateCollisionPairHandler,\n\tcreateCollisionLayer,\n\ttype CollisionLayer,\n\ttype LayerFactories,\n\ttype LayersOf,\n\ttype CollisionPairCallback,\n} from './collision';\n\n// Re-export dimension-agnostic layer utilities so consumers only need one import\nexport { defineCollisionLayers, createCollisionPairHandler, createCollisionLayer };\nexport type { CollisionLayer, LayerFactories, LayersOf, CollisionPairCallback };\n\n// Re-export collider shapes (defined in spatial-index3D to avoid duplication)\nexport type { AABB3DCollider, SphereCollider } from '../spatial/spatial-index3D';\nimport type { AABB3DCollider, SphereCollider } from '../spatial/spatial-index3D';\n\n// ==================== Component Types ====================\n\n/**\n * Component types provided by the collision3D plugin.\n */\nexport interface Collision3DComponentTypes<L extends string = never> {\n\taabb3DCollider: AABB3DCollider;\n\tsphereCollider: SphereCollider;\n\tcollisionLayer: CollisionLayer<L>;\n}\n\n// ==================== Event Types ====================\n\n/**\n * Event fired when two 3D entities collide.\n *\n * Normal components are flattened to avoid per-event allocation in the hot path.\n */\nexport interface Collision3DEvent<L extends string = never> {\n\tentityA: number;\n\tentityB: number;\n\tlayerA: L;\n\tlayerB: L;\n\t/** Contact normal X, pointing from entityA toward entityB */\n\tnormalX: number;\n\t/** Contact normal Y, pointing from entityA toward entityB */\n\tnormalY: number;\n\t/** Contact normal Z, pointing from entityA toward entityB */\n\tnormalZ: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Event types provided by the collision3D plugin.\n */\nexport interface Collision3DEventTypes<L extends string = never> {\n\tcollision3D: Collision3DEvent<L>;\n}\n\n// ==================== Plugin Options ====================\n\n/**\n * Configuration options for the collision3D plugin.\n */\nexport interface Collision3DPluginOptions<G extends string = 'physics'> extends BasePluginOptions<G> {}\n\n// ==================== Helper Functions ====================\n\nexport function createAABB3DCollider(\n\twidth: number,\n\theight: number,\n\tdepth: number,\n\toffsetX?: number,\n\toffsetY?: number,\n\toffsetZ?: number,\n): { aabb3DCollider: AABB3DCollider } {\n\tconst collider: AABB3DCollider = { width, height, depth };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\tif (offsetZ !== undefined) collider.offsetZ = offsetZ;\n\treturn { aabb3DCollider: collider };\n}\n\nexport function createSphereCollider(\n\tradius: number,\n\toffsetX?: number,\n\toffsetY?: number,\n\toffsetZ?: number,\n): { sphereCollider: SphereCollider } {\n\tconst collider: SphereCollider = { radius };\n\tif (offsetX !== undefined) collider.offsetX = offsetX;\n\tif (offsetY !== undefined) collider.offsetY = offsetY;\n\tif (offsetZ !== undefined) collider.offsetZ = offsetZ;\n\treturn { sphereCollider: collider };\n}\n\n// ==================== Module-level Collision Callback ====================\n\ninterface Collision3DEventBus<L extends string> {\n\tpublish(event: 'collision3D', data: Collision3DEvent<L>): void;\n}\n\n/**\n * Module-level reusable collision event. Subscribers must consume synchronously —\n * same contract as the shared narrowphase Contact3D.\n */\nconst _collisionEvent: Collision3DEvent<string> = {\n\tentityA: 0, entityB: 0, layerA: '', layerB: '',\n\tnormalX: 0, normalY: 0, normalZ: 0, depth: 0,\n};\n\nfunction onCollisionDetected3D<L extends string>(\n\ta: BaseColliderInfo3D<L>,\n\tb: BaseColliderInfo3D<L>,\n\tcontact: Contact3D,\n\teventBus: Collision3DEventBus<L>,\n): void {\n\t_collisionEvent.entityA = a.entityId;\n\t_collisionEvent.entityB = b.entityId;\n\t_collisionEvent.layerA = a.layer;\n\t_collisionEvent.layerB = b.layer;\n\t_collisionEvent.normalX = contact.normalX;\n\t_collisionEvent.normalY = contact.normalY;\n\t_collisionEvent.normalZ = contact.normalZ;\n\t_collisionEvent.depth = contact.depth;\n\teventBus.publish('collision3D', _collisionEvent as Collision3DEvent<L>);\n}\n\n// ==================== Plugin Factory ====================\n\n/**\n * Create a 3D collision plugin for ECSpresso.\n *\n * Provides layer-based collision detection between entities with 3D colliders,\n * publishing `collision3D` events on contact. Supports AABB3D-AABB3D,\n * sphere-sphere, and AABB3D-sphere tests. Automatically uses the\n * `spatialIndex3D` resource for broadphase when present.\n *\n * @example\n * ```typescript\n * const layers = defineCollisionLayers({ player: ['enemy'], enemy: ['player'] });\n * const ecs = ECSpresso\n * .create()\n * .withPlugin(createTransform3DPlugin())\n * .withPlugin(createCollision3DPlugin({ layers }))\n * .build();\n *\n * ecs.eventBus.subscribe('collision3D', (data) => {\n * console.log(data.entityA, data.entityB, data.normalZ);\n * });\n * ```\n */\nexport function createCollision3DPlugin<L extends string, G extends string = 'physics'>(\n\toptions: Collision3DPluginOptions<G> & { layers: LayerFactories<Record<L, readonly string[]>> },\n) {\n\tconst {\n\t\tsystemGroup = 'physics',\n\t\tpriority = 0,\n\t\tphase = 'postUpdate',\n\t} = options;\n\n\treturn definePlugin('collision3D')\n\t\t.withComponentTypes<Collision3DComponentTypes<L>>()\n\t\t.withEventTypes<Collision3DEventTypes<L>>()\n\t\t.withLabels<'collision3D-detection'>()\n\t\t.withGroups<G>()\n\t\t.requires<Transform3DWorldConfig>()\n\t\t.install((world) => {\n\t\t\t// Grow-only pool of BaseColliderInfo3D slots reused across frames.\n\t\t\tconst colliderPool: BaseColliderInfo3D<L>[] = [];\n\t\t\t// Reusable entityId → collider lookup for the broadphase path.\n\t\t\tconst broadphaseMap = new Map<number, BaseColliderInfo3D<L>>();\n\t\t\t// Cached spatial index reference (resolved once on first frame).\n\t\t\tlet cachedSI: SpatialIndex3D | undefined;\n\t\t\tlet siResolved = false;\n\n\t\t\tworld\n\t\t\t\t.addSystem('collision3D-detection')\n\t\t\t\t.setPriority(priority)\n\t\t\t\t.inPhase(phase)\n\t\t\t\t.inGroup(systemGroup)\n\t\t\t\t.addQuery('collidables', {\n\t\t\t\t\twith: ['worldTransform3D', 'collisionLayer'],\n\t\t\t\t})\n\t\t\t\t.setProcess(({ queries, ecs }) => {\n\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t// TODO(perf): collider shape is discovered via two ecs.getComponent\n\t\t\t\t\t// calls per entity per frame because the query can't express\n\t\t\t\t\t// \"aabb3DCollider OR sphereCollider\". Splitting into two queries\n\t\t\t\t\t// (aabb-bearing, sphere-bearing) would eliminate these lookups at\n\t\t\t\t\t// the cost of two pool-fill passes. Keep in sync with physics3D.\n\t\t\t\t\tfor (const entity of queries.collidables) {\n\t\t\t\t\t\tconst { worldTransform3D, collisionLayer } = entity.components;\n\t\t\t\t\t\tconst aabb = ecs.getComponent(entity.id, 'aabb3DCollider');\n\t\t\t\t\t\tconst sphere = aabb ? undefined : ecs.getComponent(entity.id, 'sphereCollider');\n\t\t\t\t\t\tif (!aabb && !sphere) continue;\n\n\t\t\t\t\t\tlet slot = colliderPool[count];\n\t\t\t\t\t\tif (!slot) {\n\t\t\t\t\t\t\tslot = {\n\t\t\t\t\t\t\t\tentityId: entity.id,\n\t\t\t\t\t\t\t\tx: worldTransform3D.x,\n\t\t\t\t\t\t\t\ty: worldTransform3D.y,\n\t\t\t\t\t\t\t\tz: worldTransform3D.z,\n\t\t\t\t\t\t\t\tlayer: collisionLayer.layer,\n\t\t\t\t\t\t\t\tcollidesWith: collisionLayer.collidesWith,\n\t\t\t\t\t\t\t\tlayerBit: 0,\n\t\t\t\t\t\t\t\tcollidesWithMask: 0,\n\t\t\t\t\t\t\t\tshape: AABB3D_SHAPE,\n\t\t\t\t\t\t\t\thalfWidth: 0,\n\t\t\t\t\t\t\t\thalfHeight: 0,\n\t\t\t\t\t\t\t\thalfDepth: 0,\n\t\t\t\t\t\t\t\tradius: 0,\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\tcolliderPool[count] = slot;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (!fillBaseColliderInfo3D(\n\t\t\t\t\t\t\tslot,\n\t\t\t\t\t\t\tentity.id, worldTransform3D.x, worldTransform3D.y, worldTransform3D.z,\n\t\t\t\t\t\t\tcollisionLayer.layer, collisionLayer.collidesWith,\n\t\t\t\t\t\t\taabb, sphere,\n\t\t\t\t\t\t)) continue;\n\n\t\t\t\t\t\tcount++;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!siResolved) {\n\t\t\t\t\t\tcachedSI = ecs.tryGetResource<SpatialIndex3D>('spatialIndex3D');\n\t\t\t\t\t\tsiResolved = true;\n\t\t\t\t\t}\n\t\t\t\t\tdetectCollisions3D(colliderPool, count, broadphaseMap, cachedSI, onCollisionDetected3D<L>, ecs.eventBus);\n\t\t\t\t});\n\t\t});\n}\n",
|
|
9
|
+
"/**\n * Shared Narrowphase Module — 3D\n *\n * Provides contact-computing narrowphase tests and a generic collision\n * iteration pipeline for 3D collider pairs (AABB3D + Sphere).\n *\n * Mirrors the 2D narrowphase (`narrowphase.ts`) with an added Z axis.\n */\n\nimport type { SpatialIndex3D } from './spatial-hash3D';\nimport { createLayerBitRegistry } from './layer-bit-registry';\n\n// ==================== Contact3D ====================\n\n/**\n * Contact result from a 3D narrowphase test. Normal points from A toward B.\n *\n * Narrowphase functions use this as an out-parameter: the caller owns the\n * struct, the function writes fields in place and returns `true` on hit.\n * The `onContact` callback in `detectCollisions3D` receives a shared module-\n * level instance — **subscribers must consume it synchronously and must not\n * retain the reference across frames**.\n */\nexport interface Contact3D {\n\tnormalX: number;\n\tnormalY: number;\n\tnormalZ: number;\n\t/** Penetration depth (positive = overlapping) */\n\tdepth: number;\n}\n\n/**\n * Module-level reusable Contact3D passed down from `detectCollisions3D` into\n * narrowphase tests and forwarded to the `onContact` callback. Reused across\n * every pair in every frame — zero allocation in the narrowphase hot path.\n */\nconst _sharedContact: Contact3D = { normalX: 0, normalY: 0, normalZ: 0, depth: 0 };\n\n// ==================== BaseColliderInfo3D ====================\n\n/** Collider shape discriminator for the flattened BaseColliderInfo3D layout. */\nexport const AABB3D_SHAPE = 0;\nexport const SPHERE_SHAPE = 1;\nexport type ColliderShape3D = typeof AABB3D_SHAPE | typeof SPHERE_SHAPE;\n\n/**\n * Minimum collider data shared by 3D collision and physics bundles.\n *\n * Flat layout (no nested sub-objects): the `shape` discriminator tells you\n * whether to read `halfWidth`/`halfHeight`/`halfDepth` (AABB3D) or `radius`\n * (Sphere). Unused fields are set to 0.\n *\n * Pool-friendly — all fields are assigned in place each frame.\n */\nexport interface BaseColliderInfo3D<L extends string = string> {\n\tentityId: number;\n\tx: number;\n\ty: number;\n\tz: number;\n\tlayer: L;\n\tcollidesWith: readonly L[];\n\t/**\n\t * Bit assigned to `layer` from the lazy layer registry. Populated by\n\t * `fillBaseColliderInfo3D`. Used together with `collidesWithMask` to\n\t * replace per-pair `Array.includes` layer checks with a single AND.\n\t */\n\tlayerBit: number;\n\t/** OR of `getLayerBit3D` for every entry in `collidesWith`. */\n\tcollidesWithMask: number;\n\tshape: ColliderShape3D;\n\thalfWidth: number;\n\thalfHeight: number;\n\thalfDepth: number;\n\tradius: number;\n}\n\n// ==================== Layer Bit Registry ====================\n\n// Independent from the 2D registry: 2D and 3D layer namespaces are\n// defined separately by user code, so bits should not collide.\nconst _layerRegistry = createLayerBitRegistry('3D collision');\nexport const getLayerBit3D = _layerRegistry.getLayerBit;\nexport const getCollidesWithMask3D = _layerRegistry.getCollidesWithMask;\n\n// ==================== Collider Construction ====================\n\n/**\n * Populate a `BaseColliderInfo3D` slot in place from raw component data.\n * Returns `true` if the slot was filled, `false` if the entity has no\n * collider (caller should skip it).\n *\n * If an entity has both AABB3D and sphere colliders, AABB3D wins and only\n * the AABB3D offset is applied.\n */\nexport function fillBaseColliderInfo3D<L extends string>(\n\tinfo: BaseColliderInfo3D<L>,\n\tentityId: number,\n\tx: number,\n\ty: number,\n\tz: number,\n\tlayer: L,\n\tcollidesWith: readonly L[],\n\taabb3D: { width: number; height: number; depth: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n\tsphere: { radius: number; offsetX?: number; offsetY?: number; offsetZ?: number } | undefined,\n): boolean {\n\tinfo.entityId = entityId;\n\tinfo.layer = layer;\n\tinfo.collidesWith = collidesWith;\n\tinfo.layerBit = getLayerBit3D(layer);\n\tinfo.collidesWithMask = getCollidesWithMask3D(collidesWith);\n\n\tif (aabb3D) {\n\t\tinfo.x = x + (aabb3D.offsetX ?? 0);\n\t\tinfo.y = y + (aabb3D.offsetY ?? 0);\n\t\tinfo.z = z + (aabb3D.offsetZ ?? 0);\n\t\tinfo.shape = AABB3D_SHAPE;\n\t\tinfo.halfWidth = aabb3D.width / 2;\n\t\tinfo.halfHeight = aabb3D.height / 2;\n\t\tinfo.halfDepth = aabb3D.depth / 2;\n\t\tinfo.radius = 0;\n\t\treturn true;\n\t}\n\n\tif (sphere) {\n\t\tinfo.x = x + (sphere.offsetX ?? 0);\n\t\tinfo.y = y + (sphere.offsetY ?? 0);\n\t\tinfo.z = z + (sphere.offsetZ ?? 0);\n\t\tinfo.shape = SPHERE_SHAPE;\n\t\tinfo.halfWidth = 0;\n\t\tinfo.halfHeight = 0;\n\t\tinfo.halfDepth = 0;\n\t\tinfo.radius = sphere.radius;\n\t\treturn true;\n\t}\n\n\treturn false;\n}\n\n// ==================== Spatial Index Lookup ====================\n\n/**\n * Retrieve the optional spatialIndex3D resource, returning undefined when absent.\n * Centralizes the cross-plugin typed lookup so individual plugins don't each\n * need to import SpatialIndex3D or repeat the tryGetResource pattern.\n */\nexport function tryGetSpatialIndex3D(\n\ttryGetResource: <T>(key: string) => T | undefined,\n): SpatialIndex3D | undefined {\n\treturn tryGetResource<SpatialIndex3D>('spatialIndex3D');\n}\n\n// ==================== Narrowphase Tests ====================\n\n/**\n * Write an AABB3D-vs-AABB3D contact into `out`. Returns `true` if the\n * shapes overlap (out was filled), `false` otherwise.\n *\n * Resolves along the axis with minimum penetration depth.\n */\nexport function computeAABB3DvsAABB3D(\n\tax: number, ay: number, az: number, ahw: number, ahh: number, ahd: number,\n\tbx: number, by: number, bz: number, bhw: number, bhh: number, bhd: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst overlapX = (ahw + bhw) - Math.abs(dx);\n\tconst overlapY = (ahh + bhh) - Math.abs(dy);\n\tconst overlapZ = (ahd + bhd) - Math.abs(dz);\n\n\tif (overlapX <= 0 || overlapY <= 0 || overlapZ <= 0) return false;\n\n\tif (overlapX <= overlapY && overlapX <= overlapZ) {\n\t\tout.normalX = dx >= 0 ? 1 : -1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapX;\n\t\treturn true;\n\t}\n\n\tif (overlapY <= overlapZ) {\n\t\tout.normalX = 0;\n\t\tout.normalY = dy >= 0 ? 1 : -1;\n\t\tout.normalZ = 0;\n\t\tout.depth = overlapY;\n\t\treturn true;\n\t}\n\n\tout.normalX = 0;\n\tout.normalY = 0;\n\tout.normalZ = dz >= 0 ? 1 : -1;\n\tout.depth = overlapZ;\n\treturn true;\n}\n\n/**\n * Write a sphere-vs-sphere contact into `out`. Returns `true` if the\n * spheres overlap.\n */\nexport function computeSphereVsSphere(\n\tax: number, ay: number, az: number, ar: number,\n\tbx: number, by: number, bz: number, br: number,\n\tout: Contact3D,\n): boolean {\n\tconst dx = bx - ax;\n\tconst dy = by - ay;\n\tconst dz = bz - az;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\tconst radiusSum = ar + br;\n\n\tif (distSq >= radiusSum * radiusSum) return false;\n\n\tconst dist = Math.sqrt(distSq);\n\tif (dist === 0) {\n\t\tout.normalX = 1;\n\t\tout.normalY = 0;\n\t\tout.normalZ = 0;\n\t\tout.depth = radiusSum;\n\t\treturn true;\n\t}\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radiusSum - dist;\n\treturn true;\n}\n\n/**\n * Write an AABB3D-vs-sphere contact into `out`. Returns `true` if the\n * shapes overlap.\n *\n * Uses closest-point-on-AABB to sphere center. When the sphere center\n * is inside the AABB, resolves along the axis with minimum push distance.\n */\nexport function computeAABB3DvsSphere(\n\taabbX: number, aabbY: number, aabbZ: number, ahw: number, ahh: number, ahd: number,\n\tsphereX: number, sphereY: number, sphereZ: number, radius: number,\n\tout: Contact3D,\n): boolean {\n\tconst closestX = Math.max(aabbX - ahw, Math.min(sphereX, aabbX + ahw));\n\tconst closestY = Math.max(aabbY - ahh, Math.min(sphereY, aabbY + ahh));\n\tconst closestZ = Math.max(aabbZ - ahd, Math.min(sphereZ, aabbZ + ahd));\n\n\tconst dx = sphereX - closestX;\n\tconst dy = sphereY - closestY;\n\tconst dz = sphereZ - closestZ;\n\tconst distSq = dx * dx + dy * dy + dz * dz;\n\n\tif (distSq >= radius * radius) return false;\n\n\t// Sphere center inside AABB — resolve along minimum push axis\n\tif (distSq === 0) {\n\t\tconst pushLeft = (sphereX - (aabbX - ahw));\n\t\tconst pushRight = ((aabbX + ahw) - sphereX);\n\t\tconst pushUp = (sphereY - (aabbY - ahh));\n\t\tconst pushDown = ((aabbY + ahh) - sphereY);\n\t\tconst pushFront = (sphereZ - (aabbZ - ahd));\n\t\tconst pushBack = ((aabbZ + ahd) - sphereZ);\n\t\tconst minPush = Math.min(pushLeft, pushRight, pushUp, pushDown, pushFront, pushBack);\n\n\t\tif (minPush === pushRight) {\n\t\t\tout.normalX = 1; out.normalY = 0; out.normalZ = 0; out.depth = pushRight + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushLeft) {\n\t\t\tout.normalX = -1; out.normalY = 0; out.normalZ = 0; out.depth = pushLeft + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushDown) {\n\t\t\tout.normalX = 0; out.normalY = 1; out.normalZ = 0; out.depth = pushDown + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushUp) {\n\t\t\tout.normalX = 0; out.normalY = -1; out.normalZ = 0; out.depth = pushUp + radius;\n\t\t\treturn true;\n\t\t}\n\t\tif (minPush === pushBack) {\n\t\t\tout.normalX = 0; out.normalY = 0; out.normalZ = 1; out.depth = pushBack + radius;\n\t\t\treturn true;\n\t\t}\n\t\tout.normalX = 0; out.normalY = 0; out.normalZ = -1; out.depth = pushFront + radius;\n\t\treturn true;\n\t}\n\n\tconst dist = Math.sqrt(distSq);\n\tout.normalX = dx / dist;\n\tout.normalY = dy / dist;\n\tout.normalZ = dz / dist;\n\tout.depth = radius - dist;\n\treturn true;\n}\n\n// ==================== Contact Dispatcher ====================\n\n/**\n * Dispatch to the correct narrowphase function for the given pair and\n * write the contact into `out`. Returns `true` if the pair overlaps.\n */\nexport function computeContact3D(a: BaseColliderInfo3D, b: BaseColliderInfo3D, out: Contact3D): boolean {\n\tif (a.shape === AABB3D_SHAPE && b.shape === AABB3D_SHAPE) {\n\t\treturn computeAABB3DvsAABB3D(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === SPHERE_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeSphereVsSphere(\n\t\t\ta.x, a.y, a.z, a.radius,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\tif (a.shape === AABB3D_SHAPE && b.shape === SPHERE_SHAPE) {\n\t\treturn computeAABB3DvsSphere(\n\t\t\ta.x, a.y, a.z, a.halfWidth, a.halfHeight, a.halfDepth,\n\t\t\tb.x, b.y, b.z, b.radius,\n\t\t\tout,\n\t\t);\n\t}\n\n\t// a is Sphere, b is AABB3D — compute as AABB3D-vs-Sphere, then flip normal\n\tif (!computeAABB3DvsSphere(\n\t\tb.x, b.y, b.z, b.halfWidth, b.halfHeight, b.halfDepth,\n\t\ta.x, a.y, a.z, a.radius,\n\t\tout,\n\t)) return false;\n\tout.normalX = -out.normalX;\n\tout.normalY = -out.normalY;\n\tout.normalZ = -out.normalZ;\n\treturn true;\n}\n\n// ==================== Collision Iteration ====================\n\n/** Module-level reusable array for broadphase candidates. */\nconst _broadphaseCandidates: number[] = [];\n\nlet _bruteForceWarned = false;\nconst BRUTE_FORCE_WARN_THRESHOLD = 50;\n\n/**\n * Generic 3D collision detection pipeline: brute-force or broadphase,\n * with layer filtering and contact computation.\n *\n * `count` is the number of live entries at the front of `colliders`.\n * The array itself may be a grow-only pool — only indices `[0, count)`\n * are iterated, so trailing pool slots are ignored.\n *\n * `workingMap` is a caller-owned `Map<number, I>` used by the broadphase\n * path as an entityId → collider lookup. It is cleared and repopulated on\n * each call; callers should allocate it once and pass the same instance\n * every frame.\n *\n * Uses a context parameter forwarded to the callback to avoid\n * per-frame closure allocation.\n */\nexport function detectCollisions3D<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tworkingMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D | undefined,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (spatialIndex) {\n\t\tbroadphaseDetect(colliders, count, workingMap, spatialIndex, onContact, context);\n\t} else {\n\t\tbruteForceDetect(colliders, count, onContact, context);\n\t}\n}\n\nfunction bruteForceDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tif (!_bruteForceWarned && count >= BRUTE_FORCE_WARN_THRESHOLD) {\n\t\t_bruteForceWarned = true;\n\t\tconsole.warn(\n\t\t\t`[ecspresso] 3D collision detection is using O(n²) brute force with ${count} colliders. ` +\n\t\t\t`For better performance, install createSpatialIndex3DPlugin() alongside your collision or physics3D plugin.`,\n\t\t);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tfor (let j = i + 1; j < count; j++) {\n\t\t\tconst b = colliders[j];\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n\nfunction broadphaseDetect<I extends BaseColliderInfo3D, C>(\n\tcolliders: I[],\n\tcount: number,\n\tcolliderMap: Map<number, I>,\n\tspatialIndex: SpatialIndex3D,\n\tonContact: (a: I, b: I, contact: Contact3D, context: C) => void,\n\tcontext: C,\n): void {\n\tcolliderMap.clear();\n\tfor (let i = 0; i < count; i++) {\n\t\tconst c = colliders[i];\n\t\tif (!c) continue;\n\t\tcolliderMap.set(c.entityId, c);\n\t}\n\n\tfor (let i = 0; i < count; i++) {\n\t\tconst a = colliders[i];\n\t\tif (!a) continue;\n\n\t\tconst aHalfW = a.shape === AABB3D_SHAPE ? a.halfWidth : a.radius;\n\t\tconst aHalfH = a.shape === AABB3D_SHAPE ? a.halfHeight : a.radius;\n\t\tconst aHalfD = a.shape === AABB3D_SHAPE ? a.halfDepth : a.radius;\n\n\t\t_broadphaseCandidates.length = 0;\n\t\tspatialIndex.queryBoxInto(\n\t\t\ta.x - aHalfW, a.y - aHalfH, a.z - aHalfD,\n\t\t\ta.x + aHalfW, a.y + aHalfH, a.z + aHalfD,\n\t\t\t_broadphaseCandidates,\n\t\t\ta.entityId,\n\t\t);\n\n\t\tfor (const bId of _broadphaseCandidates) {\n\t\t\tconst b = colliderMap.get(bId);\n\t\t\tif (!b) continue;\n\n\t\t\tif (((a.collidesWithMask & b.layerBit) | (b.collidesWithMask & a.layerBit)) === 0) continue;\n\n\t\t\tif (!computeContact3D(a, b, _sharedContact)) continue;\n\n\t\t\tonContact(a, b, _sharedContact, context);\n\t\t}\n\t}\n}\n"
|
|
9
10
|
],
|
|
10
|
-
"mappings": "4PAQA,uBAAS,kBCyBT,IAAM,EAA0B,CAAE,QAAS,EAAG,QAAS,EAAG,MAAO,CAAE,EAKtD,EAAa,EAsCnB,SAAS,CAAsC,CACrD,EACA,EACA,EACA,EACA,EACA,EACA,EACA,EACU,CAKV,GAJA,EAAK,SAAW,EAChB,EAAK,MAAQ,EACb,EAAK,aAAe,EAEhB,EAOH,OANA,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,EAAI,GAAK,EAAK,SAAW,GAC9B,EAAK,MAvDmB,EAwDxB,EAAK,UAAY,EAAK,MAAQ,EAC9B,EAAK,WAAa,EAAK,OAAS,EAChC,EAAK,OAAS,EACP,GAGR,GAAI,EAOH,OANA,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,EAAI,GAAK,EAAO,SAAW,GAChC,EAAK,MAhEqB,EAiE1B,EAAK,UAAY,EACjB,EAAK,WAAa,EAClB,EAAK,OAAS,EAAO,OACd,GAGR,MAAO,GAsBD,SAAS,CAAiB,CAChC,EAAY,EAAY,EAAa,EACrC,EAAY,EAAY,EAAa,EACrC,EACU,CACV,IAAM,EAAK,EAAK,EACV,EAAK,EAAK,EACV,EAAY,EAAM,EAAO,KAAK,IAAI,CAAE,EACpC,EAAY,EAAM,EAAO,KAAK,IAAI,CAAE,EAE1C,GAAI,GAAY,GAAK,GAAY,EAAG,MAAO,GAE3C,GAAI,EAAW,EAId,OAHA,EAAI,QAAU,GAAM,EAAI,EAAI,GAC5B,EAAI,Q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C,IAAQ,mBAAkB,kBAAmB,EAAO,WAC9C,EAAO,EAAI,aAAa,EAAO,GAAI,gBAAgB,EACnD,EAAS,EAAO,OAAY,EAAI,aAAa,EAAO,GAAI,gBAAgB,EAC9E,GAAI,CAAC,GAAQ,CAAC,EAAQ,SAEtB,IAAI,EAAO,EAAa,GACxB,GAAI,CAAC,EACJ,EAAO,CACN,SAAU,EAAO,GACjB,EAAG,EAAiB,EACpB,EAAG,EAAiB,EACpB,EAAG,EAAiB,EACpB,MAAO,EAAe,MACtB,aAAc,EAAe,aAC7B,SAAU,EACV,iBAAkB,EAClB,MAAO,EACP,UAAW,EACX,WAAY,EACZ,UAAW,EACX,OAAQ,CACT,EACA,EAAa,GAAS,EAGvB,GAAI,CAAC,EACJ,EACA,EAAO,GAAI,EAAiB,EAAG,EAAiB,EAAG,EAAiB,EACpE,EAAe,MAAO,EAAe,aACrC,EAAM,CACP,EAAG,SAEH,IAGD,GAAI,CAAC,EACJ,EAAW,EAAI,eAA+B,gBAAgB,EAC9D,EAAa,GAEd,EAAmB,EAAc,EAAO,EAAe,EAAU,GAA0B,EAAI,QAAQ,EACvG,EACF",
|
|
12
|
+
"debugId": "397322863DC03B6264756E2164756E21",
|
|
12
13
|
"names": []
|
|
13
14
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var
|
|
1
|
+
var i=((z)=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(z,{get:(J,Q)=>(typeof require<"u"?require:J)[Q]}):z)(function(z){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+z+'" is not supported')});import{definePlugin as s}from"ecspresso";function A(z){let J=new Map,Q=new WeakMap,j=1;function $(K){let Z=J.get(K);if(Z!==void 0)return Z;if(j===0)throw Error(`[ecspresso] ${z} layer bitmask overflow: more than 32 distinct layers registered`);let V=j;return J.set(K,V),j<<=1,V}function O(K){let Z=Q.get(K);if(Z!==void 0)return Z;let V=0;for(let G=0;G<K.length;G++)V|=$(K[G]);return Q.set(K,V),V}return{getLayerBit:$,getCollidesWithMask:O}}var S={normalX:0,normalY:0,depth:0},E=0,w=1,v=A("Collision"),y=v.getLayerBit,u=v.getCollidesWithMask;function g(z,J,Q,j,$,O,K,Z){if(z.entityId=J,z.layer=$,z.collidesWith=O,z.layerBit=y($),z.collidesWithMask=u(O),K)return z.x=Q+(K.offsetX??0),z.y=j+(K.offsetY??0),z.shape=E,z.halfWidth=K.width/2,z.halfHeight=K.height/2,z.radius=0,!0;if(Z)return z.x=Q+(Z.offsetX??0),z.y=j+(Z.offsetY??0),z.shape=w,z.halfWidth=0,z.halfHeight=0,z.radius=Z.radius,!0;return!1}function b(z,J,Q,j,$,O,K,Z,V){let G=$-z,U=O-J,F=Q+K-Math.abs(G),k=j+Z-Math.abs(U);if(F<=0||k<=0)return!1;if(F<k)return V.normalX=G>=0?1:-1,V.normalY=0,V.depth=F,!0;return V.normalX=0,V.normalY=U>=0?1:-1,V.depth=k,!0}function d(z,J,Q,j,$,O,K){let Z=j-z,V=$-J,G=Z*Z+V*V,U=Q+O;if(G>=U*U)return!1;let F=Math.sqrt(G);if(F===0)return K.normalX=1,K.normalY=0,K.depth=U,!0;return K.normalX=Z/F,K.normalY=V/F,K.depth=U-F,!0}function B(z,J,Q,j,$,O,K,Z){let V=Math.max(z-Q,Math.min($,z+Q)),G=Math.max(J-j,Math.min(O,J+j)),U=$-V,F=O-G,k=U*U+F*F;if(k>=K*K)return!1;if(k===0){let q=$-(z-Q),D=z+Q-$,T=O-(J-j),L=J+j-O,P=Math.min(q,D,T,L);if(P===D)return Z.normalX=1,Z.normalY=0,Z.depth=D+K,!0;if(P===q)return Z.normalX=-1,Z.normalY=0,Z.depth=q+K,!0;if(P===L)return Z.normalX=0,Z.normalY=1,Z.depth=L+K,!0;return Z.normalX=0,Z.normalY=-1,Z.depth=T+K,!0}let N=Math.sqrt(k);return Z.normalX=U/N,Z.normalY=F/N,Z.depth=K-N,!0}function f(z,J,Q){if(z.shape===E&&J.shape===E)return b(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.halfWidth,J.halfHeight,Q);if(z.shape===w&&J.shape===w)return d(z.x,z.y,z.radius,J.x,J.y,J.radius,Q);if(z.shape===E&&J.shape===w)return B(z.x,z.y,z.halfWidth,z.halfHeight,J.x,J.y,J.radius,Q);if(!B(J.x,J.y,J.halfWidth,J.halfHeight,z.x,z.y,z.radius,Q))return!1;return Q.normalX=-Q.normalX,Q.normalY=-Q.normalY,!0}var C=[];function p(){return{arr:[],gen:[],current:0}}var m=!1,r=50;function h(z,J,Q,j,$,O){if(j)n(z,J,Q,j,$,O);else l(z,J,$,O)}function l(z,J,Q,j){if(!m&&J>=r)m=!0,console.warn(`[ecspresso] Collision detection is using O(n²) brute force with ${J} colliders. For better performance, install createSpatialIndexPlugin() alongside your collision or physics2D plugin.`);for(let $=0;$<J;$++){let O=z[$];if(!O)continue;for(let K=$+1;K<J;K++){let Z=z[K];if(!Z)continue;if((O.collidesWithMask&Z.layerBit|Z.collidesWithMask&O.layerBit)===0)continue;if(!f(O,Z,S))continue;Q(O,Z,S,j)}}}function n(z,J,Q,j,$,O){let{arr:K,gen:Z}=Q,V=++Q.current;for(let G=0;G<J;G++){let U=z[G];if(!U)continue;let F=U.entityId;K[F]=U,Z[F]=V}for(let G=0;G<J;G++){let U=z[G];if(!U)continue;let F=U.shape===E?U.halfWidth:U.radius,k=U.shape===E?U.halfHeight:U.radius;C.length=0,j.queryRectInto(U.x-F,U.y-k,U.x+F,U.y+k,C,U.entityId);for(let N of C){if(Z[N]!==V)continue;let q=K[N];if(!q)continue;if((U.collidesWithMask&q.layerBit|q.collidesWithMask&U.layerBit)===0)continue;if(!f(U,q,S))continue;$(U,q,S,O)}}}function Qz(z,J){return{rigidBody:{type:z,mass:z==="static"?1/0:J?.mass??1,drag:J?.drag??0,restitution:J?.restitution??0,friction:J?.friction??0,gravityScale:J?.gravityScale??1},force:{x:0,y:0}}}function Zz(z,J){return{force:{x:z,y:J}}}function $z(z,J,Q,j){let $=z.getComponent(J,"force");if(!$)return;$.x+=Q,$.y+=j}function Kz(z,J,Q,j){let $=z.getComponent(J,"velocity"),O=z.getComponent(J,"rigidBody");if(!$||!O)return;if(O.mass===1/0||O.mass===0)return;$.x+=Q/O.mass,$.y+=j/O.mass}function Oz(z,J,Q,j){let $=z.getComponent(J,"velocity");if(!$)return;$.x=Q,$.y=j}var Y={entityA:0,entityB:0,normalX:0,normalY:0,depth:0};function c(z,J,Q,j){let $=z.rigidBody.type==="dynamic"&&z.rigidBody.mass>0&&z.rigidBody.mass!==1/0?1/z.rigidBody.mass:0,O=J.rigidBody.type==="dynamic"&&J.rigidBody.mass>0&&J.rigidBody.mass!==1/0?1/J.rigidBody.mass:0,K=$+O;if(K>0){let Z=Q.depth/K;if($>0){let F=j.getComponent(z.entityId,"localTransform");if(!F)return;let k=Z*$;F.x-=k*Q.normalX,F.y-=k*Q.normalY,z.x=F.x,z.y=F.y,j.markChanged(z.entityId,"localTransform")}if(O>0){let F=j.getComponent(J.entityId,"localTransform");if(!F)return;let k=Z*O;F.x+=k*Q.normalX,F.y+=k*Q.normalY,J.x=F.x,J.y=F.y,j.markChanged(J.entityId,"localTransform")}let V=J.velocity.x-z.velocity.x,G=J.velocity.y-z.velocity.y,U=V*Q.normalX+G*Q.normalY;if(U<0){let k=-(1+Math.min(z.rigidBody.restitution,J.rigidBody.restitution))*U/K;z.velocity.x-=k*$*Q.normalX,z.velocity.y-=k*$*Q.normalY,J.velocity.x+=k*O*Q.normalX,J.velocity.y+=k*O*Q.normalY;let N=V-U*Q.normalX,q=G-U*Q.normalY,D=Math.sqrt(N*N+q*q);if(D>0.000001){let T=N/D,L=q/D,X=Math.sqrt(z.rigidBody.friction*J.rigidBody.friction)*Math.abs(k),R=Math.min(D/K,X);z.velocity.x+=R*$*T,z.velocity.y+=R*$*L,J.velocity.x-=R*O*T,J.velocity.y-=R*O*L}}j.markChanged(z.entityId,"velocity"),j.markChanged(J.entityId,"velocity")}Y.entityA=z.entityId,Y.entityB=J.entityId,Y.normalX=Q.normalX,Y.normalY=Q.normalY,Y.depth=Q.depth,j.eventBus.publish("physicsCollision",Y)}function jz(z){let{gravity:J={x:0,y:0},systemGroup:Q="physics2D",collisionSystemGroup:j,integrationPriority:$=1000,collisionPriority:O=900,phase:K="fixedUpdate"}=z??{};return s("physics2D").withComponentTypes().withEventTypes().withResourceTypes().withLabels().withGroups().requires().install((Z)=>{Z.registerRequired("rigidBody","velocity",()=>({x:0,y:0})),Z.registerRequired("rigidBody","force",()=>({x:0,y:0})),Z.addResource("physicsConfig",{gravity:{x:J.x,y:J.y}}),Z.addSystem("physics2D-integration").setPriority($).inPhase(K).inGroup(Q).addQuery("bodies",{with:["localTransform","velocity","rigidBody","force"]}).setProcess(({queries:N,dt:q,ecs:D})=>{let{gravity:T}=D.getResource("physicsConfig"),L=T.x,P=T.y;for(let X of N.bodies){let{localTransform:R,velocity:H,rigidBody:M,force:W}=X.components;if(M.type==="static")continue;if(M.type==="dynamic"){let x=M.gravityScale*q;H.x+=L*x,H.y+=P*x;let I=M.mass;if(I>0&&I!==1/0){let _=q/I;H.x+=W.x*_,H.y+=W.y*_}if(M.drag>0){let _=Math.max(0,1-M.drag*q);H.x*=_,H.y*=_}}R.x+=H.x*q,R.y+=H.y*q,W.x=0,W.y=0,D.markChanged(X.id,"localTransform")}});let V=Z.addSystem("physics2D-collision").setPriority(O).inPhase(K).inGroup(Q);if(j)V.inGroup(j);let G=[],U=p(),F,k=!1;V.addQuery("collidables",{with:["localTransform","rigidBody","velocity","collisionLayer"]}).setProcess(({queries:N,ecs:q})=>{let D=0;for(let T of N.collidables){let{localTransform:L,rigidBody:P,velocity:X,collisionLayer:R}=T.components,H=q.getComponent(T.id,"aabbCollider"),M=H?void 0:q.getComponent(T.id,"circleCollider");if(!H&&!M)continue;let W=G[D];if(!W)W={entityId:T.id,x:L.x,y:L.y,layer:R.layer,collidesWith:R.collidesWith,layerBit:0,collidesWithMask:0,shape:E,halfWidth:0,halfHeight:0,radius:0,rigidBody:P,velocity:X},G[D]=W;else W.rigidBody=P,W.velocity=X;if(!g(W,T.id,L.x,L.y,R.layer,R.collidesWith,H,M))continue;D++}if(!k)F=q.tryGetResource("spatialIndex"),k=!0;h(G,D,U,F,c,q)})})}export{Oz as setVelocity,Qz as createRigidBody,jz as createPhysics2DPlugin,Zz as createForce,Kz as applyImpulse,$z as applyForce};
|
|
2
2
|
|
|
3
|
-
//# debugId=
|
|
3
|
+
//# debugId=07F3E3C2C1BC182564756E2164756E21
|
|
4
4
|
//# sourceMappingURL=physics2D.js.map
|