@flare-engine/physics 0.2.0 → 0.3.0
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/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +10 -5
- package/dist/index.d.mts +118 -110
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +118 -110
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +416 -425
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +404 -387
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -9
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { Rect, Vec2 } from
|
|
2
|
-
import { Signal } from
|
|
3
|
-
|
|
1
|
+
import { Rect, Vec2 } from "@flare-engine/math";
|
|
2
|
+
import { Signal } from "@flare-engine/events";
|
|
3
|
+
//#region src/shape.d.ts
|
|
4
4
|
/** AABB shape — axis-aligned bounding box. */
|
|
5
5
|
interface AABBShape {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
readonly type: 'aabb';
|
|
7
|
+
/** Half-width. */
|
|
8
|
+
readonly hw: number;
|
|
9
|
+
/** Half-height. */
|
|
10
|
+
readonly hh: number;
|
|
11
|
+
}
|
|
12
|
+
/** Circle shape. */
|
|
13
|
+
interface CircleShape {
|
|
14
|
+
readonly type: 'circle';
|
|
15
|
+
readonly radius: number;
|
|
11
16
|
}
|
|
12
17
|
/** Union of supported physics shapes. */
|
|
13
18
|
type Shape = AABBShape | CircleShape;
|
|
14
19
|
/** Create an AABB shape from width and height. */
|
|
15
20
|
declare function AABB(width: number, height: number): AABBShape;
|
|
16
|
-
/** Circle shape. */
|
|
17
|
-
interface CircleShape {
|
|
18
|
-
readonly type: 'circle';
|
|
19
|
-
readonly radius: number;
|
|
20
|
-
}
|
|
21
21
|
/** Create a circle shape from radius. */
|
|
22
22
|
declare function CircleShape(radius: number): CircleShape;
|
|
23
23
|
/** Compute the bounding rect of a shape at a given position. */
|
|
@@ -26,108 +26,84 @@ declare function shapeBounds(shape: Shape, x: number, y: number): Rect;
|
|
|
26
26
|
declare function shapeBoundsInto(shape: Shape, x: number, y: number, out: Rect): Rect;
|
|
27
27
|
/** Test overlap between two positioned shapes. Returns true if overlapping. */
|
|
28
28
|
declare function shapeOverlap(shapeA: Shape, ax: number, ay: number, shapeB: Shape, bx: number, by: number): boolean;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
* Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.
|
|
42
|
-
*/
|
|
43
|
-
declare class SpatialIndex<T> {
|
|
44
|
-
private entries;
|
|
45
|
-
/** Number of items in the index. */
|
|
46
|
-
get size(): number;
|
|
47
|
-
/** Insert an item with a bounding rect. */
|
|
48
|
-
insert(item: T, bounds: Rect): void;
|
|
49
|
-
/** Remove an item. */
|
|
50
|
-
remove(item: T): boolean;
|
|
51
|
-
/** Update the bounds for an existing item. */
|
|
52
|
-
update(item: T, bounds: Rect): void;
|
|
53
|
-
/** Query all items whose bounds overlap the given rect. */
|
|
54
|
-
query(bounds: Rect, result?: T[]): T[];
|
|
55
|
-
/** Remove all items. */
|
|
56
|
-
clear(): void;
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/collider.d.ts
|
|
31
|
+
/** Collider component data attached to an ECS entity. */
|
|
32
|
+
interface Collider {
|
|
33
|
+
/** The collision shape. */
|
|
34
|
+
shape: Shape;
|
|
35
|
+
/** If true, generates trigger events but no physical response. */
|
|
36
|
+
isTrigger: boolean;
|
|
37
|
+
/** Collision layer bitmask — which layer this collider belongs to. */
|
|
38
|
+
layer: number;
|
|
39
|
+
/** Collision mask — which layers this collider can interact with. */
|
|
40
|
+
mask: number;
|
|
57
41
|
}
|
|
58
|
-
|
|
42
|
+
/** Create a collider with default settings. */
|
|
43
|
+
declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/collision-world.d.ts
|
|
59
46
|
/** Collision pair data emitted by collision signals. */
|
|
60
47
|
interface CollisionPair {
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
readonly entityA: number;
|
|
49
|
+
readonly entityB: number;
|
|
63
50
|
}
|
|
64
51
|
/**
|
|
65
52
|
* AABB collision world. Manages entities with shapes, detects overlaps each step,
|
|
66
53
|
* and emits collision/trigger signals.
|
|
67
54
|
*/
|
|
68
55
|
declare class CollisionWorld {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
/** Collider component data attached to an ECS entity. */
|
|
101
|
-
interface Collider {
|
|
102
|
-
/** The collision shape. */
|
|
103
|
-
shape: Shape;
|
|
104
|
-
/** If true, generates trigger events but no physical response. */
|
|
105
|
-
isTrigger: boolean;
|
|
106
|
-
/** Collision layer bitmask — which layer this collider belongs to. */
|
|
107
|
-
layer: number;
|
|
108
|
-
/** Collision mask — which layers this collider can interact with. */
|
|
109
|
-
mask: number;
|
|
56
|
+
readonly onCollisionStart: Signal<CollisionPair>;
|
|
57
|
+
readonly onCollisionEnd: Signal<CollisionPair>;
|
|
58
|
+
readonly onTriggerEnter: Signal<CollisionPair>;
|
|
59
|
+
readonly onTriggerExit: Signal<CollisionPair>;
|
|
60
|
+
private entries;
|
|
61
|
+
private spatial;
|
|
62
|
+
/**
|
|
63
|
+
* Two pre-allocated pair buffers — swapped each step (no per-frame allocation).
|
|
64
|
+
* Keys are packed numbers via `encodePair(a, b)`.
|
|
65
|
+
*/
|
|
66
|
+
private pairBufA;
|
|
67
|
+
private pairBufB;
|
|
68
|
+
private activePairs;
|
|
69
|
+
/** Reusable query result buffer. */
|
|
70
|
+
private queryBuf;
|
|
71
|
+
/** Reusable bounds rect — avoids per-entity Rect allocation in step(). */
|
|
72
|
+
private boundsBuf;
|
|
73
|
+
/** Add an entity with a shape. */
|
|
74
|
+
add(entity: number, shape: Shape, x: number, y: number, isTrigger?: boolean, layer?: number, mask?: number): void;
|
|
75
|
+
/** Remove an entity from the world. Emits exit signals for any active pairs. */
|
|
76
|
+
remove(entity: number): void;
|
|
77
|
+
/** Update an entity's position. */
|
|
78
|
+
setPosition(entity: number, x: number, y: number): void;
|
|
79
|
+
/** Number of entities in the world. */
|
|
80
|
+
get size(): number;
|
|
81
|
+
/** Number of broadphase records (one per live entity after add/remove). Diagnostic for broadphase growth. */
|
|
82
|
+
get broadphaseSize(): number;
|
|
83
|
+
/** Run one collision detection step. */
|
|
84
|
+
step(): void;
|
|
85
|
+
/** Remove all entities. */
|
|
86
|
+
clear(): void;
|
|
110
87
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/raycast.d.ts
|
|
114
90
|
/** Result of a raycast hit. */
|
|
115
91
|
interface RaycastHit {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
92
|
+
/** The entity that was hit. */
|
|
93
|
+
entity: number;
|
|
94
|
+
/** Distance from ray origin to hit point. */
|
|
95
|
+
distance: number;
|
|
96
|
+
/** World-space hit point. */
|
|
97
|
+
point: Vec2;
|
|
98
|
+
/** Surface normal at hit point. */
|
|
99
|
+
normal: Vec2;
|
|
124
100
|
}
|
|
125
101
|
/** Entity entry for raycast — position + shape. */
|
|
126
102
|
interface RaycastTarget {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
103
|
+
entity: number;
|
|
104
|
+
shape: Shape;
|
|
105
|
+
x: number;
|
|
106
|
+
y: number;
|
|
131
107
|
}
|
|
132
108
|
/**
|
|
133
109
|
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
@@ -139,25 +115,26 @@ interface RaycastTarget {
|
|
|
139
115
|
* @param targets Array of entities to test against
|
|
140
116
|
*/
|
|
141
117
|
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
142
|
-
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/solid-grid.d.ts
|
|
143
120
|
/**
|
|
144
121
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
145
122
|
* `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here
|
|
146
123
|
* so callers can pass `extractColliders(map)` directly — no tilemap dep needed.
|
|
147
124
|
*/
|
|
148
125
|
interface SolidTile {
|
|
149
|
-
|
|
150
|
-
|
|
126
|
+
readonly row: number;
|
|
127
|
+
readonly col: number;
|
|
151
128
|
}
|
|
152
129
|
/**
|
|
153
130
|
* Precomputed flat-index Set of solid tile positions used by `isBlockedAABB`
|
|
154
131
|
* and `moveAndSlideInto`. Build once per map load via `buildSolidGrid`.
|
|
155
132
|
*/
|
|
156
133
|
interface SolidGrid {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
134
|
+
readonly width: number;
|
|
135
|
+
readonly height: number;
|
|
136
|
+
readonly tileSize: number;
|
|
137
|
+
readonly solid: Set<number>;
|
|
161
138
|
}
|
|
162
139
|
/**
|
|
163
140
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -174,9 +151,40 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
174
151
|
/**
|
|
175
152
|
* Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
|
|
176
153
|
* is applied independently so a blocked diagonal input slides along the wall.
|
|
177
|
-
* The y-check uses the already-updated x position
|
|
154
|
+
* The y-check uses the already-updated x position.
|
|
178
155
|
* Zero-alloc: writes result into the provided `out` Vec2 and returns it.
|
|
179
156
|
*/
|
|
180
157
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
181
|
-
|
|
182
|
-
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region src/spatial-index.d.ts
|
|
160
|
+
/** Item stored in the spatial index. */
|
|
161
|
+
interface SpatialEntry<T> {
|
|
162
|
+
readonly item: T;
|
|
163
|
+
/** Bounding rect (minX, minY, width, height). */
|
|
164
|
+
minX: number;
|
|
165
|
+
minY: number;
|
|
166
|
+
maxX: number;
|
|
167
|
+
maxY: number;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Brute-force spatial index for broadphase collision.
|
|
171
|
+
* Suitable for < ~1000 dynamic entities. Upgrade to grid/rbush for more.
|
|
172
|
+
*/
|
|
173
|
+
declare class SpatialIndex<T> {
|
|
174
|
+
private entries;
|
|
175
|
+
/** Number of items in the index. */
|
|
176
|
+
get size(): number;
|
|
177
|
+
/** Insert an item with a bounding rect. */
|
|
178
|
+
insert(item: T, bounds: Rect): void;
|
|
179
|
+
/** Remove an item. */
|
|
180
|
+
remove(item: T): boolean;
|
|
181
|
+
/** Update the bounds for an existing item. */
|
|
182
|
+
update(item: T, bounds: Rect): void;
|
|
183
|
+
/** Query all items whose bounds overlap the given rect. */
|
|
184
|
+
query(bounds: Rect, result?: T[]): T[];
|
|
185
|
+
/** Remove all items. */
|
|
186
|
+
clear(): void;
|
|
187
|
+
}
|
|
188
|
+
//#endregion
|
|
189
|
+
export { AABB, type AABBShape, CircleShape, type CircleShape as CircleShapeType, type Collider, type CollisionPair, CollisionWorld, type RaycastHit, type RaycastTarget, type Shape, type SolidGrid, type SolidTile, type SpatialEntry, SpatialIndex, buildSolidGrid, createCollider, isBlockedAABB, moveAndSlideInto, raycast, shapeBounds, shapeBoundsInto, shapeOverlap };
|
|
190
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/shape.ts","../src/collider.ts","../src/collision-world.ts","../src/raycast.ts","../src/solid-grid.ts","../src/spatial-index.ts"],"mappings":";;;;UAGiB;WACN;;WAEA;;WAEA;;;UAIM;WACN;WACA;;;KAIC,QAAQ,YAAY;;iBAGhB,KAAK,eAAe,iBAAiB;;iBAKrC,YAAY,iBAAiB;;iBAK7B,YAAY,OAAO,OAAO,WAAW,YAAY;;iBAajD,gBAAgB,OAAO,OAAO,WAAW,WAAW,KAAK,OAAO;;iBAgBhE,aACd,QAAQ,OACR,YACA,YACA,QAAQ,OACR,YACA;;;;UC/De;;EAEf,OAAO;;EAEP;;EAEA;;EAEA;;;iBAIc,eAAe,OAAO,OAAO,OAAO,QAAQ,KAAK,sBAAsB;;;;UCRtE;WACN;WACA;;;;;;cAoCE;WACF,kBAAgB,OAAA;WAChB,gBAAc,OAAA;WACd,gBAAc,OAAA;WACd,eAAa,OAAA;UAEd;UACA;;;;;UAKA;UACA;UACA;;UAEA;;UAEA;;EAGR,IACE,gBACA,OAAO,OACP,WACA,WACA,qBACA,gBACA;;EAcF,OAAO;;EAyBP,YAAY,gBAAgB,WAAW;;MAUnC;;MAKA;;EAKJ;;EAgEA;;;;;UChMe;;EAEf;;EAEA;;EAEA,OAAO;;EAEP,QAAQ;;;UAIO;EACf;EACA,OAAO;EACP;EACA;;;;;;;;;;;iBAYc,QACd,YACA,YACA,YACA,YACA,iBACA,kBAAkB,kBACjB;;;;;;;;UChCc;WACN;WACA;;;;;;UAOM;WACN;WACA;WACA;WACA,OAAO;;;;;;;iBAQF,eACd,eACA,gBACA,kBACA,gBAAgB,cACf;;;;;;iBAmBa,cACd,MAAM,WACN,WACA,WACA,eACA;;;;;;;iBA0Bc,iBACd,MAAM,WACN,WACA,WACA,YACA,YACA,eACA,eACA,KAAK,OACJ;;;;UCzFc,aAAa;WACnB,MAAM;;EAEf;EACA;EACA;EACA;;;;;;cAOW,aAAa;UAChB;;MAGJ;;EAKJ,OAAO,MAAM,GAAG,QAAQ;;EAWxB,OAAO,MAAM;;EAYb,OAAO,MAAM,GAAG,QAAQ;;EAWxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgBnC"}
|