emeraldengine 2.2.1 → 3.1.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.
Files changed (208) hide show
  1. package/README.md +2198 -968
  2. package/dist/types/index.d.ts +75 -32
  3. package/dist/types/src/Animator.d.ts +50 -0
  4. package/dist/types/{BitmapText.d.ts → src/BitmapText.d.ts} +19 -21
  5. package/dist/types/src/Camera.d.ts +122 -0
  6. package/dist/types/src/CameraController.d.ts +107 -0
  7. package/dist/types/src/CanvasText.d.ts +91 -0
  8. package/dist/types/src/CollisionLayers.d.ts +58 -0
  9. package/dist/types/{Color.d.ts → src/Color.d.ts} +6 -6
  10. package/dist/types/src/Coroutine.d.ts +111 -0
  11. package/dist/types/src/DebugOverlay.d.ts +86 -0
  12. package/dist/types/src/Drawable.d.ts +271 -0
  13. package/dist/types/src/Easing.d.ts +22 -0
  14. package/dist/types/src/Emerald.d.ts +420 -0
  15. package/dist/types/src/EmeraldDB.d.ts +159 -0
  16. package/dist/types/{FPSCounter.d.ts → src/FPSCounter.d.ts} +1 -3
  17. package/dist/types/src/GLUtils.d.ts +4 -0
  18. package/dist/types/{Instance.d.ts → src/Instance.d.ts} +37 -15
  19. package/dist/types/{InstancedTexture.d.ts → src/InstancedTexture.d.ts} +75 -23
  20. package/dist/types/src/Interpolator.d.ts +66 -0
  21. package/dist/types/src/Material.d.ts +87 -0
  22. package/dist/types/src/MathUtils.d.ts +81 -0
  23. package/dist/types/src/ParticleEmitter.d.ts +131 -0
  24. package/dist/types/src/Physics.d.ts +337 -0
  25. package/dist/types/src/Pool.d.ts +53 -0
  26. package/dist/types/src/PostEffects.d.ts +68 -0
  27. package/dist/types/src/PostProcessor.d.ts +124 -0
  28. package/dist/types/src/RenderTarget.d.ts +56 -0
  29. package/dist/types/src/Scene.d.ts +62 -0
  30. package/dist/types/src/ScreenEffects.d.ts +111 -0
  31. package/dist/types/src/Serializer.d.ts +86 -0
  32. package/dist/types/src/Shaders.d.ts +2 -0
  33. package/dist/types/{Shapes.d.ts → src/Shapes.d.ts} +4 -6
  34. package/dist/types/src/SpatialGrid.d.ts +50 -0
  35. package/dist/types/src/SpriteBatch.d.ts +89 -0
  36. package/dist/types/src/StateMachine.d.ts +59 -0
  37. package/dist/types/src/Storage.d.ts +89 -0
  38. package/dist/types/{Texture.d.ts → src/Texture.d.ts} +3 -4
  39. package/dist/types/src/TextureAtlas.d.ts +55 -0
  40. package/dist/types/src/Tilemap.d.ts +91 -0
  41. package/dist/types/src/Time.d.ts +53 -0
  42. package/dist/types/src/Timer.d.ts +54 -0
  43. package/dist/types/src/Transform.d.ts +94 -0
  44. package/dist/types/src/Tween.d.ts +81 -0
  45. package/dist/types/src/UI.d.ts +121 -0
  46. package/dist/types/src/components/Behaviour.d.ts +71 -0
  47. package/dist/types/{components → src/components}/BoxCollider.d.ts +14 -14
  48. package/dist/types/src/components/BoxColliderDebug.d.ts +15 -0
  49. package/dist/types/{components → src/components}/CircleCollider.d.ts +14 -12
  50. package/dist/types/src/components/CircleColliderDebug.d.ts +15 -0
  51. package/dist/types/src/components/Collider.d.ts +102 -0
  52. package/dist/types/src/components/GameObject.d.ts +135 -0
  53. package/dist/types/src/components/PolygonCollider.d.ts +33 -0
  54. package/dist/types/src/components/RigidBody.d.ts +456 -0
  55. package/dist/types/src/importers/Aseprite.d.ts +79 -0
  56. package/dist/types/src/importers/ForgeLevel.d.ts +97 -0
  57. package/dist/types/src/importers/TiledMap.d.ts +62 -0
  58. package/dist/types/{lights → src/lights}/DirectionalLight.d.ts +7 -9
  59. package/dist/types/{lights → src/lights}/PointLight.d.ts +6 -9
  60. package/dist/types/src/managers/AssetManager.d.ts +116 -0
  61. package/dist/types/src/managers/AudioManager.d.ts +259 -0
  62. package/dist/types/{managers → src/managers}/CameraManager.d.ts +8 -8
  63. package/dist/types/{managers → src/managers}/EventManager.d.ts +46 -32
  64. package/dist/types/src/managers/GLManager.d.ts +84 -0
  65. package/dist/types/src/managers/GLState.d.ts +37 -0
  66. package/dist/types/src/managers/Gamepad.d.ts +102 -0
  67. package/dist/types/src/managers/IDManager.d.ts +31 -0
  68. package/dist/types/src/managers/InputManager.d.ts +381 -0
  69. package/dist/types/src/managers/NetworkManager.d.ts +93 -0
  70. package/dist/types/src/managers/RenderStats.d.ts +34 -0
  71. package/dist/types/src/managers/SceneManager.d.ts +44 -0
  72. package/dist/types/src/managers/ShaderManager.d.ts +55 -0
  73. package/dist/types/src/managers/TextureManager.d.ts +102 -0
  74. package/dist/types/src/particlesystem/Particle.d.ts +64 -0
  75. package/dist/types/{particlesystem → src/particlesystem}/ParticleSettings.d.ts +32 -24
  76. package/dist/types/{particlesystem → src/particlesystem}/Particles.d.ts +20 -12
  77. package/dist/types/src/physics/AABB.d.ts +92 -0
  78. package/dist/types/src/physics/Body.d.ts +435 -0
  79. package/dist/types/src/physics/BodyType.d.ts +6 -0
  80. package/dist/types/src/physics/BroadPhase.d.ts +210 -0
  81. package/dist/types/src/physics/Collision.d.ts +102 -0
  82. package/dist/types/src/physics/Contact.d.ts +206 -0
  83. package/dist/types/src/physics/ContactSolver.d.ts +108 -0
  84. package/dist/types/src/physics/Distance.d.ts +54 -0
  85. package/dist/types/src/physics/DistanceJoint.d.ts +90 -0
  86. package/dist/types/src/physics/Fixture.d.ts +221 -0
  87. package/dist/types/src/physics/Island.d.ts +52 -0
  88. package/dist/types/src/physics/Joint.d.ts +59 -0
  89. package/dist/types/src/physics/Math2D.d.ts +371 -0
  90. package/dist/types/src/physics/RevoluteJoint.d.ts +119 -0
  91. package/dist/types/src/physics/Settings.d.ts +22 -0
  92. package/dist/types/src/physics/Shapes.d.ts +207 -0
  93. package/dist/types/src/physics/TimeOfImpact.d.ts +22 -0
  94. package/dist/types/src/physics/World.d.ts +274 -0
  95. package/dist/types/src/physics/index.d.ts +34 -0
  96. package/index.js +78 -0
  97. package/package.json +73 -60
  98. package/src/Animator.js +95 -0
  99. package/src/BitmapText.js +6 -5
  100. package/src/Camera.js +183 -0
  101. package/src/CameraController.js +192 -0
  102. package/src/CanvasText.js +281 -0
  103. package/src/CollisionLayers.js +86 -0
  104. package/src/Color.js +26 -18
  105. package/src/Coroutine.js +259 -0
  106. package/src/DebugOverlay.js +246 -0
  107. package/src/Drawable.js +842 -582
  108. package/src/Easing.js +57 -0
  109. package/src/Emerald.js +1150 -459
  110. package/src/EmeraldDB.js +328 -0
  111. package/src/FPSCounter.js +43 -43
  112. package/src/GLUtils.js +60 -67
  113. package/src/Instance.js +41 -5
  114. package/src/InstancedTexture.js +308 -127
  115. package/src/Interpolator.js +124 -0
  116. package/src/Material.js +202 -0
  117. package/src/MathUtils.js +134 -0
  118. package/src/ParticleEmitter.js +284 -0
  119. package/src/Physics.js +431 -40
  120. package/src/Pool.js +85 -0
  121. package/src/PostEffects.js +296 -0
  122. package/src/PostProcessor.js +304 -0
  123. package/src/RenderTarget.js +134 -0
  124. package/src/Scene.js +115 -83
  125. package/src/ScreenEffects.js +266 -0
  126. package/src/Serializer.js +131 -0
  127. package/src/Shaders.js +150 -165
  128. package/src/Shapes.js +118 -129
  129. package/src/SpatialGrid.js +111 -0
  130. package/src/SpriteBatch.js +299 -0
  131. package/src/StateMachine.js +82 -0
  132. package/src/Storage.js +175 -47
  133. package/src/Texture.js +58 -67
  134. package/src/TextureAtlas.js +96 -0
  135. package/src/Tilemap.js +274 -0
  136. package/src/Time.js +51 -6
  137. package/src/Timer.js +99 -0
  138. package/src/Transform.js +100 -7
  139. package/src/Tween.js +160 -0
  140. package/src/UI.js +394 -0
  141. package/src/components/Behaviour.js +90 -0
  142. package/src/components/BoxCollider.js +29 -12
  143. package/src/components/BoxColliderDebug.js +3 -4
  144. package/src/components/CircleCollider.js +26 -12
  145. package/src/components/CircleColliderDebug.js +25 -24
  146. package/src/components/Collider.js +150 -34
  147. package/src/components/GameObject.js +130 -21
  148. package/src/components/PolygonCollider.js +55 -0
  149. package/src/components/RigidBody.js +556 -8
  150. package/src/importers/Aseprite.js +142 -0
  151. package/src/importers/ForgeLevel.js +581 -0
  152. package/src/importers/TiledMap.js +158 -0
  153. package/src/lights/DirectionalLight.js +6 -15
  154. package/src/lights/PointLight.js +4 -4
  155. package/src/managers/AssetManager.js +239 -0
  156. package/src/managers/AudioManager.js +565 -146
  157. package/src/managers/EventManager.js +488 -477
  158. package/src/managers/GLManager.js +57 -0
  159. package/src/managers/GLState.js +70 -0
  160. package/src/managers/Gamepad.js +126 -0
  161. package/src/managers/IDManager.js +24 -2
  162. package/src/managers/InputManager.js +905 -0
  163. package/src/managers/NetworkManager.js +178 -0
  164. package/src/managers/RenderStats.js +34 -0
  165. package/src/managers/SceneManager.js +30 -0
  166. package/src/managers/ShaderManager.js +0 -2
  167. package/src/managers/TextureManager.js +218 -0
  168. package/src/particlesystem/Particle.js +82 -7
  169. package/src/particlesystem/ParticleSettings.js +21 -3
  170. package/src/particlesystem/Particles.js +80 -31
  171. package/src/physics/AABB.js +207 -0
  172. package/src/physics/Body.js +862 -0
  173. package/src/physics/BodyType.js +16 -0
  174. package/src/physics/BroadPhase.js +641 -0
  175. package/src/physics/Collision.js +534 -0
  176. package/src/physics/Contact.js +500 -0
  177. package/src/physics/ContactSolver.js +526 -0
  178. package/src/physics/Distance.js +403 -0
  179. package/src/physics/DistanceJoint.js +227 -0
  180. package/src/physics/Fixture.js +346 -0
  181. package/src/physics/Island.js +203 -0
  182. package/src/physics/Joint.js +78 -0
  183. package/src/physics/Math2D.js +573 -0
  184. package/src/physics/RevoluteJoint.js +278 -0
  185. package/src/physics/Settings.js +78 -0
  186. package/src/physics/Shapes.js +549 -0
  187. package/src/physics/TimeOfImpact.js +87 -0
  188. package/src/physics/World.js +731 -0
  189. package/src/physics/index.js +79 -0
  190. package/dist/types/Drawable.d.ts +0 -157
  191. package/dist/types/Emerald.d.ts +0 -73
  192. package/dist/types/GLUtils.d.ts +0 -6
  193. package/dist/types/Physics.d.ts +0 -139
  194. package/dist/types/Scene.d.ts +0 -39
  195. package/dist/types/Shaders.d.ts +0 -4
  196. package/dist/types/Storage.d.ts +0 -46
  197. package/dist/types/Time.d.ts +0 -22
  198. package/dist/types/Transform.d.ts +0 -41
  199. package/dist/types/components/BoxColliderDebug.d.ts +0 -19
  200. package/dist/types/components/CircleColliderDebug.d.ts +0 -19
  201. package/dist/types/components/Collider.d.ts +0 -53
  202. package/dist/types/components/GameObject.d.ts +0 -72
  203. package/dist/types/components/RigidBody.d.ts +0 -104
  204. package/dist/types/managers/AudioManager.d.ts +0 -60
  205. package/dist/types/managers/GLManager.d.ts +0 -47
  206. package/dist/types/managers/IDManager.d.ts +0 -21
  207. package/dist/types/managers/SceneManager.d.ts +0 -22
  208. package/dist/types/particlesystem/Particle.d.ts +0 -42
@@ -1,10 +1,16 @@
1
- import * as planck from "planck";
1
+ import { Vec2 } from "../physics/index.js";
2
2
  import IDManager from "../managers/IDManager.js";
3
3
  import { Vector2 } from "../Physics.js";
4
4
  import Collider from "./Collider.js";
5
5
 
6
6
  /**
7
7
  * @class RigidBody
8
+ * @description The game-facing physics body: everything you can do to a body
9
+ * (move it, push it, spin it, sleep it, query its mass) is a method on this
10
+ * class directly, in world/pixel units. There is no separate lower-level
11
+ * body object you need to fetch first. `getBody()` still returns the raw,
12
+ * physics-unit {@link Body} underneath for the rare case you need it (writing
13
+ * a custom joint, say), but nothing in ordinary use requires it.
8
14
  * @param {Physics} physics - The physics engine
9
15
  * @param {string | "static" | "dynamic" | "kinematic"} type - The type of rigidbody
10
16
  * @param {Vector2} position - The position of the rigidbody
@@ -26,7 +32,7 @@ class RigidBody {
26
32
  this.type = type;
27
33
  this.body = physics.world.createBody({
28
34
  type: type,
29
- position: new planck.Vec2(
35
+ position: new Vec2(
30
36
  (position.x + offset.x) / physics.scale,
31
37
  (position.y + offset.y) / physics.scale
32
38
  ),
@@ -37,6 +43,10 @@ class RigidBody {
37
43
  this.collider = null;
38
44
  this.id = IDManager.generateUniqueID();
39
45
  this.name = "RigidBody" + this.id;
46
+ /** Freely usable by your own code; see {@link RigidBody#getUserData}. */
47
+ this.userData = null;
48
+
49
+ this.body.setUserData(this);
40
50
  }
41
51
 
42
52
  /**
@@ -46,13 +56,77 @@ class RigidBody {
46
56
  */
47
57
  updatePosition(position) {
48
58
  this.body.setPosition(
49
- new planck.Vec2(
59
+ new Vec2(
50
60
  (position.x + this.offset.x) / this.physics.scale,
51
61
  (position.y + this.offset.y) / this.physics.scale
52
62
  )
53
63
  );
54
64
  }
55
65
 
66
+ /**
67
+ * @method setPosition
68
+ * @description Alias of {@link RigidBody#updatePosition}, named to match
69
+ * `setRotation`/`setTransform`.
70
+ * @param {Vector2} position - The new position, in world (pixel) units
71
+ */
72
+ setPosition(position) {
73
+ this.updatePosition(position);
74
+ }
75
+
76
+ /**
77
+ * @method setTransform
78
+ * @description Teleports the body to a new position and angle in one move
79
+ * (no velocity implied), and wakes it so contacts at the new spot are seen
80
+ * on the very next step.
81
+ * @param {Vector2} position - World (pixel) units
82
+ * @param {number} angle - Radians
83
+ */
84
+ setTransform(position, angle) {
85
+ this.body.setTransform(
86
+ new Vec2(
87
+ (position.x + this.offset.x) / this.physics.scale,
88
+ (position.y + this.offset.y) / this.physics.scale
89
+ ),
90
+ angle
91
+ );
92
+ }
93
+
94
+ /**
95
+ * @method getWorldX
96
+ * @description The single source of truth for body(physics) -> world(pixel)
97
+ * conversion on X: undo the scale and the spawn offset.
98
+ * @returns {number} - The live world-space x of the body
99
+ * @private
100
+ */
101
+ getWorldX() {
102
+ return this.body.getPosition().x * this.physics.scale - this.offset.x;
103
+ }
104
+
105
+ /**
106
+ * @method getWorldY
107
+ * @description World-space y counterpart of getWorldX.
108
+ * @returns {number} - The live world-space y of the body
109
+ * @private
110
+ */
111
+ getWorldY() {
112
+ return this.body.getPosition().y * this.physics.scale - this.offset.y;
113
+ }
114
+
115
+ /**
116
+ * @method syncTransform
117
+ * @description Writes the body's live world position and angle into a
118
+ * Transform. This is the one place body state is copied onto a renderable, so
119
+ * position and rotation stay in lock-step. Static/kinematic bodies are not
120
+ * driven by the simulation, so only dynamic bodies write back.
121
+ * @param {Transform} transform - The transform to update in place
122
+ */
123
+ syncTransform(transform) {
124
+ if (this.type !== "dynamic") return;
125
+ transform.position.x = this.getWorldX();
126
+ transform.position.y = this.getWorldY();
127
+ transform.rotation = this.body.getAngle();
128
+ }
129
+
56
130
  /**
57
131
  * @method getOffset
58
132
  * @description Returns the offset of the rigidbody
@@ -73,6 +147,29 @@ class RigidBody {
73
147
  }
74
148
  }
75
149
 
150
+ /**
151
+ * @method createFixture
152
+ * @description Attaches a shape directly, bypassing the Collider component
153
+ * classes ({@link BoxCollider}, {@link CircleCollider}, {@link PolygonCollider})
154
+ * that normally build one for you. `shape` is a physics-unit shape (e.g. from
155
+ * `Box()`/`Circle()`/`new PolygonShape()`), not pixels.
156
+ * @param {Shape} shape
157
+ * @param {Object|number} [def] - `{ density, friction, restitution, isSensor, ... }`
158
+ * @returns {Fixture}
159
+ */
160
+ createFixture(shape, def = {}) {
161
+ return this.body.createFixture(shape, def);
162
+ }
163
+
164
+ /**
165
+ * @method destroyFixture
166
+ * @description Removes a fixture created with {@link RigidBody#createFixture}.
167
+ * @param {Fixture} fixture
168
+ */
169
+ destroyFixture(fixture) {
170
+ this.body.destroyFixture(fixture);
171
+ }
172
+
76
173
  /**
77
174
  * @method setRotation
78
175
  * @description Sets the rotation of the rigidbody
@@ -82,12 +179,447 @@ class RigidBody {
82
179
  this.body.setAngle(rotation);
83
180
  }
84
181
 
182
+ /**
183
+ * @method setLinearVelocity
184
+ * @description Sets the body's linear velocity in world (pixel) units per
185
+ * second. Converts to physics units internally.
186
+ * @param {number} vx - Horizontal velocity (world units/sec)
187
+ * @param {number} vy - Vertical velocity (world units/sec)
188
+ */
189
+ setLinearVelocity(vx, vy) {
190
+ this.body.setLinearVelocity(
191
+ new Vec2(vx / this.physics.scale, vy / this.physics.scale)
192
+ );
193
+ }
194
+
195
+ /**
196
+ * @method getLinearVelocity
197
+ * @description Returns the body's linear velocity in world (pixel) units/sec.
198
+ * @returns {{x:number, y:number}}
199
+ */
200
+ getLinearVelocity() {
201
+ const v = this.body.getLinearVelocity();
202
+ return { x: v.x * this.physics.scale, y: v.y * this.physics.scale };
203
+ }
204
+
205
+ /**
206
+ * @method getLinearVelocityFromWorldPoint
207
+ * @description The velocity (world units/sec) of the material point of this
208
+ * body currently at `worldPoint`: linear velocity plus the contribution
209
+ * from spin. Useful for e.g. where a spinning platform's edge is moving.
210
+ * @param {Object} worldPoint - World (pixel) units
211
+ * @returns {Vector2}
212
+ */
213
+ getLinearVelocityFromWorldPoint(worldPoint) {
214
+ const scale = this.physics.scale;
215
+ const v = this.body.getLinearVelocityFromWorldPoint(
216
+ new Vec2(worldPoint.x / scale, worldPoint.y / scale)
217
+ );
218
+ return new Vector2(v.x * scale, v.y * scale);
219
+ }
220
+
221
+ /**
222
+ * @method setAngularVelocity
223
+ * @description Sets the body's spin, in radians per second.
224
+ * @param {number} omega
225
+ */
226
+ setAngularVelocity(omega) {
227
+ this.body.setAngularVelocity(omega);
228
+ }
229
+
230
+ /**
231
+ * @method getAngularVelocity
232
+ * @description Returns the body's spin, in radians per second.
233
+ * @returns {number}
234
+ */
235
+ getAngularVelocity() {
236
+ return this.body.getAngularVelocity();
237
+ }
238
+
239
+ /**
240
+ * @method applyImpulse
241
+ * @description Applies an instantaneous change in momentum (world units).
242
+ * This is what a jump or a knockback should use, since (unlike a force) it
243
+ * takes effect immediately rather than accumulating over the step.
244
+ * @param {number} ix
245
+ * @param {number} iy
246
+ * @param {Object} [point] - World (pixel) point to apply it at; defaults to
247
+ * the center of mass (no torque)
248
+ * @param {boolean} [wake=true]
249
+ */
250
+ applyImpulse(ix, iy, point = null, wake = true) {
251
+ const scale = this.physics.scale;
252
+ this.body.applyLinearImpulse(
253
+ new Vec2(ix / scale, iy / scale),
254
+ point
255
+ ? new Vec2(point.x / scale, point.y / scale)
256
+ : this.body.getWorldCenter(),
257
+ wake
258
+ );
259
+ }
260
+
261
+ /**
262
+ * @method applyAngularImpulse
263
+ * @description Applies an instantaneous change in angular momentum.
264
+ * @param {number} impulse
265
+ * @param {boolean} [wake=true]
266
+ */
267
+ applyAngularImpulse(impulse, wake = true) {
268
+ this.body.applyAngularImpulse(impulse, wake);
269
+ }
270
+
271
+ /**
272
+ * @method applyForce
273
+ * @description Applies a force (world units) at a world point. Forces
274
+ * accumulate and are cleared at the end of every step, so this belongs in
275
+ * your update loop, unlike an impulse.
276
+ * @param {number} fx
277
+ * @param {number} fy
278
+ * @param {Object} [point] - World (pixel) point; defaults to the center of
279
+ * mass (no torque)
280
+ * @param {boolean} [wake=true]
281
+ */
282
+ applyForce(fx, fy, point = null, wake = true) {
283
+ const scale = this.physics.scale;
284
+ this.body.applyForce(
285
+ new Vec2(fx / scale, fy / scale),
286
+ point ? new Vec2(point.x / scale, point.y / scale) : null,
287
+ wake
288
+ );
289
+ }
290
+
291
+ /**
292
+ * @method applyForceToCenter
293
+ * @description Applies a force (world units) at the center of mass: no
294
+ * torque, unlike {@link RigidBody#applyForce} with a point.
295
+ * @param {number} fx
296
+ * @param {number} fy
297
+ * @param {boolean} [wake=true]
298
+ */
299
+ applyForceToCenter(fx, fy, wake = true) {
300
+ const scale = this.physics.scale;
301
+ this.body.applyForceToCenter(new Vec2(fx / scale, fy / scale), wake);
302
+ }
303
+
304
+ /**
305
+ * @method applyTorque
306
+ * @description Applies a torque about the center of mass.
307
+ * @param {number} torque
308
+ * @param {boolean} [wake=true]
309
+ */
310
+ applyTorque(torque, wake = true) {
311
+ this.body.applyTorque(torque, wake);
312
+ }
313
+
314
+ /**
315
+ * @method setAwake
316
+ * @description Wakes or sleeps the body.
317
+ * @param {boolean} awake
318
+ */
319
+ setAwake(awake) {
320
+ this.body.setAwake(awake);
321
+ }
322
+
323
+ /**
324
+ * @method isAwake
325
+ * @description Whether the body is currently simulated.
326
+ * @returns {boolean}
327
+ */
328
+ isAwake() {
329
+ return this.body.isAwake();
330
+ }
331
+
332
+ /**
333
+ * @method setSleepingAllowed
334
+ * @description Allows or forbids this body from ever sleeping.
335
+ * @param {boolean} flag
336
+ */
337
+ setSleepingAllowed(flag) {
338
+ this.body.setSleepingAllowed(flag);
339
+ }
340
+
341
+ /**
342
+ * @method isSleepingAllowed
343
+ * @returns {boolean}
344
+ */
345
+ isSleepingAllowed() {
346
+ return this.body.isSleepingAllowed();
347
+ }
348
+
349
+ /**
350
+ * @method setActive
351
+ * @description Adds or removes the body from collision detection without
352
+ * destroying it.
353
+ * @param {boolean} flag
354
+ */
355
+ setActive(flag) {
356
+ this.body.setActive(flag);
357
+ }
358
+
359
+ /**
360
+ * @method isActive
361
+ * @returns {boolean}
362
+ */
363
+ isActive() {
364
+ return this.body.isActive();
365
+ }
366
+
367
+ /**
368
+ * @method setFixedRotation
369
+ * @description Locks or unlocks the body's rotation at runtime.
370
+ * @param {boolean} flag
371
+ */
372
+ setFixedRotation(flag) {
373
+ this.body.setFixedRotation(flag);
374
+ }
375
+
376
+ /**
377
+ * @method isFixedRotation
378
+ * @returns {boolean}
379
+ */
380
+ isFixedRotation() {
381
+ return this.body.isFixedRotation();
382
+ }
383
+
384
+ /**
385
+ * @method setContinuous
386
+ * @description Enables continuous collision detection (CCD) for this body by
387
+ * marking it a "bullet". Fast-moving bodies (e.g. a dash, a projectile, a
388
+ * player falling at high speed) otherwise sweep so far in a single fixed step
389
+ * that they tunnel straight through thin static geometry; with CCD on, the
390
+ * engine sweeps the body against static geometry so it stops at the wall
391
+ * instead of teleporting past it. Costs more per step, so reserve it for the
392
+ * handful of bodies that actually move fast.
393
+ * @param {boolean} [enabled=true]
394
+ * @returns {RigidBody} - this
395
+ */
396
+ setContinuous(enabled = true) {
397
+ this.body.setBullet(!!enabled);
398
+ return this;
399
+ }
400
+
401
+ /**
402
+ * @method isContinuous
403
+ * @description Whether CCD (bullet mode) is enabled for this body.
404
+ * @returns {boolean}
405
+ */
406
+ isContinuous() {
407
+ return this.body.isBullet();
408
+ }
409
+
410
+ /**
411
+ * @method setLinearDamping
412
+ * @description Sets the drag applied to linear motion each step.
413
+ * @param {number} damping
414
+ */
415
+ setLinearDamping(damping) {
416
+ this.body.setLinearDamping(damping);
417
+ }
418
+
419
+ /**
420
+ * @method getLinearDamping
421
+ * @returns {number}
422
+ */
423
+ getLinearDamping() {
424
+ return this.body.getLinearDamping();
425
+ }
426
+
427
+ /**
428
+ * @method setAngularDamping
429
+ * @description Sets the drag applied to rotation each step.
430
+ * @param {number} damping
431
+ */
432
+ setAngularDamping(damping) {
433
+ this.body.setAngularDamping(damping);
434
+ }
435
+
436
+ /**
437
+ * @method getAngularDamping
438
+ * @returns {number}
439
+ */
440
+ getAngularDamping() {
441
+ return this.body.getAngularDamping();
442
+ }
443
+
444
+ /**
445
+ * @method setGravityScale
446
+ * @description Scales how strongly gravity pulls on this body (0 disables
447
+ * it, 2 makes it twice as heavy-feeling).
448
+ * @param {number} scale
449
+ */
450
+ setGravityScale(scale) {
451
+ this.body.setGravityScale(scale);
452
+ }
453
+
454
+ /**
455
+ * @method getGravityScale
456
+ * @returns {number}
457
+ */
458
+ getGravityScale() {
459
+ return this.body.getGravityScale();
460
+ }
461
+
462
+ /**
463
+ * @method getMass
464
+ * @description Returns the body's mass, in physics units (derived from its
465
+ * fixtures' shapes and densities, unaffected by the pixel scale).
466
+ * @returns {number}
467
+ */
468
+ getMass() {
469
+ return this.body.getMass();
470
+ }
471
+
472
+ /**
473
+ * @method getInertia
474
+ * @description Returns the body's rotational inertia about its center of
475
+ * mass, in physics units.
476
+ * @returns {number}
477
+ */
478
+ getInertia() {
479
+ return this.body.getInertia();
480
+ }
481
+
482
+ /**
483
+ * @method resetMassData
484
+ * @description Re-derives mass/center/inertia from the body's current
485
+ * fixtures. Call this after changing a fixture's density at runtime.
486
+ */
487
+ resetMassData() {
488
+ this.body.resetMassData();
489
+ }
490
+
491
+ /**
492
+ * @method setMassData
493
+ * @description Overrides the computed mass properties directly.
494
+ * @param {Object} massData - `{ mass, center, I }`; `center` is a world
495
+ * (pixel) offset from the body's origin
496
+ */
497
+ setMassData(massData) {
498
+ const scale = this.physics.scale;
499
+ const center = massData.center || { x: 0, y: 0 };
500
+ this.body.setMassData({
501
+ mass: massData.mass,
502
+ center: new Vec2(center.x / scale, center.y / scale),
503
+ I: massData.I,
504
+ });
505
+ }
506
+
507
+ /**
508
+ * @method getWorldPoint
509
+ * @description Converts a point local to this body into world (pixel) space.
510
+ * @param {Object} localPoint - World-unit offset from the body's origin
511
+ * @returns {Vector2}
512
+ */
513
+ getWorldPoint(localPoint) {
514
+ const scale = this.physics.scale;
515
+ const p = this.body.getWorldPoint(
516
+ new Vec2(localPoint.x / scale, localPoint.y / scale)
517
+ );
518
+ return new Vector2(p.x * scale, p.y * scale);
519
+ }
520
+
521
+ /**
522
+ * @method getLocalPoint
523
+ * @description Converts a world (pixel) point into this body's local frame.
524
+ * @param {Object} worldPoint - World (pixel) units
525
+ * @returns {Vector2}
526
+ */
527
+ getLocalPoint(worldPoint) {
528
+ const scale = this.physics.scale;
529
+ const p = this.body.getLocalPoint(
530
+ new Vec2(worldPoint.x / scale, worldPoint.y / scale)
531
+ );
532
+ return new Vector2(p.x * scale, p.y * scale);
533
+ }
534
+
535
+ /**
536
+ * @method getWorldVector
537
+ * @description Rotates a local direction (not a point, unaffected by the
538
+ * body's position) into world space.
539
+ * @param {Object} localVector
540
+ * @returns {Vector2}
541
+ */
542
+ getWorldVector(localVector) {
543
+ const scale = this.physics.scale;
544
+ const v = this.body.getWorldVector(
545
+ new Vec2(localVector.x / scale, localVector.y / scale)
546
+ );
547
+ return new Vector2(v.x * scale, v.y * scale);
548
+ }
549
+
550
+ /**
551
+ * @method getLocalVector
552
+ * @description Rotates a world direction into this body's local frame.
553
+ * @param {Object} worldVector
554
+ * @returns {Vector2}
555
+ */
556
+ getLocalVector(worldVector) {
557
+ const scale = this.physics.scale;
558
+ const v = this.body.getLocalVector(
559
+ new Vec2(worldVector.x / scale, worldVector.y / scale)
560
+ );
561
+ return new Vector2(v.x * scale, v.y * scale);
562
+ }
563
+
564
+ /**
565
+ * @method getContactList
566
+ * @description Returns the raw physics contacts this body currently takes
567
+ * part in. Each contact's `fixtureA`/`fixtureB` point at physics-unit
568
+ * fixtures/bodies, not RigidBody wrappers. Walk `fixture.body.getUserData()`
569
+ * to get back to the owning RigidBody.
570
+ * @returns {Array<Contact>}
571
+ */
572
+ getContactList() {
573
+ return this.body.getContactList();
574
+ }
575
+
576
+ /**
577
+ * @method getWorld
578
+ * @description Returns the raw physics {@link World} this body lives in.
579
+ * @returns {World}
580
+ */
581
+ getWorld() {
582
+ return this.body.getWorld();
583
+ }
584
+
585
+ /**
586
+ * @method getUserData
587
+ * @description Returns whatever you last passed to
588
+ * {@link RigidBody#setUserData}. This is separate from the underlying
589
+ * physics body's own userData slot, which the engine itself uses internally
590
+ * to route collisions back to this RigidBody, so setting it here can never
591
+ * break that.
592
+ * @returns {*}
593
+ */
594
+ getUserData() {
595
+ return this.userData;
596
+ }
597
+
598
+ /**
599
+ * @method setUserData
600
+ * @param {*} data
601
+ */
602
+ setUserData(data) {
603
+ this.userData = data;
604
+ }
605
+
85
606
  /**
86
607
  * @method getPosition
87
- * @description Returns the position of the rigidbody
88
- * @returns {Vector2} - The position of the rigidbody
608
+ * @description Returns the live world-space position of the body (kept in sync
609
+ * with the simulation), not the spawn position. Use getInitialPosition() for
610
+ * the position the body was created at.
611
+ * @returns {Vector2} - The current world-space position of the rigidbody
89
612
  */
90
613
  getPosition() {
614
+ return new Vector2(this.getWorldX(), this.getWorldY());
615
+ }
616
+
617
+ /**
618
+ * @method getInitialPosition
619
+ * @description Returns the world-space position the body was created at.
620
+ * @returns {Vector2} - The spawn position of the rigidbody
621
+ */
622
+ getInitialPosition() {
91
623
  return this.position;
92
624
  }
93
625
 
@@ -111,8 +643,11 @@ class RigidBody {
111
643
 
112
644
  /**
113
645
  * @method getBody
114
- * @description Returns the body of the rigidbody
115
- * @returns {planck.Body} - The body of the rigidbody
646
+ * @description Returns the underlying, physics-unit {@link Body}. Nothing in
647
+ * ordinary use needs this: every common operation is a method on RigidBody
648
+ * itself, in world/pixel units, but it's here for advanced cases (writing a
649
+ * custom joint or solver hook) that need the raw physics object.
650
+ * @returns {Body} - The body of the rigidbody
116
651
  */
117
652
  getBody() {
118
653
  return this.body;
@@ -142,7 +677,7 @@ class RigidBody {
142
677
  */
143
678
  detachCollider(collider) {
144
679
  if (collider && collider.getCollider()) {
145
- this.body.destroyFixture(collider.getCollider());
680
+ this.destroyFixture(collider.getCollider());
146
681
  this.collider = null;
147
682
  }
148
683
  }
@@ -164,6 +699,19 @@ class RigidBody {
164
699
  getType() {
165
700
  return this.type;
166
701
  }
702
+
703
+ /**
704
+ * @method setType
705
+ * @description Changes the body type at runtime (e.g. turning a kinematic
706
+ * moving platform into a dynamic one when it breaks apart). Resets
707
+ * velocities and re-derives mass; existing contacts are dropped so they
708
+ * rebuild against the new type.
709
+ * @param {string | "static" | "dynamic" | "kinematic"} type
710
+ */
711
+ setType(type) {
712
+ this.type = type;
713
+ this.body.setType(type);
714
+ }
167
715
  }
168
716
 
169
717
  export default RigidBody;