@margoengine/physics2d 0.0.0-stage → 1.0.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 -0
- package/NOTICE +4 -0
- package/README.md +565 -2
- package/dist/articulation.d.ts +103 -0
- package/dist/body.d.ts +388 -0
- package/dist/character.d.ts +86 -0
- package/dist/clock.d.ts +2 -0
- package/dist/composites.d.ts +161 -0
- package/dist/composites.js +183 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +2640 -0
- package/dist/joint.d.ts +229 -0
- package/dist/kernel-DkjP8dvA.js +159 -0
- package/dist/kernel.d.ts +107 -0
- package/dist/mesh-clean.d.ts +17 -0
- package/dist/outline.d.ts +55 -0
- package/dist/outline.js +222 -0
- package/dist/physics.d.ts +616 -0
- package/dist/physics2d-scalar.wasm +0 -0
- package/dist/physics2d.wasm +0 -0
- package/dist/pid.d.ts +36 -0
- package/dist/shapes.d.ts +215 -0
- package/dist/tilemap.d.ts +84 -0
- package/dist/vehicle.d.ts +121 -0
- package/dist/vehicle.js +178 -0
- package/dist/wasm-load.d.ts +21 -0
- package/package.json +62 -4
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Body2D } from './body.js';
|
|
2
|
+
import type { Joint2D, MotorOptions2D } from './joint.js';
|
|
3
|
+
import type { Physics2D } from './physics.js';
|
|
4
|
+
import type { Vec2D } from './shapes.js';
|
|
5
|
+
/** One link of an articulation: a body joined to its parent by a revolute or prismatic joint. */
|
|
6
|
+
export interface ArticulationLink2D {
|
|
7
|
+
body: Body2D;
|
|
8
|
+
/** Index in the list of the link this one hangs from; -1 or left out for the root. Must come before this link. */
|
|
9
|
+
parent?: number;
|
|
10
|
+
joint: {
|
|
11
|
+
type: 'revolute' | 'prismatic';
|
|
12
|
+
/** The joint's anchor in the parent's frame, world units (default the parent's origin). */
|
|
13
|
+
anchorParent?: Vec2D;
|
|
14
|
+
/** The joint's anchor in this link's frame (default its origin). */
|
|
15
|
+
anchorChild?: Vec2D;
|
|
16
|
+
/** The slide axis in the parent's frame (prismatic only). */
|
|
17
|
+
axis?: Vec2D;
|
|
18
|
+
/** Radians (revolute) or world units (prismatic). */
|
|
19
|
+
limits?: readonly [number, number];
|
|
20
|
+
motor?: MotorOptions2D;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** Where a link is, in world units and radians. */
|
|
24
|
+
export interface LinkPose2D {
|
|
25
|
+
position: [number, number];
|
|
26
|
+
rotation: number;
|
|
27
|
+
}
|
|
28
|
+
/** Options of {@link Articulation2D.solveIK}. */
|
|
29
|
+
export interface IKOptions2D {
|
|
30
|
+
/** Most iterations (default 64). */
|
|
31
|
+
iterations?: number;
|
|
32
|
+
/** Stop when the point is this close to the target, world units (default 0.01). */
|
|
33
|
+
tolerance?: number;
|
|
34
|
+
/** Damping of the least-squares step: larger is steadier near a straight arm and slower (default 0.5). */
|
|
35
|
+
damping?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A tree of bodies joined by one-degree-of-freedom joints, with the joint space of the whole made explicit: the joint
|
|
39
|
+
* positions and velocities, motors and limits on each joint, forward kinematics and inverse kinematics. The bodies stay
|
|
40
|
+
* ordinary bodies of the world, and the kernel's joints keep them together: a chain solves exactly (block elimination, the
|
|
41
|
+
* constraint form of Featherstone's articulated-body result: "Rigid Body Dynamics Algorithms", Springer 2008, chapter 7) so a
|
|
42
|
+
* heavy end does not stretch it, and contacts of any link act on the whole.
|
|
43
|
+
* @example
|
|
44
|
+
* const arm = physics.addArticulation(base, [
|
|
45
|
+
* { body: upper, joint: { type: 'revolute', anchorParent: [0, 0], anchorChild: [-20, 0], limits: [-2, 2] } },
|
|
46
|
+
* { body: lower, parent: 0, joint: { type: 'revolute', anchorParent: [20, 0], anchorChild: [-20, 0] } },
|
|
47
|
+
* ]);
|
|
48
|
+
* arm.solveIK(1, [20, 0], [60, 10]); // joint positions that put the lower arm's tip there
|
|
49
|
+
*/
|
|
50
|
+
export declare class Articulation2D {
|
|
51
|
+
private readonly physics;
|
|
52
|
+
readonly root: Body2D;
|
|
53
|
+
/** The joints, one per link, in link order. */
|
|
54
|
+
readonly joints: Joint2D[];
|
|
55
|
+
private readonly links;
|
|
56
|
+
private removed;
|
|
57
|
+
/** @internal */
|
|
58
|
+
constructor(physics: Physics2D, root: Body2D, links: readonly ArticulationLink2D[]);
|
|
59
|
+
/** The number of joints, which is the number of degrees of freedom. */
|
|
60
|
+
get dof(): number;
|
|
61
|
+
private bodyOf;
|
|
62
|
+
private check;
|
|
63
|
+
/** The joint positions now, in link order: radians for a revolute joint (child turn less parent turn), world units along the axis for a prismatic one. */
|
|
64
|
+
positions(): number[];
|
|
65
|
+
/** The joint velocities now: the relative angular speed, or the speed along the axis. */
|
|
66
|
+
velocities(): number[];
|
|
67
|
+
private world;
|
|
68
|
+
private pointVelocity;
|
|
69
|
+
/** Where every link is when the joints are at `q`, from the root's pose now. */
|
|
70
|
+
forwardKinematics(q?: readonly number[]): LinkPose2D[];
|
|
71
|
+
/** The world position of `local` (a point in link `link`'s frame) when the joints are at `q`. */
|
|
72
|
+
pointAt(link: number, local: Vec2D, q?: readonly number[]): [number, number];
|
|
73
|
+
private checkLink;
|
|
74
|
+
/** The 2 by dof Jacobian of the point `local` of link `link`: how it moves, in world units per unit of each joint, at `q`. */
|
|
75
|
+
jacobian(link: number, local: Vec2D, q?: readonly number[]): [number[], number[]];
|
|
76
|
+
/**
|
|
77
|
+
* Joint positions that put the point `local` of link `link` on `target`, by damped least squares from the joints' present
|
|
78
|
+
* positions (Wampler 1986, Nakamura and Hanafusa 1986), kept inside the joints' limits. Returns the positions and whether
|
|
79
|
+
* the point got within `tolerance`; nothing moves until you `setPositions` or `driveTo`.
|
|
80
|
+
*/
|
|
81
|
+
solveIK(link: number, local: Vec2D, target: Vec2D, options?: IKOptions2D): {
|
|
82
|
+
q: number[];
|
|
83
|
+
reached: boolean;
|
|
84
|
+
error: number;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Puts the links where the joints at `q` would, now: each body is moved (as `teleport` does) and keeps its velocity. The
|
|
88
|
+
* root stays where it is. Joint limits are not applied; give positions inside them.
|
|
89
|
+
*/
|
|
90
|
+
setPositions(q: readonly number[]): this;
|
|
91
|
+
/** Sets the position motor of every joint toward `q`, with the stiffness and damping given (defaults 200 and 20), so the arm moves there under the physics. */
|
|
92
|
+
driveTo(q: readonly number[], { stiffness, damping, maxForce }?: {
|
|
93
|
+
stiffness?: number;
|
|
94
|
+
damping?: number;
|
|
95
|
+
maxForce?: number;
|
|
96
|
+
}): this;
|
|
97
|
+
/** Sets one joint's motor. */
|
|
98
|
+
setMotor(joint: number, motor: MotorOptions2D): this;
|
|
99
|
+
/** Sets one joint's limits (radians or world units). */
|
|
100
|
+
setLimits(joint: number, min: number, max: number): this;
|
|
101
|
+
/** Removes the articulation's joints; the bodies stay. */
|
|
102
|
+
remove(): void;
|
|
103
|
+
}
|
package/dist/body.d.ts
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import type { Kernel } from './kernel.js';
|
|
2
|
+
import { type CoefficientCombineRule2D, describeShape, type ActiveHooks2D, type ColliderOptions2D, type Described, type ShapeOptions2D, type Vec2D } from './shapes.js';
|
|
3
|
+
/** How a body moves: `dynamic` (simulated), `fixed`, or kinematic (driven by position or by velocity). */
|
|
4
|
+
export type BodyType2D = 'dynamic' | 'fixed' | 'kinematicPosition' | 'kinematicVelocity';
|
|
5
|
+
/** Anything with a position and a rotation in world units and radians: a `Sprite` from `@margoengine/sprites` fits. */
|
|
6
|
+
export interface SpriteLike {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
rotation: number;
|
|
10
|
+
/** With `rect`, the default collider is a box of that size around the pivot. */
|
|
11
|
+
readonly rect?: readonly [number, number, number, number];
|
|
12
|
+
readonly pivotX?: number;
|
|
13
|
+
readonly pivotY?: number;
|
|
14
|
+
readonly scaleX?: number;
|
|
15
|
+
readonly scaleY?: number;
|
|
16
|
+
}
|
|
17
|
+
/** Options for adding a body; every field has a default. */
|
|
18
|
+
export interface BodyOptions2D {
|
|
19
|
+
/** Default 'dynamic'. */
|
|
20
|
+
type?: BodyType2D;
|
|
21
|
+
/** World2D position. Defaults to the sprite's position, or [0, 0]. */
|
|
22
|
+
position?: Vec2D;
|
|
23
|
+
/** Radians, clockwise on screen. Defaults to the sprite's rotation, or 0. */
|
|
24
|
+
rotation?: number;
|
|
25
|
+
/** One or more colliders. Without any, a box fitted to the sprite's `rect`, else none. */
|
|
26
|
+
colliders?: ColliderOptions2D | readonly ColliderOptions2D[];
|
|
27
|
+
linearVelocity?: Vec2D;
|
|
28
|
+
angularVelocity?: number;
|
|
29
|
+
linearDamping?: number;
|
|
30
|
+
angularDamping?: number;
|
|
31
|
+
gravityScale?: number;
|
|
32
|
+
/** Continuous collision detection for fast bodies. */
|
|
33
|
+
ccd?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Soft CCD: how far ahead, in world units, the body's contacts reach, so the solver holds a fast body at what it would
|
|
36
|
+
* hit one step on; set it to about the body's speed times the step. Default 0: the world's own margin, 0.02 m.
|
|
37
|
+
*/
|
|
38
|
+
softCcd?: number;
|
|
39
|
+
canSleep?: boolean;
|
|
40
|
+
lockRotation?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* In a contact with a body of lower dominance, this body acts as if it had infinite mass. Integers from -127 to 127,
|
|
43
|
+
* default 0.
|
|
44
|
+
*/
|
|
45
|
+
dominance?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Holds the body's translation on an axis: `true` locks both, `[x, y]` picks. The body stays where it is on a locked
|
|
48
|
+
* axis and moves freely on the other (it still turns unless `lockRotation`).
|
|
49
|
+
*/
|
|
50
|
+
lockTranslation?: boolean | readonly [boolean, boolean];
|
|
51
|
+
/** Mass in kilograms added to what the colliders weigh; the body turns as a heavier body of the same build. Default 0. */
|
|
52
|
+
additionalMass?: number;
|
|
53
|
+
userData?: unknown;
|
|
54
|
+
}
|
|
55
|
+
/** @internal What a body needs from its world. */
|
|
56
|
+
export interface BodyOwner2D {
|
|
57
|
+
readonly kernel: Kernel;
|
|
58
|
+
readonly handle: number;
|
|
59
|
+
readonly scale: number;
|
|
60
|
+
addCollider(body: Body2D, options: ColliderOptions2D): Collider2D;
|
|
61
|
+
removeCollider(collider: Collider2D): void;
|
|
62
|
+
replaceColliderShapes(collider: Collider2D, shape: ShapeOptions2D, parts: Described[]): void;
|
|
63
|
+
removeBody(body: Body2D): void;
|
|
64
|
+
lockTranslation(body: Body2D, x: boolean, y: boolean): void;
|
|
65
|
+
retype(body: Body2D, type: BodyType2D): void;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* A collider: one or more kernel shapes (a concave polygon or a polyline is several) on a body, with the settings that
|
|
69
|
+
* can change after it exists. Positions are in world units.
|
|
70
|
+
*/
|
|
71
|
+
export declare class Collider2D {
|
|
72
|
+
private readonly owner;
|
|
73
|
+
readonly body: Body2D;
|
|
74
|
+
private readonly settings;
|
|
75
|
+
private offset;
|
|
76
|
+
private turn;
|
|
77
|
+
/** The kernel shapes this collider is made of. @internal */
|
|
78
|
+
readonly shapes: number[];
|
|
79
|
+
/** Stable identifier for collider-pair tracking across multi-shape swaps. @internal */
|
|
80
|
+
readonly uid: number;
|
|
81
|
+
/** The shape as the kernel was given it, for the queries a character controller makes. @internal */
|
|
82
|
+
described: Described | null;
|
|
83
|
+
/** The options it was made with (its shape and settings), to describe it again where it moves. @internal */
|
|
84
|
+
options: ColliderOptions2D | null;
|
|
85
|
+
private removed;
|
|
86
|
+
private enabled;
|
|
87
|
+
private savedGroups;
|
|
88
|
+
private explicitMass;
|
|
89
|
+
/** @internal */
|
|
90
|
+
constructor(owner: BodyOwner2D, body: Body2D, shapes: number[], settings: Uint32Array, offset: readonly [number, number], turn: number, uid?: number);
|
|
91
|
+
/** @internal Current settings words shared across all kernel shapes of this collider. */
|
|
92
|
+
get rawSettings(): Uint32Array;
|
|
93
|
+
/** @internal Re-applies explicit mass or mass properties after kernel shapes are replaced. */
|
|
94
|
+
reapplyMass(): void;
|
|
95
|
+
/** The kernel's handle of its first shape: unique among live colliders. */
|
|
96
|
+
get handle(): number;
|
|
97
|
+
/** The body this collider is on. */
|
|
98
|
+
parent(): Body2D;
|
|
99
|
+
get isRemoved(): boolean;
|
|
100
|
+
/** @internal */
|
|
101
|
+
markRemoved(): void;
|
|
102
|
+
private push;
|
|
103
|
+
/**
|
|
104
|
+
* Changes the collider's outline after it is made (Rapier's `setShape`): any shape a collider takes
|
|
105
|
+
* (`circle`, `box`, `capsule`, `segment`, `triangle`, convex or concave `polygon`, `polyline`, `trimesh`,
|
|
106
|
+
* `heightfield`, `compound`, `voxels` or `halfspace`), placed at the collider's own offset and turn.
|
|
107
|
+
* Its material, density, mass mode, filtering and flags stay; the body's mass follows the new outline, and
|
|
108
|
+
* pairs already touching are carried across the swap without raising a duplicate start event.
|
|
109
|
+
* @throws RangeError for an invalid shape description; MargoError (state) once the collider was removed.
|
|
110
|
+
*/
|
|
111
|
+
setShape(shape: ShapeOptions2D): void;
|
|
112
|
+
isSensor(): boolean;
|
|
113
|
+
/** A sensor reports overlaps (`intersectionstart`) and pushes nothing. It also senses fixed and kinematic bodies. */
|
|
114
|
+
setSensor(sensor: boolean): void;
|
|
115
|
+
/**
|
|
116
|
+
* Which questions `Physics2D.hooks` is asked about this collider's pairs (Rapier's `setActiveHooks`): `filterPair` for a pair of
|
|
117
|
+
* solids, `filterIntersection` for a pair with a sensor in it, `modifyContact` for a touching contact. An omitted key is turned off.
|
|
118
|
+
*/
|
|
119
|
+
setActiveHooks(hooks: ActiveHooks2D): void;
|
|
120
|
+
/** The questions the hooks are asked about this collider's pairs, as {@link Collider2D.setActiveHooks} sets them. */
|
|
121
|
+
get activeHooks(): Required<ActiveHooks2D>;
|
|
122
|
+
setFriction(value: number): void;
|
|
123
|
+
/** Sets the coefficient of rolling resistance (see `ColliderSettings2D.rollingResistance`). */
|
|
124
|
+
setRollingResistance(value: number): void;
|
|
125
|
+
/** The coefficient of rolling resistance. */
|
|
126
|
+
rollingResistance(): number;
|
|
127
|
+
setRestitution(value: number): void;
|
|
128
|
+
/** How a contact combines this collider's friction with the other's (Rapier's `setFrictionCombineRule`). */
|
|
129
|
+
setFrictionCombineRule(rule: CoefficientCombineRule2D): void;
|
|
130
|
+
frictionCombineRule(): CoefficientCombineRule2D;
|
|
131
|
+
/** How a contact combines this collider's restitution with the other's (Rapier's `setRestitutionCombineRule`). */
|
|
132
|
+
setRestitutionCombineRule(rule: CoefficientCombineRule2D): void;
|
|
133
|
+
restitutionCombineRule(): CoefficientCombineRule2D;
|
|
134
|
+
setDensity(value: number): void;
|
|
135
|
+
/**
|
|
136
|
+
* Gives the collider a mass of its own, kilograms, whatever its outline's area (Rapier's `setMass`): its centre of mass
|
|
137
|
+
* stays the outline's and its inertia is the outline's at that mass, and it holds through `setShape`. A collider of
|
|
138
|
+
* several kernel shapes shares it among them by area. `setDensity` takes it back to its density.
|
|
139
|
+
* @throws RangeError for a mass below 0 or not finite.
|
|
140
|
+
*/
|
|
141
|
+
setMass(kilograms: number): void;
|
|
142
|
+
/**
|
|
143
|
+
* Gives the collider its mass, centre of mass and inertia outright (Rapier's `setMassProperties`): `mass` in kilograms,
|
|
144
|
+
* `centre` in the collider's own frame (world units) and `inertia` in kg m^2 in world units about that centre. They hold
|
|
145
|
+
* through `setShape` and a move on the body; `setDensity` or `setMass` replaces them.
|
|
146
|
+
* @throws RangeError for a number that is not finite, or a mass or inertia below 0.
|
|
147
|
+
*/
|
|
148
|
+
setMassProperties(mass: number, centre?: Vec2D, inertia?: number): void;
|
|
149
|
+
/** The collider's mass, kilograms: from its density, or as set. */
|
|
150
|
+
mass(): number;
|
|
151
|
+
/** The mass properties given outright, kept so a move on the body takes their centre with it. */
|
|
152
|
+
private massProps;
|
|
153
|
+
private live;
|
|
154
|
+
/**
|
|
155
|
+
* Which kinds of body pair this collider takes part in, as the bits of `activeCollisionTypes` (Rapier's
|
|
156
|
+
* `setActiveCollisionTypes`): 1 dynamic with dynamic, 2 dynamic with kinematic, 4 dynamic with fixed (the default 7),
|
|
157
|
+
* 8 kinematic with kinematic, 16 kinematic with fixed, 32 fixed with fixed. A sensor sees all six (63).
|
|
158
|
+
*/
|
|
159
|
+
setActiveCollisionTypes(types: number): void;
|
|
160
|
+
/** The pair kinds this collider takes part in, as {@link Collider2D.setActiveCollisionTypes} reads them. */
|
|
161
|
+
activeCollisionTypes(): number;
|
|
162
|
+
/**
|
|
163
|
+
* Chooses which events this collider asks for, Rapier's `setActiveEvents`: `collision` for `contactstart`,
|
|
164
|
+
* `contactend` and the intersection events, `contactForce` for `contactforce` (past `setContactForceEventThreshold`).
|
|
165
|
+
* What you leave out stays.
|
|
166
|
+
*/
|
|
167
|
+
setActiveEvents(events: {
|
|
168
|
+
collision?: boolean;
|
|
169
|
+
contactForce?: boolean;
|
|
170
|
+
}): void;
|
|
171
|
+
/** Which events this collider asks for. */
|
|
172
|
+
activeEvents(): {
|
|
173
|
+
collision: boolean;
|
|
174
|
+
contactForce: boolean;
|
|
175
|
+
};
|
|
176
|
+
/** The newtons a contact's force must pass to raise a `contactforce` event (Rapier's `setContactForceEventThreshold`); it also asks for the event. */
|
|
177
|
+
/** Rapier's `setContactSkin`: the gap, world units, the solver holds this collider off what it touches (0 for none). */
|
|
178
|
+
setContactSkin(distance: number): void;
|
|
179
|
+
contactSkin(): number;
|
|
180
|
+
setContactForceEventThreshold(newtons: number): void;
|
|
181
|
+
/** Packed interaction groups, see `interactionGroups`. */
|
|
182
|
+
setCollisionGroups(groups: number): void;
|
|
183
|
+
setSolverGroups(groups: number): void;
|
|
184
|
+
/** A disabled collider meets nothing, reports nothing and is skipped by queries; its mass still counts. */
|
|
185
|
+
setEnabled(enabled: boolean): void;
|
|
186
|
+
get isEnabled(): boolean;
|
|
187
|
+
/** The collider's centre in world units, at the body's latest step. */
|
|
188
|
+
translation(): [number, number];
|
|
189
|
+
/** The collider's rotation in radians, at the body's latest step. */
|
|
190
|
+
rotation(): number;
|
|
191
|
+
/** Its position on its body, world units in the body's frame (Rapier's `translationWrtParent`). */
|
|
192
|
+
translationWrtParent(): [number, number];
|
|
193
|
+
/** Its rotation on its body, radians (Rapier's `rotationWrtParent`). */
|
|
194
|
+
rotationWrtParent(): number;
|
|
195
|
+
/** Moves it on its body to `offset`, world units in the body's frame (Rapier's `setTranslationWrtParent`). */
|
|
196
|
+
setTranslationWrtParent(offset: Vec2D): void;
|
|
197
|
+
/** Turns it on its body to `angle` radians (Rapier's `setRotationWrtParent`). */
|
|
198
|
+
setRotationWrtParent(angle: number): void;
|
|
199
|
+
/**
|
|
200
|
+
* Moves the collider's kernel shapes from where they are on the body to `offset` and `turn`: each turns by the
|
|
201
|
+
* difference of the angles and shifts by the new offset less the old one turned. The body's mass and centre of mass
|
|
202
|
+
* follow, and its contacts are built again from where it went.
|
|
203
|
+
*/
|
|
204
|
+
private place;
|
|
205
|
+
/** Removes the collider from its body. */
|
|
206
|
+
remove(): void;
|
|
207
|
+
}
|
|
208
|
+
/** A rigid body in the kernel, bound to an optional sprite. Positions are in world units, y down. */
|
|
209
|
+
export declare class Body2D {
|
|
210
|
+
private readonly owner;
|
|
211
|
+
/** The kernel's handle. @internal */
|
|
212
|
+
readonly handle: number;
|
|
213
|
+
readonly sprite: SpriteLike | null;
|
|
214
|
+
private kind;
|
|
215
|
+
/** The colliders on this body. */
|
|
216
|
+
readonly colliders: Collider2D[];
|
|
217
|
+
userData: unknown;
|
|
218
|
+
/** @internal previous (0..2) and current (3..5) step poses: x, y, angle. */
|
|
219
|
+
readonly pose: Float64Array<ArrayBuffer>;
|
|
220
|
+
/** @internal */
|
|
221
|
+
dirty: boolean;
|
|
222
|
+
private removed;
|
|
223
|
+
private extraMass;
|
|
224
|
+
/** @internal */
|
|
225
|
+
constructor(owner: BodyOwner2D,
|
|
226
|
+
/** The kernel's handle. @internal */
|
|
227
|
+
handle: number, sprite: SpriteLike | null, kind: BodyType2D);
|
|
228
|
+
/** @internal Stores the current pose as the newest step. */
|
|
229
|
+
snapshot(): void;
|
|
230
|
+
/** @internal */
|
|
231
|
+
store(x: number, y: number, c: number, s: number, k: number): void;
|
|
232
|
+
/** How the body moves: `dynamic`, `fixed`, `kinematicPosition` or `kinematicVelocity`. */
|
|
233
|
+
get type(): BodyType2D;
|
|
234
|
+
/**
|
|
235
|
+
* Changes how the body moves. A dynamic body is weighed again from its colliders; a fixed body stops. A body locked on an
|
|
236
|
+
* axis keeps its lock only while it is dynamic or kinematic.
|
|
237
|
+
*/
|
|
238
|
+
setType(type: BodyType2D): this;
|
|
239
|
+
/** @internal Damping, gravity scale, sleep permission, ccd and dominance as the kernel holds them. */
|
|
240
|
+
params: {
|
|
241
|
+
linearDamping: number;
|
|
242
|
+
angularDamping: number;
|
|
243
|
+
gravityScale: number;
|
|
244
|
+
canSleep: boolean;
|
|
245
|
+
ccd: boolean;
|
|
246
|
+
dominance: number;
|
|
247
|
+
};
|
|
248
|
+
private setParams;
|
|
249
|
+
get linearDamping(): number;
|
|
250
|
+
set linearDamping(value: number);
|
|
251
|
+
get angularDamping(): number;
|
|
252
|
+
set angularDamping(value: number);
|
|
253
|
+
get gravityScale(): number;
|
|
254
|
+
set gravityScale(value: number);
|
|
255
|
+
get canSleep(): boolean;
|
|
256
|
+
set canSleep(value: boolean);
|
|
257
|
+
/** Continuous collision for a fast body. */
|
|
258
|
+
get ccd(): boolean;
|
|
259
|
+
set ccd(value: boolean);
|
|
260
|
+
/** Soft CCD reach in world units, see `BodyOptions2D.softCcd`. */
|
|
261
|
+
get softCcd(): number;
|
|
262
|
+
set softCcd(value: number);
|
|
263
|
+
private soft;
|
|
264
|
+
get dominance(): number;
|
|
265
|
+
set dominance(value: number);
|
|
266
|
+
private check;
|
|
267
|
+
private get k();
|
|
268
|
+
get isRemoved(): boolean;
|
|
269
|
+
/** @internal */
|
|
270
|
+
markRemoved(): void;
|
|
271
|
+
/** Position at the latest step. */
|
|
272
|
+
get position(): [number, number];
|
|
273
|
+
/** Rotation at the latest step, radians. */
|
|
274
|
+
get rotation(): number;
|
|
275
|
+
/** Moves the body now with no interpolation sweep. For a kinematic position body, use `moveTo`. */
|
|
276
|
+
teleport(position: Vec2D, rotation?: number): this;
|
|
277
|
+
/** Sets where a kinematic position body should be after the next step. */
|
|
278
|
+
moveTo(position: Vec2D, rotation?: number): this;
|
|
279
|
+
private read;
|
|
280
|
+
/** Velocity in world units per second. */
|
|
281
|
+
get linearVelocity(): [number, number];
|
|
282
|
+
set linearVelocity(value: Vec2D);
|
|
283
|
+
/** Radians per second. */
|
|
284
|
+
get angularVelocity(): number;
|
|
285
|
+
set angularVelocity(value: number);
|
|
286
|
+
/** Impulse in kernel units (kilogram metres per second); a body of mass m gains impulse / m metres per second. */
|
|
287
|
+
applyImpulse(impulse: Vec2D): this;
|
|
288
|
+
/** Impulse applied at a world point (world units), which also spins the body. */
|
|
289
|
+
applyImpulseAtPoint(impulse: Vec2D, point: Vec2D): this;
|
|
290
|
+
applyTorqueImpulse(torque: number): this;
|
|
291
|
+
/** Force in kernel units (newtons), held until `resetForces`: it acts every step. */
|
|
292
|
+
applyForce(force: Vec2D): this;
|
|
293
|
+
/** A torque held until `resetForces`. */
|
|
294
|
+
applyTorque(torque: number): this;
|
|
295
|
+
/** A force held until `resetForces`, applied at a world point: it pushes the body and turns it by the arm from the centre of mass. */
|
|
296
|
+
applyForceAtPoint(force: Vec2D, point: Vec2D): this;
|
|
297
|
+
/** Clears the torque that acts every step and keeps the force; `resetForces` clears both. */
|
|
298
|
+
resetTorques(): this;
|
|
299
|
+
/** Clears the forces and torques that act every step. */
|
|
300
|
+
resetForces(): this;
|
|
301
|
+
get mass(): number;
|
|
302
|
+
/**
|
|
303
|
+
* Moment of inertia about the centre of mass, in kilogram square world units (Rapier's `principalInertia`): what turns the
|
|
304
|
+
* body as `mass` is what moves it. 0 for a fixed or kinematic body.
|
|
305
|
+
*/
|
|
306
|
+
get inertia(): number;
|
|
307
|
+
/** @internal The added mass a restored snapshot carries. */
|
|
308
|
+
restoreAdditionalMass(value: number): void;
|
|
309
|
+
/** Mass added to what the colliders weigh, in kilograms. */
|
|
310
|
+
get additionalMass(): number;
|
|
311
|
+
set additionalMass(value: number);
|
|
312
|
+
/**
|
|
313
|
+
* Whether the body is in the simulation (default true). Disabled, it keeps its pose and velocity, moves and wakes nothing, touches
|
|
314
|
+
* nothing (its contacts end), is found by no query, and its joints rest; enabled again, it goes on with the velocity it had. Rapier's
|
|
315
|
+
* `setEnabled`: its API reference says only that a collider disabled is "excluded from all collision detection and physics", and
|
|
316
|
+
* says nothing of a body's, so this follows what Rapier 2D does when run (the body's colliders read disabled, nothing touches
|
|
317
|
+
* them, the pose and velocity stay, the body goes on at that velocity when enabled), and what the 3D kernel's disabled body does.
|
|
318
|
+
*/
|
|
319
|
+
get enabled(): boolean;
|
|
320
|
+
set enabled(value: boolean);
|
|
321
|
+
/**
|
|
322
|
+
* The most the body may turn at, in radians a second (default 0: no limit). Rapier caps every body's spin by default to keep its
|
|
323
|
+
* continuous collision reliable, and its `allowFastRotation` lifts the cap; here a fast spinner with `ccd` set is swept along its turn
|
|
324
|
+
* (it is stopped by a thin wall at 2000 rad/s, `kernel/tests/ccd.rs`), so there is no default cap and `allowFastRotation` has nothing to
|
|
325
|
+
* lift. Set this to cap a body as Rapier does. Applied each substep before the solve.
|
|
326
|
+
* @throws RangeError for a speed below 0 or not finite.
|
|
327
|
+
*/
|
|
328
|
+
set maxAngularSpeed(value: number);
|
|
329
|
+
get maxAngularSpeed(): number;
|
|
330
|
+
/**
|
|
331
|
+
* Solver passes the body's island takes beyond the world's, each substep (default 0). Rapier's `additional_pgs_iterations` (its `additional_solver_iterations` is extra substeps, `additionalSolverIterations` here): a stack
|
|
332
|
+
* that must stay stiff under a heavy load, or a ring of joints that needs to close, asks for more passes without raising every island's
|
|
333
|
+
* cost. The island runs the most any of its awake bodies asks for, over its own contacts and joints only; no other island is touched,
|
|
334
|
+
* and with no body asking the step is bit for bit what it was. The extra passes visit the contacts one at a time. A stack of 20 boxes
|
|
335
|
+
* under a box 100 times as heavy is still creeping to rest after two seconds at one pass; with 16 more it has arrived.
|
|
336
|
+
* @throws RangeError for a number that is not a whole number of 0 or more (up to 4,294,967,295).
|
|
337
|
+
*/
|
|
338
|
+
set additionalPgsIterations(value: number);
|
|
339
|
+
get additionalPgsIterations(): number;
|
|
340
|
+
/**
|
|
341
|
+
* Substeps the body's island takes beyond the world's, each step (default 0): Rapier's `additional_solver_iterations`, `num_solver_iterations`
|
|
342
|
+
* plus the most any body of the island asks for. The island is solved with that many substeps, each shorter, as a world set to that count
|
|
343
|
+
* would solve it; the other islands are not touched, and a kinematic body ends the step where the world's own count leaves it. (Extra
|
|
344
|
+
* passes in each substep are {@link Body2D.additionalPgsIterations}.)
|
|
345
|
+
* @throws RangeError for a number that is not a whole number of 0 or more (up to 4,294,967,295).
|
|
346
|
+
*/
|
|
347
|
+
set additionalSolverIterations(value: number);
|
|
348
|
+
get additionalSolverIterations(): number;
|
|
349
|
+
/** The centre of mass in world units, as of the last step. */
|
|
350
|
+
get centerOfMass(): [number, number];
|
|
351
|
+
/**
|
|
352
|
+
* Adds a solid to what the colliders weigh, as it is and not in proportion (Rapier's `setAdditionalMassProperties`): `mass` in
|
|
353
|
+
* kilograms with its centre at `centre` in the body's frame (world units) and `inertia` in kg m^2 (in the body's world units, as
|
|
354
|
+
* the colliders' are) about that centre. It joins the colliders' mass by the parallel axis theorem about the combined centre of
|
|
355
|
+
* mass. A mass and an inertia of 0 take it away; {@link Body2D.additionalMass} stays as the share-in-proportion kind.
|
|
356
|
+
* @throws RangeError for a number that is not finite, or a mass or inertia below 0.
|
|
357
|
+
*/
|
|
358
|
+
setAdditionalMassProperties(mass: number, centre?: Vec2D, inertia?: number): this;
|
|
359
|
+
get sleeping(): boolean;
|
|
360
|
+
wakeUp(): this;
|
|
361
|
+
/** Puts the body to sleep now; a contact or a call to `wakeUp` wakes it. */
|
|
362
|
+
sleep(): this;
|
|
363
|
+
/**
|
|
364
|
+
* Locks the body's translation on the x axis, the y axis or both (default both); `lockTranslation(false)` frees it. The
|
|
365
|
+
* lock holds the body at its position now, with an exact constraint; `teleport` moves the lock with the body.
|
|
366
|
+
*/
|
|
367
|
+
lockTranslation(x?: boolean, y?: boolean): this;
|
|
368
|
+
/**
|
|
369
|
+
* Stops the body turning, or lets it turn again: `lockRotation()` holds its angle and takes its spin away. Rapier's
|
|
370
|
+
* `lockRotations`. The `lockRotation` option of `addBody` does the same at creation.
|
|
371
|
+
*/
|
|
372
|
+
lockRotation(locked?: boolean): this;
|
|
373
|
+
/** True while the body's rotation is locked. */
|
|
374
|
+
get rotationLocked(): boolean;
|
|
375
|
+
/** @internal */
|
|
376
|
+
turnLocked: boolean;
|
|
377
|
+
/** Which axes are locked, [x, y]. */
|
|
378
|
+
get translationLocked(): [boolean, boolean];
|
|
379
|
+
/** @internal The kernel handle of the joint that holds the lock, or -1. */
|
|
380
|
+
lockJoint: number;
|
|
381
|
+
private locked;
|
|
382
|
+
/** Adds a collider. */
|
|
383
|
+
addCollider(options: ColliderOptions2D): Collider2D;
|
|
384
|
+
/** Removes the body, its colliders and its joints from the world. */
|
|
385
|
+
remove(): void;
|
|
386
|
+
}
|
|
387
|
+
/** @internal */
|
|
388
|
+
export { describeShape };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { Body2D } from './body.js';
|
|
2
|
+
import type { Physics2D, QueryFilter2D } from './physics.js';
|
|
3
|
+
import type { Vec2D } from './shapes.js';
|
|
4
|
+
/** Options of a character controller; every field has a default. */
|
|
5
|
+
export interface CharacterOptions2D {
|
|
6
|
+
/** Gap kept between the character and what it touches, world units (default 0.5). */
|
|
7
|
+
offset?: number;
|
|
8
|
+
/** Which way is up. Default [0, -1] (screen up). */
|
|
9
|
+
up?: Vec2D;
|
|
10
|
+
/**
|
|
11
|
+
* Step over ledges up to `maxHeight` high, with at least `minWidth` of free room across the top, and onto dynamic bodies too unless
|
|
12
|
+
* `includeDynamic` is false (default true, as Rapier's `includeDynamicBodies`).
|
|
13
|
+
*/
|
|
14
|
+
autostep?: {
|
|
15
|
+
maxHeight: number;
|
|
16
|
+
minWidth: number;
|
|
17
|
+
includeDynamic?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/** Steepest slope it can climb, radians (default 45 degrees). */
|
|
20
|
+
maxSlopeClimbAngle?: number;
|
|
21
|
+
/** Slopes steeper than this slide it down, radians (default 30 degrees). */
|
|
22
|
+
minSlopeSlideAngle?: number;
|
|
23
|
+
/** Stick to the ground when walking down slopes or steps up to this distance, world units. */
|
|
24
|
+
snapToGround?: number;
|
|
25
|
+
/** Slide along walls instead of stopping (default true). */
|
|
26
|
+
slide?: boolean;
|
|
27
|
+
/** Push dynamic bodies it walks into (default false). */
|
|
28
|
+
pushDynamic?: boolean;
|
|
29
|
+
/** Mass used when pushing, in kilograms. */
|
|
30
|
+
mass?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Which colliders the character meets, like a scene query's filter: `groups`, `excludeFixed`, `excludeKinematic`, `excludeDynamic`,
|
|
33
|
+
* `predicate`, `excludeCollider` (Rapier's `filterFlags`, `filterGroups` and `filterPredicate`). The character's own body is always
|
|
34
|
+
* left out, and sensors unless `excludeSensors` is false. A filter given to `move` replaces this one for that move.
|
|
35
|
+
*/
|
|
36
|
+
filter?: QueryFilter2D;
|
|
37
|
+
}
|
|
38
|
+
/** What one `move` did. */
|
|
39
|
+
export interface CharacterMoveResult2D {
|
|
40
|
+
/** The movement that was applied, world units. */
|
|
41
|
+
movement: [number, number];
|
|
42
|
+
grounded: boolean;
|
|
43
|
+
/** Number of contacts the move hit. */
|
|
44
|
+
collisions: number;
|
|
45
|
+
/** What the move hit, in order: the collider, the surface normal pointing out of it and the contact point. */
|
|
46
|
+
hits: Array<{
|
|
47
|
+
collider: import('./body.js').Collider2D;
|
|
48
|
+
body: Body2D;
|
|
49
|
+
normal: [number, number];
|
|
50
|
+
point: [number, number];
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Moves a kinematic position body around obstacles. Call `move` once per fixed step (from `beforestep`).
|
|
55
|
+
*
|
|
56
|
+
* The algorithm is the one the Rapier user guide describes for its kinematic character controller, built on the shape
|
|
57
|
+
* cast of the kernel: the character slides along what it hits keeping `offset` away from it, climbs slopes up to
|
|
58
|
+
* `maxSlopeClimbAngle` keeping its horizontal speed, slides down slopes steeper than `minSlopeSlideAngle`, steps over
|
|
59
|
+
* ledges (`autostep`) and sticks to the ground on the way down (`snapToGround`).
|
|
60
|
+
*/
|
|
61
|
+
export declare class CharacterController2D {
|
|
62
|
+
private readonly physics;
|
|
63
|
+
readonly body: Body2D;
|
|
64
|
+
private readonly options;
|
|
65
|
+
private readonly onDispose;
|
|
66
|
+
private readonly up;
|
|
67
|
+
private readonly offset;
|
|
68
|
+
private readonly climb;
|
|
69
|
+
private readonly slideAngle;
|
|
70
|
+
private disposed;
|
|
71
|
+
/** @internal */
|
|
72
|
+
constructor(physics: Physics2D, body: Body2D, options: CharacterOptions2D, onDispose: (controller: CharacterController2D) => void);
|
|
73
|
+
/** Casts the character's shape from `at` (metres) along the unit `dir` for `max`; the nearest hit that is not itself. */
|
|
74
|
+
private cast;
|
|
75
|
+
/** The filter of the move under way: the caller's, with the character's own body left out. */
|
|
76
|
+
private filter;
|
|
77
|
+
private filterFor;
|
|
78
|
+
/** Computes the movement for `delta` (world units) and sets the body's next position; `filter` replaces the controller's for this move. */
|
|
79
|
+
move(delta: Vec2D, filter?: QueryFilter2D): CharacterMoveResult2D;
|
|
80
|
+
/** Tries to climb a ledge in the way of `rest`: up by at most `maxHeight`, across by at least `minWidth`, down onto ground. */
|
|
81
|
+
private step;
|
|
82
|
+
/** Pushes a dynamic body the character walks into, with the character's mass behind the movement. */
|
|
83
|
+
private pushDynamic;
|
|
84
|
+
dispose(): void;
|
|
85
|
+
[Symbol.dispose]: () => void;
|
|
86
|
+
}
|
package/dist/clock.d.ts
ADDED