@melonjs/matter-adapter 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.
@@ -0,0 +1,208 @@
1
+ import * as Matter from "matter-js";
2
+ import { type AdapterCapabilities, type BodyDefinition, type BodyShape, type Bounds, type PhysicsAdapter, type PhysicsBody, type RaycastHit, Rect, type Renderable, Vector2d, type World } from "melonjs";
3
+ /**
4
+ * Minimum melonJS version this adapter requires. The PhysicsAdapter
5
+ * interface, `bodyDef` auto-registration, and the resolver in
6
+ * `Application` are part of the 19.5 work — older releases don't have
7
+ * the plumbing for instance-based adapters. Tracks the engine version
8
+ * actually shipping these APIs (will be bumped to "19.5.0" once the
9
+ * engine release is cut).
10
+ */
11
+ export declare const REQUIRED_MELONJS_VERSION = "19.5.0";
12
+ /**
13
+ * Options accepted by the {@link MatterAdapter} constructor.
14
+ */
15
+ export interface MatterAdapterOptions {
16
+ /** initial world gravity, default `{x: 0, y: 1}` (matter-js convention) */
17
+ gravity?: {
18
+ x: number;
19
+ y: number;
20
+ };
21
+ /**
22
+ * Number of physics substeps per engine frame. Each call to
23
+ * {@link MatterAdapter.step} runs `Engine.update(engine, dt / N)`
24
+ * `N` times instead of one full-dt step. Increases narrow-phase
25
+ * accuracy at high relative velocities (break shots, projectiles)
26
+ * at the cost of ~N× physics CPU. Default `1` matches the legacy
27
+ * single-step behaviour. Values < 1 are clamped to 1.
28
+ */
29
+ subSteps?: number;
30
+ /**
31
+ * raw matter-js engine options forwarded to `Matter.Engine.create()`.
32
+ * Use this to tweak solver iterations, constraint accuracy, etc.
33
+ */
34
+ matterEngineOptions?: Matter.IEngineDefinition;
35
+ }
36
+ /**
37
+ * melonJS physics adapter wrapping matter-js (https://brm.io/matter-js/).
38
+ *
39
+ * Implements the full {@link PhysicsAdapter} interface so the same game
40
+ * code that runs on the built-in SAT physics also runs under Matter —
41
+ * with the upgrade in capabilities Matter brings (real rotational
42
+ * dynamics, restitution-based stacking, constraints, sleeping bodies,
43
+ * native raycasts).
44
+ * @example
45
+ * import { Application } from "melonjs";
46
+ * import { MatterAdapter } from "@melonjs/matter-adapter";
47
+ *
48
+ * const app = new Application(800, 600, {
49
+ * parent: "screen",
50
+ * physic: new MatterAdapter(),
51
+ * });
52
+ */
53
+ export declare class MatterAdapter implements PhysicsAdapter {
54
+ readonly physicLabel = "matter";
55
+ readonly name = "@melonjs/matter-adapter";
56
+ readonly version: string;
57
+ readonly url = "https://www.npmjs.com/package/@melonjs/matter-adapter";
58
+ readonly capabilities: AdapterCapabilities;
59
+ gravity: Vector2d;
60
+ /**
61
+ * Raw matter-js namespace — escape hatch for matter-specific features
62
+ * the portable `PhysicsAdapter` interface doesn't cover (constraints,
63
+ * compound bodies, events on the Matter engine, queries, etc.).
64
+ *
65
+ * Saves you from adding a transitive `import * as Matter from "matter-js"`
66
+ * just to reach the factories you need. The Matter modules are accessed
67
+ * by the same names matter's own docs use, so examples from the
68
+ * matter-js docs copy-paste without renaming:
69
+ *
70
+ * ```ts
71
+ * const spring = adapter.matter.Constraint.create({
72
+ * bodyA: a.body, bodyB: b.body, stiffness: 0.04, length: 80,
73
+ * });
74
+ * adapter.matter.Composite.add(adapter.engine.world, spring);
75
+ * ```
76
+ *
77
+ * Game code that touches `adapter.matter.*` is matter-only — it will
78
+ * not work under any other physics adapter. Use the
79
+ * `PhysicsAdapter` methods for anything that should stay portable.
80
+ * @see {@link https://brm.io/matter-js/docs/ Official matter-js documentation}
81
+ * for the full module reference (`Matter.Constraint`, `Matter.Composite`,
82
+ * `Matter.Bodies`, `Matter.Events`, `Matter.Query`, `Matter.Vector`, …).
83
+ */
84
+ readonly matter: typeof Matter;
85
+ /** the underlying Matter engine; exposed for advanced use cases. */
86
+ engine: Matter.Engine;
87
+ /** back-reference to the owning melonJS world (set in {@link init}). */
88
+ world: World;
89
+ /** renderable → its matter-js body */
90
+ private readonly bodyMap;
91
+ /** matter-js body → its renderable (for collision / sync) */
92
+ private readonly renderableMap;
93
+ /** per-body velocity cap (Matter has no native equivalent) */
94
+ private readonly velocityLimits;
95
+ /** per-body bodyDef hash kept for shape introspection / removeBody */
96
+ private readonly defMap;
97
+ /**
98
+ * Per-body gravity multiplier. matter-js 0.20 doesn't honor a
99
+ * body-level gravityScale, so we emulate it by applying a counter-
100
+ * force each step (see {@link init} `beforeUpdate`). Only stored for
101
+ * bodies with scale ≠ 1; the default-1 case is the hot path and we
102
+ * skip the map lookup entirely for it.
103
+ */
104
+ private readonly bodyGravityScale;
105
+ /**
106
+ * Offset between `renderable.pos` (top-left in melonJS convention)
107
+ * and `Matter.Body.position` (centroid in Matter's convention). Stored
108
+ * at addBody time so syncFromPhysics can place the sprite correctly.
109
+ */
110
+ private readonly posOffsets;
111
+ private readonly matterOptions;
112
+ private readonly subSteps;
113
+ constructor(options?: MatterAdapterOptions);
114
+ /**
115
+ * Stored references to the listeners registered in {@link init}, so
116
+ * {@link destroy} can `Matter.Events.off` them. Without these stored
117
+ * refs, a destroy/re-init cycle would leak the old listeners and
118
+ * dispatch every event twice on the second cycle.
119
+ */
120
+ private _matterListeners;
121
+ init(world: World): void;
122
+ destroy(): void;
123
+ step(dt: number): void;
124
+ syncFromPhysics(): void;
125
+ addBody(renderable: Renderable, def: BodyDefinition): MatterAdapter.Body;
126
+ removeBody(renderable: Renderable): void;
127
+ updateShape(renderable: Renderable, shapes: BodyShape[]): void;
128
+ getVelocity(renderable: Renderable, out?: Vector2d): Vector2d;
129
+ setVelocity(renderable: Renderable, v: Vector2d): void;
130
+ applyForce(renderable: Renderable, force: Vector2d, point?: Vector2d): void;
131
+ applyImpulse(renderable: Renderable, impulse: Vector2d): void;
132
+ setPosition(renderable: Renderable, p: Vector2d): void;
133
+ private invalidateContactsFor;
134
+ setAngle(renderable: Renderable, angle: number): void;
135
+ getAngle(renderable: Renderable): number;
136
+ setAngularVelocity(renderable: Renderable, omega: number): void;
137
+ getAngularVelocity(renderable: Renderable): number;
138
+ applyTorque(renderable: Renderable, torque: number): void;
139
+ setStatic(renderable: Renderable, isStatic: boolean): void;
140
+ setGravityScale(renderable: Renderable, scale: number): void;
141
+ setSensor(renderable: Renderable, isSensor: boolean): void;
142
+ setFrictionAir(renderable: Renderable, friction: number | {
143
+ x: number;
144
+ y: number;
145
+ }): void;
146
+ setMaxVelocity(renderable: Renderable, limit: {
147
+ x: number;
148
+ y: number;
149
+ }): void;
150
+ getMaxVelocity(renderable: Renderable): {
151
+ x: number;
152
+ y: number;
153
+ };
154
+ setCollisionType(renderable: Renderable, type: number): void;
155
+ setCollisionMask(renderable: Renderable, mask: number): void;
156
+ /**
157
+ * Adapter-side debug surface: the body's AABB in renderable-local
158
+ * coordinates. Matter tracks `body.bounds` in WORLD space; we
159
+ * subtract `renderable.pos` so the result matches melonJS's local-
160
+ * space convention (the debug plugin translates to the renderable
161
+ * origin before drawing, and would otherwise see the bounds drawn
162
+ * offset by the renderable's world position).
163
+ * @param renderable - the renderable whose body bounds to read
164
+ * @param out - destination `Bounds` (filled in place, also returned)
165
+ */
166
+ getBodyAABB(renderable: Renderable, out: Bounds): Bounds | undefined;
167
+ /**
168
+ * Adapter-side debug surface: the body's collision shapes in
169
+ * renderable-local coordinates. We return the original `def.shapes`
170
+ * array — those are the input shape definitions in local space,
171
+ * unchanged by matter's body transformation (rotation is baked into
172
+ * matter's vertices, not into our local-space defs). Read-only.
173
+ * @param renderable - the renderable whose body shapes to read
174
+ */
175
+ getBodyShapes(renderable: Renderable): readonly BodyShape[];
176
+ isGrounded(renderable: Renderable): boolean;
177
+ raycast(from: Vector2d, to: Vector2d): RaycastHit | null;
178
+ queryAABB(rect: Rect): Renderable[];
179
+ private _clampVelocities;
180
+ private _dispatchCollisions;
181
+ private _shapeToMatter;
182
+ }
183
+ /**
184
+ * Namespace-merged with the {@link MatterAdapter} class to expose
185
+ * adapter-owned types alongside the runtime API. Access via
186
+ * `MatterAdapter.Body`, etc.
187
+ */
188
+ export declare namespace MatterAdapter {
189
+ /**
190
+ * The concrete body handle attached to `renderable.body` under
191
+ * {@link MatterAdapter}. Combines the raw `Matter.Body` (with all
192
+ * native fields — `frictionAir`, `restitution`, `friction`, `angle`,
193
+ * `angularVelocity`, …) with the portable melonJS helper methods
194
+ * (`setVelocity`, `applyImpulse`, `setStatic`, etc.) spliced on at
195
+ * `addBody` time.
196
+ *
197
+ * Type it explicitly when reaching for matter-native fields:
198
+ *
199
+ * ```ts
200
+ * (this.body as MatterAdapter.Body).frictionAir = 0.02;
201
+ * ```
202
+ *
203
+ * Keeps user code free of a direct `matter-js` import — the matter
204
+ * dependency stays behind the adapter boundary.
205
+ */
206
+ type Body = ReturnType<typeof Matter.Body.create> & PhysicsBody;
207
+ }
208
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,WAAW,CAAC;AAWpC,OAAO,EACN,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,SAAS,EACd,KAAK,MAAM,EAGX,KAAK,cAAc,EACnB,KAAK,WAAW,EAEhB,KAAK,UAAU,EACf,IAAI,EACJ,KAAK,UAAU,EAGf,QAAQ,EACR,KAAK,KAAK,EACV,MAAM,SAAS,CAAC;AAIjB;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,WAAW,CAAC;AAEjD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACpC,2EAA2E;IAC3E,OAAO,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACnC;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,mBAAmB,CAAC,EAAE,MAAM,CAAC,iBAAiB,CAAC;CAC/C;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,aAAc,YAAW,cAAc;IACnD,QAAQ,CAAC,WAAW,YAAY;IAChC,QA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