@flare-engine/physics 0.2.1 → 0.3.1
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 +133 -110
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +133 -110
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +511 -425
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +499 -387
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Grzegorz Otto
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# @flare-engine/physics
|
|
2
2
|
|
|
3
|
-
Built-in AABB / Circle collision detection — `CollisionWorld` with enter/exit signals, `SpatialIndex` brute-force broadphase tuned for <1000 dynamic entities, `raycast` for line-vs-shape queries. Trigger / layer / mask filtering. Solid-tile grid movement: `buildSolidGrid` / `isBlockedAABB` / `moveAndSlideInto` (structural `SolidTile` input, no tilemap dep, half-open AABB-vs-tile, axis-separated slide, zero-alloc).
|
|
3
|
+
Built-in AABB / Circle collision detection — `CollisionWorld` with enter/exit signals, `SpatialIndex` brute-force broadphase tuned for <1000 dynamic entities, `raycast` for line-vs-shape queries. Trigger / layer / mask filtering. Solid-tile grid movement: `buildSolidGrid` / `isBlockedAABB` / `moveAndSlideInto` (structural `SolidTile` input, no tilemap dep, half-open AABB-vs-tile, axis-separated slide, zero-alloc). There is no third-party physics dependency: rigid-body response lives in the first-party [`@flare-engine/dynamics`](https://github.com/grzott/flare-engine/tree/main/packages/dynamics), and the planck.js option evaluated in the design docs was never adopted. Measured cost by collider count — with the capture date and run-to-run spread — is published in [benchmarks](https://github.com/grzott/flare-engine/blob/main/docs/references/benchmarks.md). Those rows time a whole physics frame (move every collider, then `step()`), not `step()` in isolation. Colliders wrap at a hard field boundary so density is constant for the whole sample window, and a probe fails the capture if one escapes; read the rows as an order-of-magnitude guide, which is where the `<1000` figure above comes from.
|
|
4
|
+
|
|
5
|
+
`@flare-engine/physics` answers *did these overlap*; `@flare-engine/dynamics` answers *where do they end up*. Neither package depends on the other — reach for [`@flare-engine/dynamics`](https://github.com/grzott/flare-engine/tree/main/packages/dynamics) when bodies need to push each other around, not just report contact.
|
|
4
6
|
|
|
5
7
|
## Install
|
|
6
8
|
|
|
@@ -13,16 +15,19 @@ bun add @flare-engine/physics
|
|
|
13
15
|
```ts
|
|
14
16
|
import { CollisionWorld, AABB, createCollider } from '@flare-engine/physics';
|
|
15
17
|
|
|
18
|
+
declare const entityA: number;
|
|
19
|
+
declare const x: number, y: number;
|
|
20
|
+
|
|
16
21
|
const world = new CollisionWorld();
|
|
17
|
-
world.onCollisionStart.add((pair) => console.log(pair.
|
|
22
|
+
world.onCollisionStart.add((pair) => console.log(pair.entityA, pair.entityB));
|
|
18
23
|
|
|
19
|
-
const colliderA = createCollider(
|
|
24
|
+
const colliderA = createCollider(AABB(32, 32), { layer: 1, mask: 2 });
|
|
20
25
|
world.add(entityA, colliderA.shape, x, y, false, colliderA.layer, colliderA.mask);
|
|
21
26
|
world.step();
|
|
22
27
|
```
|
|
23
28
|
|
|
24
|
-
See [API spec](
|
|
29
|
+
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-enginephysics--4kb-aabb).
|
|
25
30
|
|
|
26
31
|
## License
|
|
27
32
|
|
|
28
|
-
|
|
33
|
+
Apache-2.0
|
package/dist/index.d.mts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { Signal } from "@flare-engine/events";
|
|
2
|
+
import { Rect, Vec2 } from "@flare-engine/math";
|
|
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,88 @@ 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
|
-
|
|
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
|
+
/** Zero-alloc collider bounds — writes the AABB of entity's shape into `out`. Returns null when entity is absent. */
|
|
86
|
+
getColliderBoundsInto(entity: number, out: Rect): Rect | null;
|
|
87
|
+
/** Allocating convenience wrapper — returns a fresh Rect or null when entity is absent. */
|
|
88
|
+
getColliderBounds(entity: number): Rect | null;
|
|
89
|
+
/** Remove all entities. */
|
|
90
|
+
clear(): void;
|
|
98
91
|
}
|
|
99
|
-
|
|
100
|
-
|
|
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;
|
|
110
|
-
}
|
|
111
|
-
/** Create a collider with default settings. */
|
|
112
|
-
declare function createCollider(shape: Shape, opts?: Partial<Omit<Collider, 'shape'>>): Collider;
|
|
113
|
-
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/raycast.d.ts
|
|
114
94
|
/** Result of a raycast hit. */
|
|
115
95
|
interface RaycastHit {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
96
|
+
/** The entity that was hit. */
|
|
97
|
+
entity: number;
|
|
98
|
+
/** Distance from ray origin to hit point. */
|
|
99
|
+
distance: number;
|
|
100
|
+
/** World-space hit point. */
|
|
101
|
+
point: Vec2;
|
|
102
|
+
/** Surface normal at hit point. */
|
|
103
|
+
normal: Vec2;
|
|
124
104
|
}
|
|
125
105
|
/** Entity entry for raycast — position + shape. */
|
|
126
106
|
interface RaycastTarget {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
107
|
+
entity: number;
|
|
108
|
+
shape: Shape;
|
|
109
|
+
x: number;
|
|
110
|
+
y: number;
|
|
131
111
|
}
|
|
132
112
|
/**
|
|
133
113
|
* Cast a ray against a list of targets. Returns the closest hit, or null.
|
|
@@ -139,25 +119,26 @@ interface RaycastTarget {
|
|
|
139
119
|
* @param targets Array of entities to test against
|
|
140
120
|
*/
|
|
141
121
|
declare function raycast(ox: number, oy: number, dx: number, dy: number, maxDist: number, targets: readonly RaycastTarget[]): RaycastHit | null;
|
|
142
|
-
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/solid-grid.d.ts
|
|
143
124
|
/**
|
|
144
125
|
* Minimal structural type for a solid tile position in grid coordinates.
|
|
145
126
|
* `TileCollider[]` from `@flare-engine/tilemap` is structurally assignable here
|
|
146
127
|
* so callers can pass `extractColliders(map)` directly — no tilemap dep needed.
|
|
147
128
|
*/
|
|
148
129
|
interface SolidTile {
|
|
149
|
-
|
|
150
|
-
|
|
130
|
+
readonly row: number;
|
|
131
|
+
readonly col: number;
|
|
151
132
|
}
|
|
152
133
|
/**
|
|
153
134
|
* Precomputed flat-index Set of solid tile positions used by `isBlockedAABB`
|
|
154
135
|
* and `moveAndSlideInto`. Build once per map load via `buildSolidGrid`.
|
|
155
136
|
*/
|
|
156
137
|
interface SolidGrid {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
138
|
+
readonly width: number;
|
|
139
|
+
readonly height: number;
|
|
140
|
+
readonly tileSize: number;
|
|
141
|
+
readonly solid: Set<number>;
|
|
161
142
|
}
|
|
162
143
|
/**
|
|
163
144
|
* Build a `SolidGrid` from a list of solid tile positions.
|
|
@@ -174,9 +155,51 @@ declare function isBlockedAABB(grid: SolidGrid, x: number, y: number, halfW: num
|
|
|
174
155
|
/**
|
|
175
156
|
* Axis-separated slide movement. Attempts to move by (`dx`, `dy`); each axis
|
|
176
157
|
* is applied independently so a blocked diagonal input slides along the wall.
|
|
177
|
-
* The y-check uses the already-updated x position
|
|
158
|
+
* The y-check uses the already-updated x position.
|
|
178
159
|
* Zero-alloc: writes result into the provided `out` Vec2 and returns it.
|
|
179
160
|
*/
|
|
180
161
|
declare function moveAndSlideInto(grid: SolidGrid, x: number, y: number, dx: number, dy: number, halfW: number, halfH: number, out: Vec2): Vec2;
|
|
181
|
-
|
|
182
|
-
|
|
162
|
+
//#endregion
|
|
163
|
+
//#region src/spatial-index.d.ts
|
|
164
|
+
/** Item stored in the spatial index. */
|
|
165
|
+
interface SpatialEntry<T> {
|
|
166
|
+
readonly item: T;
|
|
167
|
+
/** Bounding rect (minX, minY, maxX, maxY). */
|
|
168
|
+
minX: number;
|
|
169
|
+
minY: number;
|
|
170
|
+
maxX: number;
|
|
171
|
+
maxY: number;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Uniform spatial-hash broadphase.
|
|
175
|
+
* `insert`, `remove`, and `update` are O(cells touched) — O(1) for typical entities.
|
|
176
|
+
* `query` walks only the cells overlapping the given rect; result order is unspecified.
|
|
177
|
+
* Zero-alloc in the query hot-path: duplicate suppression uses a monotone stamp counter,
|
|
178
|
+
* not a per-call Set.
|
|
179
|
+
*/
|
|
180
|
+
declare class SpatialIndex<T> {
|
|
181
|
+
private _data;
|
|
182
|
+
private _slots;
|
|
183
|
+
private _cells;
|
|
184
|
+
private _gen;
|
|
185
|
+
/** Number of items in the index. */
|
|
186
|
+
get size(): number;
|
|
187
|
+
/** Insert an item with a bounding rect. */
|
|
188
|
+
insert(item: T, bounds: Rect): void;
|
|
189
|
+
/** Remove an item. Returns false when the item was not present. */
|
|
190
|
+
remove(item: T): boolean;
|
|
191
|
+
/** Update the bounds for an existing item. No-op when the item is absent. */
|
|
192
|
+
update(item: T, bounds: Rect): void;
|
|
193
|
+
/**
|
|
194
|
+
* Query all items whose bounds overlap the given rect.
|
|
195
|
+
* Result order is unspecified. Appends to `result` when provided and returns it.
|
|
196
|
+
*/
|
|
197
|
+
query(bounds: Rect, result?: T[]): T[];
|
|
198
|
+
/** Remove all items. */
|
|
199
|
+
clear(): void;
|
|
200
|
+
private _link;
|
|
201
|
+
private _unlink;
|
|
202
|
+
}
|
|
203
|
+
//#endregion
|
|
204
|
+
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 };
|
|
205
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","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;;EAiEA,sBAAsB,gBAAgB,KAAK,OAAO;;EAQlD,kBAAkB,iBAAiB;;EAOnC;;;;;UChNe;;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;;;;;;;;;cAuCW,aAAa;UAChB;UACA;UACA;UACA;;MAGJ;;EAKJ,OAAO,MAAM,GAAG,QAAQ;;EAexB,OAAO,MAAM;;EAgBb,OAAO,MAAM,GAAG,QAAQ;;;;;EAgBxB,MAAM,QAAQ,MAAM,SAAS,MAAM;;EAgCnC;UAOQ;UAmBA"}
|