littlejsengine 1.11.17 → 1.12.4

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 (62) hide show
  1. package/dist/littlejs.d.ts +112 -43
  2. package/dist/littlejs.esm.js +586 -500
  3. package/dist/littlejs.esm.min.js +1 -1
  4. package/dist/littlejs.js +2993 -122
  5. package/dist/littlejs.min.js +1 -1
  6. package/dist/littlejs.release.js +2992 -119
  7. package/examples/box2d/game.js +70 -39
  8. package/examples/box2d/gameObjects.js +81 -77
  9. package/examples/box2d/index.html +2 -7
  10. package/examples/box2d/scenes.js +80 -90
  11. package/examples/breakout/game.js +63 -28
  12. package/examples/breakout/gameObjects.js +45 -47
  13. package/examples/breakout/index.html +1 -5
  14. package/examples/breakoutTutorial/game.js +36 -29
  15. package/examples/breakoutTutorial/index.html +1 -3
  16. package/examples/empty/game.js +5 -2
  17. package/examples/empty/index.html +1 -3
  18. package/examples/htmlMenu/game.js +24 -15
  19. package/examples/htmlMenu/index.html +5 -7
  20. package/examples/index.html +2 -3
  21. package/examples/module/game.js +32 -34
  22. package/examples/module/index.html +1 -1
  23. package/examples/particles/index.html +27 -22
  24. package/examples/platformer/game.js +71 -45
  25. package/examples/platformer/gameCharacter.js +42 -34
  26. package/examples/platformer/gameEffects.js +82 -75
  27. package/examples/platformer/gameLevel.js +67 -38
  28. package/examples/platformer/{gameLevelData.js → gameLevelData.json} +1 -11
  29. package/examples/platformer/gameObjects.js +70 -64
  30. package/examples/platformer/gamePlayer.js +12 -7
  31. package/examples/platformer/index.html +1 -9
  32. package/examples/puzzle/game.js +59 -40
  33. package/examples/puzzle/index.html +1 -3
  34. package/examples/shorts/base.html +2 -2
  35. package/examples/shorts/particles.js +1 -1
  36. package/examples/shorts/platformer.js +1 -1
  37. package/examples/shorts/tileLayer.js +5 -6
  38. package/examples/shorts/tiles.png +0 -0
  39. package/examples/starter/game.js +9 -12
  40. package/examples/starter/index.html +13 -13
  41. package/examples/stress/index.html +44 -44
  42. package/examples/typescript/build.js +17 -18
  43. package/examples/typescript/game.js +32 -34
  44. package/examples/typescript/game.ts +32 -34
  45. package/examples/typescript/index.html +1 -1
  46. package/examples/uiSystem/game.js +34 -31
  47. package/examples/uiSystem/index.html +1 -5
  48. package/package.json +1 -1
  49. package/plugins/box2d.js +6 -2
  50. package/plugins/pluginExport.js +1 -2
  51. package/reference.md +29 -27
  52. package/src/engine.js +1 -1
  53. package/src/engineBuild.js +13 -6
  54. package/src/engineDebug.js +1 -3
  55. package/src/engineExport.js +2 -6
  56. package/src/engineInput.js +3 -1
  57. package/src/engineObject.js +18 -14
  58. package/src/engineSettings.js +6 -6
  59. package/src/engineTileLayer.js +179 -96
  60. package/examples/typescript/build/dist/littlejs.esm.js +0 -4834
  61. package/examples/typescript/build/examples/typescript/build.js +0 -24
  62. package/examples/typescript/build/examples/typescript/game.js +0 -102
package/dist/littlejs.js CHANGED
@@ -334,11 +334,9 @@ function debugRender()
334
334
  }
335
335
  }
336
336
 
337
- if (tileCollisionSize.x > 0 && tileCollisionSize.y > 0)
337
+ if (tileCollisionLayers.length) // show floored tile pick if there is tile collision
338
338
  drawRect(mousePos.floor().add(vec2(.5)), vec2(1), rgb(0,0,1,.5), 0, false);
339
339
  mainContext = saveContext;
340
-
341
- //glCopyToContext(mainContext = saveContext);
342
340
  }
343
341
 
344
342
  {
@@ -1530,7 +1528,7 @@ let canvasMaxSize = vec2(1920, 1080);
1530
1528
  * @memberof Settings */
1531
1529
  let canvasFixedSize = vec2();
1532
1530
 
1533
- /** Use nearest neighbor scaling algorithm for canvas for more pixelated look
1531
+ /** Use nearest neighbor canvas scaling for more pixelated look
1534
1532
  * - Must be set before startup to take effect
1535
1533
  * - If enabled sets css image-rendering:pixelated
1536
1534
  * @type {boolean}
@@ -1640,11 +1638,11 @@ let objectDefaultFriction = .8;
1640
1638
  * @memberof Settings */
1641
1639
  let objectMaxSpeed = 1;
1642
1640
 
1643
- /** How much gravity to apply to objects along the Y axis, negative is down
1644
- * @type {number}
1641
+ /** How much gravity to apply to objects, negative Y is down
1642
+ * @type {Vector2}
1645
1643
  * @default
1646
1644
  * @memberof Settings */
1647
- let gravity = 0;
1645
+ let gravity = vec2();
1648
1646
 
1649
1647
  /** Scales emit rate of particles, useful for low graphics mode (0 disables particle emitters)
1650
1648
  * @type {number}
@@ -1870,8 +1868,8 @@ function setObjectDefaultFriction(friction) { objectDefaultFriction = friction;
1870
1868
  * @memberof Settings */
1871
1869
  function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
1872
1870
 
1873
- /** Set how much gravity to apply to objects along the Y axis
1874
- * @param {number} newGravity
1871
+ /** Set how much gravity to apply to objects
1872
+ * @param {Vector2} newGravity
1875
1873
  * @memberof Settings */
1876
1874
  function setGravity(newGravity) { gravity = newGravity; }
1877
1875
 
@@ -2139,7 +2137,10 @@ class EngineObject
2139
2137
  this.velocity.x *= this.damping;
2140
2138
  this.velocity.y *= this.damping;
2141
2139
  if (this.mass) // don't apply gravity to static objects
2142
- this.velocity.y += gravity * this.gravityScale;
2140
+ {
2141
+ this.velocity.x += gravity.x * this.gravityScale;
2142
+ this.velocity.y += gravity.y * this.gravityScale;
2143
+ }
2143
2144
  this.pos.x += this.velocity.x;
2144
2145
  this.pos.y += this.velocity.y;
2145
2146
  this.angle += this.angleVelocity *= this.angleDamping;
@@ -2154,9 +2155,9 @@ class EngineObject
2154
2155
  if (this.groundObject)
2155
2156
  {
2156
2157
  // apply friction in local space of ground object
2157
- const groundSpeed = this.groundObject != this && this.groundObject.velocity ?
2158
- this.groundObject.velocity.x : 0;
2159
- this.velocity.x = groundSpeed + (this.velocity.x - groundSpeed) * this.friction;
2158
+ const friction = max(this.friction, this.groundObject.friction);
2159
+ const groundSpeed = this.groundObject.velocity ? this.groundObject.velocity.x : 0;
2160
+ this.velocity.x = groundSpeed + (this.velocity.x - groundSpeed) * friction;
2160
2161
  this.groundObject = undefined;
2161
2162
  //debugOverlay && debugPhysics && debugPoint(this.pos.subtract(vec2(0,this.size.y/2)), '#0f0');
2162
2163
  }
@@ -2198,7 +2199,7 @@ class EngineObject
2198
2199
 
2199
2200
  // check for collision
2200
2201
  const sizeBoth = this.size.add(o.size);
2201
- const smallStepUp = (oldPos.y - o.pos.y)*2 > sizeBoth.y + gravity; // prefer to push up if small delta
2202
+ const smallStepUp = (oldPos.y - o.pos.y)*2 > sizeBoth.y + gravity.y; // prefer to push up if small delta
2202
2203
  const isBlockedX = abs(oldPos.y - o.pos.y)*2 < sizeBoth.y;
2203
2204
  const isBlockedY = abs(oldPos.x - o.pos.x)*2 < sizeBoth.x;
2204
2205
  const elasticity = max(this.elasticity, o.elasticity);
@@ -2260,19 +2261,21 @@ class EngineObject
2260
2261
  if (this.collideTiles)
2261
2262
  {
2262
2263
  // check collision against tiles
2263
- if (tileCollisionTest(this.pos, this.size, this))
2264
+ const hitLayer = tileCollisionTest(this.pos, this.size, this)
2265
+ if (hitLayer)
2264
2266
  {
2265
2267
  // if already was stuck in collision, don't do anything
2266
2268
  // this should not happen unless something starts in collision
2267
2269
  if (!tileCollisionTest(oldPos, this.size, this))
2268
2270
  {
2269
2271
  // test which side we bounced off (or both if a corner)
2270
- const isBlockedY = tileCollisionTest(vec2(oldPos.x, this.pos.y), this.size, this);
2271
- const isBlockedX = tileCollisionTest(vec2(this.pos.x, oldPos.y), this.size, this);
2272
- if (isBlockedY || !isBlockedX)
2272
+ const blockedLayerY = tileCollisionTest(vec2(oldPos.x, this.pos.y), this.size, this);
2273
+ const blockedLayerX = tileCollisionTest(vec2(this.pos.x, oldPos.y), this.size, this);
2274
+ if (blockedLayerY || !blockedLayerX)
2273
2275
  {
2274
2276
  // bounce velocity
2275
- this.velocity.y *= -this.elasticity;
2277
+ const elasticity = max(this.elasticity, hitLayer.elasticity);
2278
+ this.velocity.y *= -elasticity;
2276
2279
 
2277
2280
  if (wasMovingDown)
2278
2281
  {
@@ -2281,9 +2284,8 @@ class EngineObject
2281
2284
  const epsilon = .0001;
2282
2285
  this.pos.y = (oldPos.y-this.size.y/2|0)+this.size.y/2+epsilon;
2283
2286
 
2284
- // set ground object to self for tile collision
2285
- // TODO: rework system so tile collision is its own object
2286
- this.groundObject = this;
2287
+ // set ground object for tile collision
2288
+ this.groundObject = hitLayer;
2287
2289
  }
2288
2290
  else
2289
2291
  {
@@ -2292,7 +2294,7 @@ class EngineObject
2292
2294
  this.groundObject = undefined;
2293
2295
  }
2294
2296
  }
2295
- if (isBlockedX)
2297
+ if (blockedLayerX)
2296
2298
  {
2297
2299
  // move to previous position and bounce
2298
2300
  this.pos.x = oldPos.x;
@@ -3392,7 +3394,9 @@ function gamepadsUpdate()
3392
3394
  const button = gamepad.buttons[j];
3393
3395
  const wasDown = gamepadIsDown(j,i);
3394
3396
  data[j] = button.pressed ? wasDown ? 1 : 3 : wasDown ? 4 : 0;
3395
- isUsingGamepad ||= !i && button.pressed;
3397
+ if (!button.value || button.value > .9) // must be a full press
3398
+ if (!i && button.pressed)
3399
+ isUsingGamepad = true;
3396
3400
  }
3397
3401
 
3398
3402
  if (gamepadDirectionEmulateStick)
@@ -4067,75 +4071,47 @@ function zzfxG
4067
4071
  * LittleJS Tile Layer System
4068
4072
  * - Caches arrays of tiles to off screen canvas for fast rendering
4069
4073
  * - Unlimited numbers of layers, allocates canvases as needed
4070
- * - Interfaces with EngineObject for collision
4071
- * - Collision layer is separate from visible layers
4072
- * - It is recommended to have a visible layer that matches the collision
4073
4074
  * - Tile layers can be drawn to using their context with canvas2d
4074
4075
  * - Drawn directly to the main canvas without using WebGL
4076
+ * - Tile layers can also have collision with EngineObjects
4075
4077
  * @namespace TileCollision
4076
4078
  */
4077
4079
 
4078
4080
 
4079
4081
 
4080
- /** The tile collision layer grid, use setTileCollisionData and getTileCollisionData to access
4081
- * @type {Array<number>}
4082
- * @memberof TileCollision */
4083
- let tileCollision = [];
4084
-
4085
- /** Size of the tile collision layer 2d grid
4086
- * @type {Vector2}
4087
- * @memberof TileCollision */
4088
- let tileCollisionSize = vec2();
4089
-
4090
- /** Clear and initialize tile collision
4091
- * @param {Vector2} size - width and height of tile collision 2d grid
4092
- * @memberof TileCollision */
4093
- function initTileCollision(size)
4094
- {
4095
- tileCollisionSize = size;
4096
- tileCollision = [];
4097
- for (let i=tileCollision.length = tileCollisionSize.area(); i--;)
4098
- tileCollision[i] = 0;
4099
- }
4082
+ ///////////////////////////////////////////////////////////////////////////////
4083
+ // Tile Layer System
4100
4084
 
4101
- /** Set tile collision data for a given cell in the grid
4102
- * @param {Vector2} pos
4103
- * @param {number} [data]
4085
+ /** Keep track of all tile layers with collision
4086
+ * @type {Array<TileCollisionLayer>}
4104
4087
  * @memberof TileCollision */
4105
- function setTileCollisionData(pos, data=0)
4106
- {
4107
- pos.arrayCheck(tileCollisionSize) && (tileCollision[(pos.y|0)*tileCollisionSize.x+pos.x|0] = data);
4108
- }
4088
+ let tileCollisionLayers = [];
4109
4089
 
4110
4090
  /** Get tile collision data for a given cell in the grid
4111
- * @param {Vector2} pos
4112
- * @return {number}
4113
- * @memberof TileCollision */
4091
+ * @param {Vector2} pos
4092
+ * @return {number}
4093
+ * @memberof TileCollision */
4114
4094
  function getTileCollisionData(pos)
4115
4095
  {
4116
- return pos.arrayCheck(tileCollisionSize) ? tileCollision[(pos.y|0)*tileCollisionSize.x+pos.x|0] : 0;
4096
+ // check all tile collision layers
4097
+ for (const layer of tileCollisionLayers)
4098
+ if (pos.arrayCheck(layer.size))
4099
+ return layer.getCollisionData(pos);
4100
+ return 0;
4117
4101
  }
4118
4102
 
4119
- /** Check if collision with another object should occur
4103
+ /** Check if a tile layer collides with another object
4120
4104
  * @param {Vector2} pos
4121
4105
  * @param {Vector2} [size=(0,0)]
4122
4106
  * @param {EngineObject} [object]
4123
- * @return {boolean}
4107
+ * @return {TileCollisionLayer}
4124
4108
  * @memberof TileCollision */
4125
4109
  function tileCollisionTest(pos, size=vec2(), object)
4126
4110
  {
4127
- const minX = max(pos.x - size.x/2|0, 0);
4128
- const minY = max(pos.y - size.y/2|0, 0);
4129
- const maxX = min(pos.x + size.x/2, tileCollisionSize.x);
4130
- const maxY = min(pos.y + size.y/2, tileCollisionSize.y);
4131
- for (let y = minY; y < maxY; ++y)
4132
- for (let x = minX; x < maxX; ++x)
4133
- {
4134
- const tileData = tileCollision[y*tileCollisionSize.x+x];
4135
- if (tileData && (!object || object.collideWithTile(tileData, vec2(x, y))))
4136
- return true;
4137
- }
4138
- return false;
4111
+ // check all tile collision layers
4112
+ for (const layer of tileCollisionLayers)
4113
+ if (layer.collisionTest(pos, size, object))
4114
+ return layer;
4139
4115
  }
4140
4116
 
4141
4117
  /** Return the center of first tile hit, undefined if nothing was hit.
@@ -4147,49 +4123,17 @@ function tileCollisionTest(pos, size=vec2(), object)
4147
4123
  * @memberof TileCollision */
4148
4124
  function tileCollisionRaycast(posStart, posEnd, object)
4149
4125
  {
4150
- // test if a ray collides with tiles from start to end
4151
- // todo: a way to get the exact hit point, it must still be inside the hit tile
4152
- const delta = posEnd.subtract(posStart);
4153
- const totalLength = delta.length();
4154
- const normalizedDelta = delta.normalize();
4155
- const unit = vec2(abs(1/normalizedDelta.x), abs(1/normalizedDelta.y));
4156
- const flooredPosStart = posStart.floor();
4157
-
4158
- // setup iteration variables
4159
- let pos = flooredPosStart;
4160
- let xi = unit.x * (delta.x < 0 ? posStart.x - pos.x : pos.x - posStart.x + 1);
4161
- let yi = unit.y * (delta.y < 0 ? posStart.y - pos.y : pos.y - posStart.y + 1);
4162
-
4163
- while (true)
4164
- {
4165
- // check for tile collision
4166
- const tileData = getTileCollisionData(pos);
4167
- if (tileData && (!object || object.collideWithTile(tileData, pos)))
4168
- {
4169
- debugRaycast && debugLine(posStart, posEnd, '#f00', .02);
4170
- debugRaycast && debugPoint(pos.add(vec2(.5)), '#ff0');
4171
- return pos.add(vec2(.5));
4172
- }
4173
-
4174
- // check if past the end
4175
- if (xi > totalLength && yi > totalLength)
4176
- break;
4177
-
4178
- // get coordinates of the next tile to check
4179
- if (xi > yi)
4180
- pos.y += sign(delta.y), yi += unit.y;
4181
- else
4182
- pos.x += sign(delta.x), xi += unit.x;
4126
+ // check all tile collision layers
4127
+ for (const layer of tileCollisionLayers)
4128
+ {
4129
+ const hitPos = layer.collisionRaycast(posStart, posEnd, object)
4130
+ if (hitPos)
4131
+ return hitPos;
4183
4132
  }
4184
-
4185
- debugRaycast && debugLine(posStart, posEnd, '#00f', .02);
4186
4133
  }
4187
4134
 
4188
- ///////////////////////////////////////////////////////////////////////////////
4189
- // Tile Layer Rendering System
4190
-
4191
4135
  /**
4192
- * Tile layer data object stores info about how to render a tile
4136
+ * Tile layer data object stores info about how to draw a tile
4193
4137
  * @example
4194
4138
  * // create tile layer data with tile index 0 and random orientation and color
4195
4139
  * const tileIndex = 0;
@@ -4221,28 +4165,27 @@ class TileLayerData
4221
4165
  clear() { this.tile = this.direction = 0; this.mirror = false; this.color = new Color; }
4222
4166
  }
4223
4167
 
4168
+ ///////////////////////////////////////////////////////////////////////////////
4224
4169
  /**
4225
4170
  * Tile Layer - cached rendering system for tile layers
4226
4171
  * - Each Tile layer is rendered to an off screen canvas
4227
4172
  * - To allow dynamic modifications, layers are rendered using canvas 2d
4228
4173
  * - Some devices like mobile phones are limited to 4k texture resolution
4229
- * - So with 16x16 tiles this limits layers to 256x256 on mobile devices
4174
+ * - For with 16x16 tiles this limits layers to 256x256 on mobile devices
4230
4175
  * @extends EngineObject
4231
4176
  * @example
4232
- * // create tile collision and visible tile layer
4233
- * initTileCollision(vec2(200,100));
4234
- * const tileLayer = new TileLayer();
4177
+ * const tileLayer = new TileLayer(vec2(), vec2(200,100));
4235
4178
  */
4236
4179
  class TileLayer extends EngineObject
4237
4180
  {
4238
4181
  /** Create a tile layer object
4239
- * @param {Vector2} [position=(0,0)] - World space position
4240
- * @param {Vector2} [size=tileCollisionSize] - World space size
4241
- * @param {TileInfo} [tileInfo] - Tile info for layer
4242
- * @param {Vector2} [scale=(1,1)] - How much to scale this layer when rendered
4243
- * @param {number} [renderOrder] - Objects are sorted by renderOrder
4182
+ * @param {Vector2} [position=(0,0)] - World space position
4183
+ * @param {Vector2} [size=(1,1)] - World space size
4184
+ * @param {TileInfo} [tileInfo] - Tile info for layer
4185
+ * @param {Vector2} [scale=(1,1)] - How much to scale this layer when rendered
4186
+ * @param {number} [renderOrder] - Objects are sorted by renderOrder
4244
4187
  */
4245
- constructor(position, size=tileCollisionSize, tileInfo=tile(), scale=vec2(1), renderOrder=0)
4188
+ constructor(position, size, tileInfo=tile(), scale=vec2(1), renderOrder=0)
4246
4189
  {
4247
4190
  super(position, size, tileInfo, 0, undefined, renderOrder);
4248
4191
 
@@ -4254,6 +4197,10 @@ class TileLayer extends EngineObject
4254
4197
  this.scale = scale;
4255
4198
  /** @property {boolean} - If true this layer will render to overlay canvas and appear above all objects */
4256
4199
  this.isOverlay = false;
4200
+ // set no friction by default, applied friction is max of both objects
4201
+ this.friction = 0;
4202
+ // set no elasticity by default, applied elasticity is max of both objects
4203
+ this.elasticity = 0;
4257
4204
 
4258
4205
  // init tile data
4259
4206
  this.data = [];
@@ -4450,6 +4397,146 @@ class TileLayer extends EngineObject
4450
4397
  * @param {number} [angle=0] */
4451
4398
  drawRect(pos, size, color, angle)
4452
4399
  { this.drawTile(pos, size, undefined, color, angle); }
4400
+ }
4401
+
4402
+ ///////////////////////////////////////////////////////////////////////////////
4403
+ /**
4404
+ * Tile Collision Layer - a tile layer with collision
4405
+ * - adds collision data and functions to TileLayer
4406
+ * - there can be multiple tile collision layers
4407
+ * - tile collison layers should not overlap each other
4408
+ * @extends TileLayer
4409
+ */
4410
+ class TileCollisionLayer extends TileLayer
4411
+ {
4412
+ /** Create a tile layer object
4413
+ * @param {Vector2} [position=(0,0)] - World space position
4414
+ * @param {Vector2} [size=(0,0)] - World space size
4415
+ * @param {TileInfo} [tileInfo] - Tile info for layer
4416
+ * @param {number} [renderOrder] - Objects are sorted by renderOrder
4417
+ */
4418
+ constructor(position, size, tileInfo=tile(), renderOrder=0)
4419
+ {
4420
+ const scale = vec2(1); // collision layers are not scaled
4421
+ super(position, size.floor(), tileInfo, scale, renderOrder);
4422
+
4423
+ /** @property {Array<number>} - The tile collision grid */
4424
+ this.collisionData = [];
4425
+ this.initCollision(this.size);
4426
+
4427
+ // keep track of all collision layers
4428
+ tileCollisionLayers.push(this);
4429
+ }
4430
+
4431
+ /** Destroy this collision layer */
4432
+ destroy()
4433
+ {
4434
+ if (this.destroyed)
4435
+ return;
4436
+
4437
+ // remove from collision layers array and destroy
4438
+ const index = tileCollisionLayers.indexOf(this);
4439
+ ASSERT(index >= 0, 'tile collision layer not found in array');
4440
+ tileCollisionLayers.splice(index, 1);
4441
+ super.destroy();
4442
+ }
4443
+
4444
+ /** Clear and initialize tile collision to new size
4445
+ * @param {Vector2} size - width and height of tile collision 2d grid */
4446
+ initCollision(size)
4447
+ {
4448
+ this.size = size.floor();
4449
+ this.collisionData = [];
4450
+ this.collisionData.length = size.area();
4451
+ this.collisionData.fill(0);
4452
+ }
4453
+
4454
+ /** Set tile collision data for a given cell in the grid
4455
+ * @param {Vector2} pos
4456
+ * @param {number} [data] */
4457
+ setCollisionData(pos, data=1)
4458
+ {
4459
+ const i = (pos.y|0)*this.size.x + pos.x|0;
4460
+ pos.arrayCheck(this.size) && (this.collisionData[i] = data);
4461
+ }
4462
+
4463
+ /** Get tile collision data for a given cell in the grid
4464
+ * @param {Vector2} pos
4465
+ * @return {number} */
4466
+ getCollisionData(pos)
4467
+ {
4468
+ const i = (pos.y|0)*this.size.x + pos.x|0;
4469
+ return pos.arrayCheck(this.size) ? this.collisionData[i] : 0;
4470
+ }
4471
+
4472
+ /** Check if collision with another object should occur
4473
+ * @param {Vector2} pos
4474
+ * @param {Vector2} [size=(0,0)]
4475
+ * @param {EngineObject} [object]
4476
+ * @return {boolean} */
4477
+ collisionTest(pos, size=vec2(), object)
4478
+ {
4479
+ const minX = max(pos.x - size.x/2|0, 0);
4480
+ const minY = max(pos.y - size.y/2|0, 0);
4481
+ const maxX = min(pos.x + size.x/2, this.size.x);
4482
+ const maxY = min(pos.y + size.y/2, this.size.y);
4483
+ for (let y = minY; y < maxY; ++y)
4484
+ for (let x = minX; x < maxX; ++x)
4485
+ {
4486
+ // check if the object should collide with this tile
4487
+ const tileData = this.collisionData[y*this.size.x+x];
4488
+ if (tileData && (!object || object.collideWithTile(tileData, vec2(x, y))))
4489
+ return true;
4490
+ }
4491
+ return false;
4492
+ }
4493
+
4494
+ /** Return the center of first tile hit, undefined if nothing was hit.
4495
+ * This does not return the exact intersection, but the center of the tile hit.
4496
+ * @param {Vector2} posStart
4497
+ * @param {Vector2} posEnd
4498
+ * @param {EngineObject} [object]
4499
+ * @return {Vector2} */
4500
+ collisionRaycast(posStart, posEnd, object)
4501
+ {
4502
+ // test if a ray collides with tiles from start to end
4503
+ // todo: a way to get the exact hit point, it must still be inside the hit tile
4504
+ const delta = posEnd.subtract(posStart);
4505
+ const totalLength = delta.length();
4506
+ const normalizedDelta = delta.normalize();
4507
+ const unit = vec2(abs(1/normalizedDelta.x), abs(1/normalizedDelta.y));
4508
+ const flooredPosStart = posStart.floor();
4509
+
4510
+ // setup iteration variables
4511
+ let pos = flooredPosStart;
4512
+ let xi = unit.x * (delta.x < 0 ? posStart.x - pos.x : pos.x - posStart.x + 1);
4513
+ let yi = unit.y * (delta.y < 0 ? posStart.y - pos.y : pos.y - posStart.y + 1);
4514
+
4515
+ // use line drawing algorithm to test for collisions
4516
+ while (true)
4517
+ {
4518
+ // check for tile collision
4519
+ const tileData = this.getCollisionData(pos);
4520
+ if (tileData && (!object || object.collideWithTile(tileData, pos)))
4521
+ {
4522
+ debugRaycast && debugLine(posStart, posEnd, '#f00', .02);
4523
+ debugRaycast && debugPoint(pos.add(vec2(.5)), '#ff0');
4524
+ return pos.add(vec2(.5));
4525
+ }
4526
+
4527
+ // check if past the end
4528
+ if (xi > totalLength && yi > totalLength)
4529
+ break;
4530
+
4531
+ // get coordinates of next tile to check
4532
+ if (xi > yi)
4533
+ pos.y += sign(delta.y), yi += unit.y;
4534
+ else
4535
+ pos.x += sign(delta.x), xi += unit.x;
4536
+ }
4537
+
4538
+ debugRaycast && debugLine(posStart, posEnd, '#00f', .02);
4539
+ }
4453
4540
  }
4454
4541
  /**
4455
4542
  * LittleJS Particle System
@@ -5326,7 +5413,7 @@ const engineName = 'LittleJS';
5326
5413
  * @type {string}
5327
5414
  * @default
5328
5415
  * @memberof Engine */
5329
- const engineVersion = '1.11.17';
5416
+ const engineVersion = '1.12.4';
5330
5417
 
5331
5418
  /** Frames per second to update
5332
5419
  * @type {number}
@@ -5920,3 +6007,2787 @@ function drawEngineSplashScreen(t)
5920
6007
  x.restore();
5921
6008
  }
5922
6009
 
6010
+ /**
6011
+ * LittleJS Newgrounds API
6012
+ * - NewgroundsMedal extends Medal with Newgrounds API functionality
6013
+ * - Call new NewgroundsPlugin() to setup Newgrounds
6014
+ * - Uses CryptoJS for encryption if optional cipher is provided
6015
+ * - Keeps connection alive and logs views
6016
+ * - Functions to interact with scoreboards
6017
+ * - Functions to unlock medals
6018
+ */
6019
+
6020
+
6021
+
6022
+ /** Global Newgrounds object
6023
+ * @type {NewgroundsPlugin}
6024
+ * @memberof Medal */
6025
+ let newgrounds;
6026
+
6027
+ ///////////////////////////////////////////////////////////////////////////////
6028
+ /**
6029
+ * Newgrounds medal auto unlocks in newgrounds API
6030
+ * @extends Medal
6031
+ */
6032
+ class NewgroundsMedal extends Medal
6033
+ {
6034
+ /** Create a newgrounds medal object and adds it to the list of medals
6035
+ * @param {Number} id - The unique identifier of the medal
6036
+ * @param {String} name - Name of the medal
6037
+ * @param {String} [description] - Description of the medal
6038
+ * @param {String} [icon] - Icon for the medal
6039
+ * @param {String} [src] - Image location for the medal
6040
+ */
6041
+ constructor(id, name, description, icon, src)
6042
+ { super(id, name, description, icon, src); }
6043
+
6044
+ /** Unlocks a medal if not already unlocked */
6045
+ unlock()
6046
+ {
6047
+ super.unlock();
6048
+ newgrounds && newgrounds.unlockMedal(this.id);
6049
+ }
6050
+ }
6051
+
6052
+ ///////////////////////////////////////////////////////////////////////////////
6053
+ /**
6054
+ * Newgrounds API object
6055
+ */
6056
+ class NewgroundsPlugin
6057
+ {
6058
+ /** Create the global newgrounds object
6059
+ * @param {string} app_id - The newgrounds App ID
6060
+ * @param {string} [cipher] - The encryption Key (AES-128/Base64)
6061
+ * @param {Object} [cryptoJS] - An instance of CryptoJS, if there is a cipher
6062
+ * @example
6063
+ * // create the newgrounds object, replace the app id with your own
6064
+ * const app_id = 'your_app_id_here';
6065
+ * new NewgroundsPlugin(app_id);
6066
+ */
6067
+ constructor(app_id, cipher, cryptoJS)
6068
+ {
6069
+ ASSERT(!newgrounds, 'there can only be one newgrounds object');
6070
+ ASSERT(!cipher || cryptoJS, 'must provide cryptojs if there is a cipher');
6071
+
6072
+ newgrounds = this; // set global newgrounds object
6073
+ this.app_id = app_id;
6074
+ this.cipher = cipher;
6075
+ this.cryptoJS = cryptoJS;
6076
+ this.host = location ? location.hostname : '';
6077
+
6078
+ // get session id from url search params
6079
+ const url = new URL(location.href);
6080
+ this.session_id = url.searchParams.get('ngio_session_id');
6081
+
6082
+ if (!this.session_id)
6083
+ return; // only use newgrounds when logged in
6084
+
6085
+ // get medals
6086
+ const medalsResult = this.call('Medal.getList');
6087
+ this.medals = medalsResult ? medalsResult.result.data['medals'] : [];
6088
+ debugMedals && console.log(this.medals);
6089
+ for (const newgroundsMedal of this.medals)
6090
+ {
6091
+ const medal = medals[newgroundsMedal['id']];
6092
+ if (medal)
6093
+ {
6094
+ // copy newgrounds medal data
6095
+ medal.image = new Image;
6096
+ medal.image.src = newgroundsMedal['icon'];
6097
+ medal.name = newgroundsMedal['name'];
6098
+ medal.description = newgroundsMedal['description'];
6099
+ medal.unlocked = newgroundsMedal['unlocked'];
6100
+ medal.difficulty = newgroundsMedal['difficulty'];
6101
+ medal.value = newgroundsMedal['value'];
6102
+
6103
+ if (medal.value) // add value to description
6104
+ medal.description = medal.description + ` (${ medal.value })`;
6105
+ }
6106
+ }
6107
+
6108
+ // get scoreboards
6109
+ const scoreboardResult = this.call('ScoreBoard.getBoards');
6110
+ this.scoreboards = scoreboardResult ? scoreboardResult.result.data.scoreboards : [];
6111
+ debugMedals && console.log(this.scoreboards);
6112
+
6113
+ // keep the session alive with a ping every minute
6114
+ const keepAliveMS = 60 * 1e3;
6115
+ setInterval(()=>this.call('Gateway.ping', 0, true), keepAliveMS);
6116
+ }
6117
+
6118
+ /** Send message to unlock a medal by id
6119
+ * @param {number} id - The medal id */
6120
+ unlockMedal(id) { return this.call('Medal.unlock', {'id':id}, true); }
6121
+
6122
+ /** Send message to post score
6123
+ * @param {number} id - The scoreboard id
6124
+ * @param {number} value - The score value */
6125
+ postScore(id, value) { return this.call('ScoreBoard.postScore', {'id':id, 'value':value}, true); }
6126
+
6127
+ /** Get scores from a scoreboard
6128
+ * @param {number} id - The scoreboard id
6129
+ * @param {string} [user] - A user's id or name
6130
+ * @param {number} [social] - If true, only social scores will be loaded
6131
+ * @param {number} [skip] - Number of scores to skip before start
6132
+ * @param {number} [limit] - Number of scores to include in the list
6133
+ * @return {Object} - The response JSON object
6134
+ */
6135
+ getScores(id, user, social=0, skip=0, limit=10)
6136
+ { return this.call('ScoreBoard.getScores', {'id':id, 'user':user, 'social':social, 'skip':skip, 'limit':limit}); }
6137
+
6138
+ /** Send message to log a view */
6139
+ logView() { return this.call('App.logView', {'host':this.host}, true); }
6140
+
6141
+ /** Send a message to call a component of the Newgrounds API
6142
+ * @param {string} component - Name of the component
6143
+ * @param {Object} [parameters] - Parameters to use for call
6144
+ * @param {boolean} [async] - If true, don't wait for response before continuing
6145
+ * @return {Object} - The response JSON object
6146
+ */
6147
+ call(component, parameters, async=false)
6148
+ {
6149
+ const call = {'component':component, 'parameters':parameters};
6150
+ if (this.cipher)
6151
+ {
6152
+ // encrypt using AES-128 Base64 with cryptoJS
6153
+ const cryptoJS = this.cryptoJS;
6154
+ const aesKey = cryptoJS['enc']['Base64']['parse'](this.cipher);
6155
+ const iv = cryptoJS['lib']['WordArray']['random'](16);
6156
+ const encrypted = cryptoJS['AES']['encrypt'](JSON.stringify(call), aesKey, {'iv':iv});
6157
+ call['secure'] = cryptoJS['enc']['Base64']['stringify'](iv.concat(encrypted['ciphertext']));
6158
+ call['parameters'] = 0;
6159
+ }
6160
+
6161
+ // build the input object
6162
+ const input =
6163
+ {
6164
+ 'app_id': this.app_id,
6165
+ 'session_id': this.session_id,
6166
+ 'call': call
6167
+ };
6168
+
6169
+ // build post data
6170
+ const formData = new FormData();
6171
+ formData.append('input', JSON.stringify(input));
6172
+
6173
+ // send post data
6174
+ const xmlHttp = new XMLHttpRequest();
6175
+ const url = 'https://newgrounds.io/gateway_v3.php';
6176
+ xmlHttp.open('POST', url, !debugMedals && async);
6177
+ try { xmlHttp.send(formData); }
6178
+ catch(e)
6179
+ {
6180
+ debugMedals && console.log('newgrounds call failed', e);
6181
+ return;
6182
+ }
6183
+ debugMedals && console.log(xmlHttp.responseText);
6184
+ return xmlHttp.responseText && JSON.parse(xmlHttp.responseText);
6185
+ }
6186
+ }
6187
+
6188
+ /**
6189
+ * LittleJS Post Processing Plugin
6190
+ * - Supports shadertoy style post processing shaders
6191
+ * - call new new PostProcessPlugin() to setup post processing
6192
+ * - can be enabled to pass other canvases through a final shader
6193
+ */
6194
+
6195
+
6196
+
6197
+ ///////////////////////////////////////////////////////////////////////////////
6198
+
6199
+ /** Global Post Process plugin object
6200
+ * @type {PostProcessPlugin} */
6201
+ let postProcess;
6202
+
6203
+ /////////////////////////////////////////////////////////////////////////
6204
+ /**
6205
+ * UI System Global Object
6206
+ */
6207
+ class PostProcessPlugin
6208
+ {
6209
+ /** Create global post processing shader
6210
+ * @param {string} shaderCode
6211
+ * @param {boolean} [includeOverlay]
6212
+ * @example
6213
+ * // create the post process plugin object
6214
+ * new PostProcessPlugin(shaderCode);
6215
+ */
6216
+ constructor(shaderCode, includeOverlay=false)
6217
+ {
6218
+ ASSERT(!postProcess, 'Post process already initialized');
6219
+ postProcess = this;
6220
+
6221
+ if (headlessMode) return;
6222
+ if (!shaderCode) // default shader pass through
6223
+ shaderCode = 'void mainImage(out vec4 c,vec2 p){c=texture(iChannel0,p/iResolution.xy);}';
6224
+
6225
+ /** @property {WebGLProgram} - Shader for post processing */
6226
+ this.shader = glCreateProgram(
6227
+ '#version 300 es\n' + // specify GLSL ES version
6228
+ 'precision highp float;'+ // use highp for better accuracy
6229
+ 'in vec2 p;'+ // position
6230
+ 'void main(){'+ // shader entry point
6231
+ 'gl_Position=vec4(p+p-1.,1,1);'+ // set position
6232
+ '}' // end of shader
6233
+ ,
6234
+ '#version 300 es\n' + // specify GLSL ES version
6235
+ 'precision highp float;'+ // use highp for better accuracy
6236
+ 'uniform sampler2D iChannel0;'+ // input texture
6237
+ 'uniform vec3 iResolution;'+ // size of output texture
6238
+ 'uniform float iTime;'+ // time
6239
+ 'out vec4 c;'+ // out color
6240
+ '\n' + shaderCode + '\n'+ // insert custom shader code
6241
+ 'void main(){'+ // shader entry point
6242
+ 'mainImage(c,gl_FragCoord.xy);'+ // call post process function
6243
+ 'c.a=1.;'+ // always use full alpha
6244
+ '}' // end of shader
6245
+ );
6246
+
6247
+ /** @property {WebGLTexture} - Texture for post processing */
6248
+ this.texture = glCreateTexture();
6249
+
6250
+ /** @property {boolean} - Should overlay canvas be included in post processing */
6251
+ this.includeOverlay = includeOverlay;
6252
+
6253
+ // Render the post processing shader, called automatically by the engine
6254
+ engineAddPlugin(undefined, postProcessRender);
6255
+ function postProcessRender()
6256
+ {
6257
+ if (headlessMode) return;
6258
+
6259
+ // prepare to render post process shader
6260
+ if (glEnable)
6261
+ {
6262
+ glFlush(); // clear out the buffer
6263
+ mainContext.drawImage(glCanvas, 0, 0); // copy to the main canvas
6264
+ }
6265
+ else
6266
+ {
6267
+ // set the viewport
6268
+ glContext.viewport(0, 0, glCanvas.width = mainCanvas.width, glCanvas.height = mainCanvas.height);
6269
+ }
6270
+
6271
+ if (postProcess.includeOverlay)
6272
+ {
6273
+ // copy overlay canvas so it will be included in post processing
6274
+ mainContext.drawImage(overlayCanvas, 0, 0);
6275
+ overlayCanvas.width |= 0;
6276
+ }
6277
+
6278
+ // setup shader program to draw one triangle
6279
+ glContext.useProgram(postProcess.shader);
6280
+ glContext.bindBuffer(glContext.ARRAY_BUFFER, glGeometryBuffer);
6281
+ glContext.pixelStorei(glContext.UNPACK_FLIP_Y_WEBGL, 1);
6282
+ glContext.disable(glContext.BLEND);
6283
+
6284
+ // set textures, pass in the 2d canvas and gl canvas in separate texture channels
6285
+ glContext.activeTexture(glContext.TEXTURE0);
6286
+ glContext.bindTexture(glContext.TEXTURE_2D, postProcess.texture);
6287
+ glContext.texImage2D(glContext.TEXTURE_2D, 0, glContext.RGBA, glContext.RGBA, glContext.UNSIGNED_BYTE, mainCanvas);
6288
+
6289
+ // set vertex position attribute
6290
+ const vertexByteStride = 8;
6291
+ const pLocation = glContext.getAttribLocation(postProcess.shader, 'p');
6292
+ glContext.enableVertexAttribArray(pLocation);
6293
+ glContext.vertexAttribPointer(pLocation, 2, glContext.FLOAT, false, vertexByteStride, 0);
6294
+
6295
+ // set uniforms and draw
6296
+ const uniformLocation = (name)=>glContext.getUniformLocation(postProcess.shader, name);
6297
+ glContext.uniform1i(uniformLocation('iChannel0'), 0);
6298
+ glContext.uniform1f(uniformLocation('iTime'), time);
6299
+ glContext.uniform3f(uniformLocation('iResolution'), mainCanvas.width, mainCanvas.height, 1);
6300
+ glContext.drawArrays(glContext.TRIANGLE_STRIP, 0, 4);
6301
+ }
6302
+ }
6303
+ }
6304
+
6305
+ /**
6306
+ * LittleJS ZzFXM Plugin
6307
+ */
6308
+
6309
+
6310
+
6311
+ /**
6312
+ * Music Object - Stores a zzfx music track for later use
6313
+ *
6314
+ * <a href=https://keithclark.github.io/ZzFXM/>Create music with the ZzFXM tracker.</a>
6315
+ * @example
6316
+ * // create some music
6317
+ * const music_example = new Music(
6318
+ * [
6319
+ * [ // instruments
6320
+ * [,0,400] // simple note
6321
+ * ],
6322
+ * [ // patterns
6323
+ * [ // pattern 1
6324
+ * [ // channel 0
6325
+ * 0, -1, // instrument 0, left speaker
6326
+ * 1, 0, 9, 1 // channel notes
6327
+ * ],
6328
+ * [ // channel 1
6329
+ * 0, 1, // instrument 0, right speaker
6330
+ * 0, 12, 17, -1 // channel notes
6331
+ * ]
6332
+ * ],
6333
+ * ],
6334
+ * [0, 0, 0, 0], // sequence, play pattern 0 four times
6335
+ * 90 // BPM
6336
+ * ]);
6337
+ *
6338
+ * // play the music
6339
+ * music_example.play();
6340
+ */
6341
+ class ZzFXMusic extends Sound
6342
+ {
6343
+ /** Create a music object and cache the zzfx music samples for later use
6344
+ * @param {[Array, Array, Array, number]} zzfxMusic - Array of zzfx music parameters
6345
+ */
6346
+ constructor(zzfxMusic)
6347
+ {
6348
+ super(undefined);
6349
+
6350
+ if (!soundEnable || headlessMode) return;
6351
+ this.randomness = 0;
6352
+ this.sampleChannels = zzfxM(...zzfxMusic);
6353
+ this.sampleRate = zzfxR;
6354
+ }
6355
+
6356
+ /** Play the music
6357
+ * @param {number} [volume=1] - How much to scale volume by
6358
+ * @param {boolean} [loop] - True if the music should loop
6359
+ * @return {AudioBufferSourceNode} - The audio source node
6360
+ */
6361
+ playMusic(volume, loop=false)
6362
+ { return super.play(undefined, volume, 1, 1, loop); }
6363
+ }
6364
+
6365
+ ///////////////////////////////////////////////////////////////////////////////
6366
+ // ZzFX Music Renderer v2.0.3 by Keith Clark and Frank Force
6367
+
6368
+ /** Generate samples for a ZzFM song with given parameters
6369
+ * @param {Array} instruments - Array of ZzFX sound parameters
6370
+ * @param {Array} patterns - Array of pattern data
6371
+ * @param {Array} sequence - Array of pattern indexes
6372
+ * @param {number} [BPM] - Playback speed of the song in BPM
6373
+ * @return {Array} - Left and right channel sample data */
6374
+ function zzfxM(instruments, patterns, sequence, BPM = 125)
6375
+ {
6376
+ let i, j, k;
6377
+ let instrumentParameters;
6378
+ let note;
6379
+ let sample;
6380
+ let patternChannel;
6381
+ let notFirstBeat;
6382
+ let stop;
6383
+ let instrument;
6384
+ let attenuation;
6385
+ let outSampleOffset;
6386
+ let isSequenceEnd;
6387
+ let sampleOffset = 0;
6388
+ let nextSampleOffset;
6389
+ let sampleBuffer = [];
6390
+ let leftChannelBuffer = [];
6391
+ let rightChannelBuffer = [];
6392
+ let channelIndex = 0;
6393
+ let panning = 0;
6394
+ let hasMore = 1;
6395
+ let sampleCache = {};
6396
+ let beatLength = zzfxR / BPM * 60 >> 2;
6397
+
6398
+ // for each channel in order until there are no more
6399
+ for (; hasMore; channelIndex++) {
6400
+
6401
+ // reset current values
6402
+ sampleBuffer = [hasMore = notFirstBeat = outSampleOffset = 0];
6403
+
6404
+ // for each pattern in sequence
6405
+ sequence.forEach((patternIndex, sequenceIndex) => {
6406
+ // get pattern for current channel, use empty 1 note pattern if none found
6407
+ patternChannel = patterns[patternIndex][channelIndex] || [0, 0, 0];
6408
+
6409
+ // check if there are more channels
6410
+ hasMore |= patterns[patternIndex][channelIndex]&&1;
6411
+
6412
+ // get next offset, use the length of first channel
6413
+ nextSampleOffset = outSampleOffset + (patterns[patternIndex][0].length - 2 - (notFirstBeat?0:1)) * beatLength;
6414
+ // for each beat in pattern, plus one extra if end of sequence
6415
+ isSequenceEnd = sequenceIndex == sequence.length - 1;
6416
+ for (i = 2, k = outSampleOffset; i < patternChannel.length + isSequenceEnd; notFirstBeat = ++i) {
6417
+
6418
+ // <channel-note>
6419
+ note = patternChannel[i];
6420
+
6421
+ // stop if end, different instrument or new note
6422
+ stop = i == patternChannel.length + isSequenceEnd - 1 && isSequenceEnd ||
6423
+ instrument != (patternChannel[0] || 0) || note | 0;
6424
+
6425
+ // fill buffer with samples for previous beat, most cpu intensive part
6426
+ for (j = 0; j < beatLength && notFirstBeat;
6427
+
6428
+ // fade off attenuation at end of beat if stopping note, prevents clicking
6429
+ j++ > beatLength - 99 && stop && attenuation < 1? attenuation += 1 / 99 : 0
6430
+ ) {
6431
+ // copy sample to stereo buffers with panning
6432
+ sample = (1 - attenuation) * sampleBuffer[sampleOffset++] / 2 || 0;
6433
+ leftChannelBuffer[k] = (leftChannelBuffer[k] || 0) - sample * panning + sample;
6434
+ rightChannelBuffer[k] = (rightChannelBuffer[k++] || 0) + sample * panning + sample;
6435
+ }
6436
+
6437
+ // set up for next note
6438
+ if (note) {
6439
+ // set attenuation
6440
+ attenuation = note % 1;
6441
+ panning = patternChannel[1] || 0;
6442
+ if (note |= 0) {
6443
+ // get cached sample
6444
+ sampleBuffer = sampleCache[
6445
+ [
6446
+ instrument = patternChannel[sampleOffset = 0] || 0,
6447
+ note
6448
+ ]
6449
+ ] = sampleCache[[instrument, note]] || (
6450
+ // add sample to cache
6451
+ instrumentParameters = [...instruments[instrument]],
6452
+ instrumentParameters[2] = (instrumentParameters[2] || 220) * 2**(note / 12 - 1),
6453
+
6454
+ // allow negative values to stop notes
6455
+ note > 0 ? zzfxG(...instrumentParameters) : []
6456
+ );
6457
+ }
6458
+ }
6459
+ }
6460
+
6461
+ // update the sample offset
6462
+ outSampleOffset = nextSampleOffset;
6463
+ });
6464
+ }
6465
+
6466
+ return [leftChannelBuffer, rightChannelBuffer];
6467
+ }
6468
+
6469
+ /**
6470
+ * LittleJS User Interface Plugin
6471
+ * - call new UISystemPlugin() to setup the UI system
6472
+ * - Nested Menus
6473
+ * - Text
6474
+ * - Buttons
6475
+ * - Checkboxes
6476
+ * - Images
6477
+ */
6478
+
6479
+
6480
+
6481
+ ///////////////////////////////////////////////////////////////////////////////
6482
+
6483
+ /** Global UI system plugin object
6484
+ * @type {UISystemPlugin} */
6485
+ let uiSystem;
6486
+
6487
+ ///////////////////////////////////////////////////////////////////////////////
6488
+ /**
6489
+ * UI System Global Object
6490
+ */
6491
+ class UISystemPlugin
6492
+ {
6493
+ /** Create the global UI system object
6494
+ * @param {CanvasRenderingContext2D} [context]
6495
+ * @example
6496
+ * // create the ui plugin object
6497
+ * new UISystemPlugin;
6498
+ */
6499
+ constructor(context=overlayContext)
6500
+ {
6501
+ ASSERT(!uiSystem, 'UI system already initialized');
6502
+ uiSystem = this;
6503
+
6504
+ /** @property {Color} - Default fill color for UI elements */
6505
+ this.defaultColor = WHITE;
6506
+ /** @property {Color} - Default outline color for UI elements */
6507
+ this.defaultLineColor = BLACK;
6508
+ /** @property {Color} - Default text color for UI elements */
6509
+ this.defaultTextColor = BLACK;
6510
+ /** @property {Color} - Default button color for UI elements */
6511
+ this.defaultButtonColor = hsl(0,0,.5);
6512
+ /** @property {Color} - Default hover color for UI elements */
6513
+ this.defaultHoverColor = hsl(0,0,.7);
6514
+ /** @property {number} - Default line width for UI elements */
6515
+ this.defaultLineWidth = 4;
6516
+ /** @property {string} - Default font for UI elements */
6517
+ this.defaultFont = 'arial';
6518
+ /** @property {Array<UIObject>} - List of all UI elements */
6519
+ this.uiObjects = [];
6520
+ /** @property {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} - Context to render UI elements to */
6521
+ this.uiContext = context;
6522
+
6523
+ engineAddPlugin(uiUpdate, uiRender);
6524
+
6525
+ // setup recursive update and render
6526
+ function uiUpdate()
6527
+ {
6528
+ function updateObject(o)
6529
+ {
6530
+ if (!o.visible)
6531
+ return;
6532
+ if (o.parent)
6533
+ o.pos = o.localPos.add(o.parent.pos);
6534
+ o.update();
6535
+ for(const c of o.children)
6536
+ updateObject(c);
6537
+ }
6538
+ uiSystem.uiObjects.forEach(o=> o.parent || updateObject(o));
6539
+ }
6540
+ function uiRender()
6541
+ {
6542
+ function renderObject(o)
6543
+ {
6544
+ if (!o.visible)
6545
+ return;
6546
+ if (o.parent)
6547
+ o.pos = o.localPos.add(o.parent.pos);
6548
+ o.render();
6549
+ for(const c of o.children)
6550
+ renderObject(c);
6551
+ }
6552
+ uiSystem.uiObjects.forEach(o=> o.parent || renderObject(o));
6553
+ }
6554
+ }
6555
+
6556
+ /** Draw a rectangle to the UI context
6557
+ * @param {Vector2} pos
6558
+ * @param {Vector2} size
6559
+ * @param {Color} [color=uiSystem.defaultColor]
6560
+ * @param {number} [lineWidth=uiSystem.defaultLineWidth]
6561
+ * @param {Color} [lineColor=uiSystem.defaultLineColor] */
6562
+ drawRect(pos, size, color=uiSystem.defaultColor, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor)
6563
+ {
6564
+ uiSystem.uiContext.fillStyle = color.toString();
6565
+ uiSystem.uiContext.beginPath();
6566
+ uiSystem.uiContext.rect(pos.x-size.x/2, pos.y-size.y/2, size.x, size.y);
6567
+ uiSystem.uiContext.fill();
6568
+ if (lineWidth)
6569
+ {
6570
+ uiSystem.uiContext.strokeStyle = lineColor.toString();
6571
+ uiSystem.uiContext.lineWidth = lineWidth;
6572
+ uiSystem.uiContext.stroke();
6573
+ }
6574
+ }
6575
+
6576
+ /** Draw a line to the UI context
6577
+ * @param {Vector2} posA
6578
+ * @param {Vector2} posB
6579
+ * @param {number} [lineWidth=uiSystem.defaultLineWidth]
6580
+ * @param {Color} [lineColor=uiSystem.defaultLineColor] */
6581
+ drawLine(posA, posB, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor)
6582
+ {
6583
+ uiSystem.uiContext.strokeStyle = lineColor.toString();
6584
+ uiSystem.uiContext.lineWidth = lineWidth;
6585
+ uiSystem.uiContext.beginPath();
6586
+ uiSystem.uiContext.lineTo(posA.x, posA.y);
6587
+ uiSystem.uiContext.lineTo(posB.x, posB.y);
6588
+ uiSystem.uiContext.stroke();
6589
+ }
6590
+
6591
+ /** Draw a tile to the UI context
6592
+ * @param {Vector2} pos
6593
+ * @param {Vector2} size
6594
+ * @param {TileInfo} tileInfo
6595
+ * @param {Color} [color=uiSystem.defaultColor]
6596
+ * @param {number} [angle]
6597
+ * @param {boolean} [mirror] */
6598
+ drawTile(pos, size, tileInfo, color=uiSystem.defaultColor, angle=0, mirror=false)
6599
+ {
6600
+ drawTile(pos, size, tileInfo, color, angle, mirror, BLACK, false, true, uiSystem.uiContext);
6601
+ }
6602
+
6603
+ /** Draw text to the UI context
6604
+ * @param {string} text
6605
+ * @param {Vector2} pos
6606
+ * @param {Vector2} size
6607
+ * @param {Color} [color=uiSystem.defaultColor]
6608
+ * @param {number} [lineWidth=uiSystem.defaultLineWidth]
6609
+ * @param {Color} [lineColor=uiSystem.defaultLineColor]
6610
+ * @param {string} [align]
6611
+ * @param {string} [font=uiSystem.defaultFont] */
6612
+ drawText(text, pos, size, color=uiSystem.defaultColor, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor, align='center', font=uiSystem.defaultFont)
6613
+ {
6614
+ drawTextScreen(text, pos, size.y, color, lineWidth, lineColor, align, font, size.x, uiSystem.uiContext);
6615
+ }
6616
+ }
6617
+
6618
+ ///////////////////////////////////////////////////////////////////////////////
6619
+ /**
6620
+ * UI Object - Base level object for all UI elements
6621
+ */
6622
+ class UIObject
6623
+ {
6624
+ /** Create a UIObject
6625
+ * @param {Vector2} [pos=(0,0)]
6626
+ * @param {Vector2} [size=(1,1)]
6627
+ */
6628
+ constructor(pos=vec2(), size=vec2())
6629
+ {
6630
+ /** @property {Vector2} - Local position of the object */
6631
+ this.localPos = pos.copy();
6632
+ /** @property {Vector2} - Screen space position of the object */
6633
+ this.pos = pos.copy();
6634
+ /** @property {Vector2} - Screen space size of the object */
6635
+ this.size = size.copy();
6636
+ /** @property {Color} */
6637
+ this.color = uiSystem.defaultColor;
6638
+ /** @property {Color} */
6639
+ this.lineColor = uiSystem.defaultLineColor;
6640
+ /** @property {Color} */
6641
+ this.textColor = uiSystem.defaultTextColor;
6642
+ /** @property {Color} */
6643
+ this.hoverColor = uiSystem.defaultHoverColor;
6644
+ /** @property {number} */
6645
+ this.lineWidth = uiSystem.defaultLineWidth;
6646
+ /** @property {string} */
6647
+ this.font = uiSystem.defaultFont;
6648
+ /** @property {boolean} */
6649
+ this.visible = true;
6650
+ /** @property {Array<UIObject>} */
6651
+ this.children = [];
6652
+ /** @property {UIObject} */
6653
+ this.parent = undefined;
6654
+ uiSystem.uiObjects.push(this);
6655
+ }
6656
+
6657
+ /** Add a child UIObject to this object
6658
+ * @param {UIObject} child
6659
+ */
6660
+ addChild(child)
6661
+ {
6662
+ ASSERT(!child.parent && !this.children.includes(child));
6663
+ this.children.push(child);
6664
+ child.parent = this;
6665
+ }
6666
+
6667
+ /** Remove a child UIObject from this object
6668
+ * @param {UIObject} child
6669
+ */
6670
+ removeChild(child)
6671
+ {
6672
+ ASSERT(child.parent == this && this.children.includes(child));
6673
+ this.children.splice(this.children.indexOf(child), 1);
6674
+ child.parent = undefined;
6675
+ }
6676
+
6677
+ /** Update the object, called automatically by plugin once each frame */
6678
+ update()
6679
+ {
6680
+ // track mouse input
6681
+ const mouseWasOver = this.mouseIsOver;
6682
+ const mouseDown = mouseIsDown(0);
6683
+ if (!mouseDown || isTouchDevice)
6684
+ {
6685
+ this.mouseIsOver = isOverlapping(this.pos, this.size, mousePosScreen);
6686
+ if (!mouseDown && isTouchDevice)
6687
+ this.mouseIsOver = false;
6688
+ if (this.mouseIsOver && !mouseWasOver)
6689
+ this.onEnter();
6690
+ if (!this.mouseIsOver && mouseWasOver)
6691
+ this.onLeave();
6692
+ }
6693
+ if (mouseWasPressed(0) && this.mouseIsOver)
6694
+ {
6695
+ this.mouseIsHeld = true;
6696
+ this.onPress();
6697
+ if (isTouchDevice)
6698
+ this.mouseIsOver = false;
6699
+ }
6700
+ else if (this.mouseIsHeld && !mouseDown)
6701
+ {
6702
+ this.mouseIsHeld = false;
6703
+ this.onRelease();
6704
+ }
6705
+ }
6706
+
6707
+ /** Render the object, called automatically by plugin once each frame */
6708
+ render()
6709
+ {
6710
+ if (this.size.x && this.size.y)
6711
+ uiSystem.drawRect(this.pos, this.size, this.color, this.lineWidth, this.lineColor);
6712
+ }
6713
+
6714
+ /** Called when the mouse enters the object */
6715
+ onEnter() {}
6716
+
6717
+ /** Called when the mouse leaves the object */
6718
+ onLeave() {}
6719
+
6720
+ /** Called when the mouse is pressed while over the object */
6721
+ onPress() {}
6722
+
6723
+ /** Called when the mouse is released while over the object */
6724
+ onRelease() {}
6725
+
6726
+ /** Called when the state of this object changes */
6727
+ onChange() {}
6728
+ }
6729
+
6730
+ ///////////////////////////////////////////////////////////////////////////////
6731
+ /**
6732
+ * UIText - A UI object that displays text
6733
+ * @extends UIObject
6734
+ */
6735
+ class UIText extends UIObject
6736
+ {
6737
+ /** Create a UIText object
6738
+ * @param {Vector2} [pos]
6739
+ * @param {Vector2} [size]
6740
+ * @param {string} [text]
6741
+ * @param {string} [align]
6742
+ * @param {string} [font=uiSystem.defaultFont]
6743
+ */
6744
+ constructor(pos, size, text='', align='center', font=uiSystem.defaultFont)
6745
+ {
6746
+ super(pos, size);
6747
+
6748
+ /** @property {string} */
6749
+ this.text = text;
6750
+ /** @property {string} */
6751
+ this.align = align;
6752
+
6753
+ this.font = font; // set font
6754
+ this.lineWidth = 0; // set text to not be outlined by default
6755
+ }
6756
+ render()
6757
+ {
6758
+ uiSystem.drawText(this.text, this.pos, this.size, this.textColor, this.lineWidth, this.lineColor, this.align, this.font);
6759
+ }
6760
+ }
6761
+
6762
+ ///////////////////////////////////////////////////////////////////////////////
6763
+ /**
6764
+ * UITile - A UI object that displays a tile image
6765
+ * @extends UIObject
6766
+ */
6767
+ class UITile extends UIObject
6768
+ {
6769
+ /** Create a UITile object
6770
+ * @param {Vector2} [pos]
6771
+ * @param {Vector2} [size]
6772
+ * @param {TileInfo} [tileInfo]
6773
+ * @param {Color} [color=WHITE]
6774
+ * @param {number} [angle]
6775
+ * @param {boolean} [mirror]
6776
+ */
6777
+ constructor(pos, size, tileInfo, color=WHITE, angle=0, mirror=false)
6778
+ {
6779
+ super(pos, size);
6780
+
6781
+ /** @property {TileInfo} - Tile image to use */
6782
+ this.tileInfo = tileInfo;
6783
+ /** @property {number} - Angle to rotate in radians */
6784
+ this.angle = angle;
6785
+ /** @property {boolean} - Should it be mirrored? */
6786
+ this.mirror = mirror;
6787
+ this.color = color;
6788
+ }
6789
+ render()
6790
+ {
6791
+ uiSystem.drawTile(this.pos, this.size, this.tileInfo, this.color, this.angle, this.mirror);
6792
+ }
6793
+ }
6794
+
6795
+ ///////////////////////////////////////////////////////////////////////////////
6796
+ /**
6797
+ * UIButton - A UI object that acts as a button
6798
+ * @extends UIObject
6799
+ */
6800
+ class UIButton extends UIObject
6801
+ {
6802
+ /** Create a UIButton object
6803
+ * @param {Vector2} [pos]
6804
+ * @param {Vector2} [size]
6805
+ * @param {string} [text]
6806
+ * @param {Color} [color=uiSystem.defaultButtonColor]
6807
+ */
6808
+ constructor(pos, size, text='', color=uiSystem.defaultButtonColor)
6809
+ {
6810
+ super(pos, size);
6811
+
6812
+ /** @property {string} */
6813
+ this.text = text;
6814
+ this.color = color;
6815
+ }
6816
+ render()
6817
+ {
6818
+ const lineColor = this.mouseIsHeld ? this.color : this.lineColor;
6819
+ const color = this.mouseIsOver? this.hoverColor : this.color;
6820
+ uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, lineColor);
6821
+ const textSize = vec2(this.size.x, this.size.y*.8);
6822
+ uiSystem.drawText(this.text, this.pos, textSize,
6823
+ this.textColor, 0, undefined, this.align, this.font);
6824
+ }
6825
+ }
6826
+
6827
+ ///////////////////////////////////////////////////////////////////////////////
6828
+ /**
6829
+ * UICheckbox - A UI object that acts as a checkbox
6830
+ * @extends UIObject
6831
+ */
6832
+ class UICheckbox extends UIObject
6833
+ {
6834
+ /** Create a UICheckbox object
6835
+ * @param {Vector2} [pos]
6836
+ * @param {Vector2} [size]
6837
+ * @param {boolean} [checked]
6838
+ */
6839
+ constructor(pos, size, checked=false)
6840
+ {
6841
+ super(pos, size);
6842
+
6843
+ /** @property {boolean} */
6844
+ this.checked = checked;
6845
+ }
6846
+ onPress()
6847
+ {
6848
+ this.checked = !this.checked;
6849
+ this.onChange();
6850
+ }
6851
+ render()
6852
+ {
6853
+ const color = this.mouseIsOver? this.hoverColor : this.color;
6854
+ uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, this.lineColor);
6855
+ if (this.checked)
6856
+ {
6857
+ // draw an X if checked
6858
+ uiSystem.drawLine(this.pos.add(this.size.multiply(vec2(-.5,-.5))), this.pos.add(this.size.multiply(vec2(.5,.5))), this.lineWidth, this.lineColor);
6859
+ uiSystem.drawLine(this.pos.add(this.size.multiply(vec2(-.5,.5))), this.pos.add(this.size.multiply(vec2(.5,-.5))), this.lineWidth, this.lineColor);
6860
+ }
6861
+ }
6862
+ }
6863
+
6864
+ ///////////////////////////////////////////////////////////////////////////////
6865
+ /**
6866
+ * UIScrollbar - A UI object that acts as a scrollbar
6867
+ * @extends UIObject
6868
+ */
6869
+ class UIScrollbar extends UIObject
6870
+ {
6871
+ /** Create a UIScrollbar object
6872
+ * @param {Vector2} [pos]
6873
+ * @param {Vector2} [size]
6874
+ * @param {number} [value]
6875
+ * @param {string} [text]
6876
+ * @param {Color} [color=uiSystem.defaultButtonColor]
6877
+ * @param {Color} [handleColor=WHITE]
6878
+ */
6879
+ constructor(pos, size, value=.5, text='', color=uiSystem.defaultButtonColor, handleColor=WHITE)
6880
+ {
6881
+ super(pos, size);
6882
+
6883
+ /** @property {number} */
6884
+ this.value = value;
6885
+ /** @property {string} */
6886
+ this.text = text;
6887
+ this.color = color;
6888
+ this.handleColor = handleColor;
6889
+ }
6890
+ update()
6891
+ {
6892
+ super.update();
6893
+ if (this.mouseIsHeld)
6894
+ {
6895
+ const handleSize = vec2(this.size.y);
6896
+ const handleWidth = this.size.x - handleSize.x;
6897
+ const p1 = this.pos.x - handleWidth/2;
6898
+ const p2 = this.pos.x + handleWidth/2;
6899
+ const oldValue = this.value;
6900
+ this.value = percent(mousePosScreen.x, p1, p2);
6901
+ this.value == oldValue || this.onChange();
6902
+ }
6903
+ }
6904
+ render()
6905
+ {
6906
+ const lineColor = this.mouseIsHeld ? this.color : this.lineColor;
6907
+ const color = this.mouseIsOver? this.hoverColor : this.color;
6908
+ uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, lineColor);
6909
+
6910
+ const handleSize = vec2(this.size.y);
6911
+ const handleWidth = this.size.x - handleSize.x;
6912
+ const p1 = this.pos.x - handleWidth/2;
6913
+ const p2 = this.pos.x + handleWidth/2;
6914
+ const handlePos = vec2(lerp(this.value, p1, p2), this.pos.y);
6915
+ const barColor = this.mouseIsHeld ? this.color : this.handleColor;
6916
+ uiSystem.drawRect(handlePos, handleSize, barColor, this.lineWidth, this.lineColor);
6917
+
6918
+ const textSize = vec2(this.size.x, this.size.y*.8);
6919
+ uiSystem.drawText(this.text, this.pos, textSize,
6920
+ this.textColor, 0, undefined, this.align, this.font);
6921
+ }
6922
+ }
6923
+
6924
+ /**
6925
+ * LittleJS Box2D Physics Plugin
6926
+ * - Box2dObject extends EngineObject with Box2D physics
6927
+ * - Call box2dEngineInit() to start instead of normal engineInit()
6928
+ * - You will also need to include box2d.wasm.js
6929
+ * - Uses box2d.js super fast web assembly port of Box2D
6930
+ * - More info: https://github.com/kripken/box2d.js
6931
+ * - Fully wraps everything in Box2d
6932
+ * - Functions to create polygon, circle, and edge shapes
6933
+ * - Raycasting and querying
6934
+ * - Joint creation
6935
+ * - Contact begin and end callbacks
6936
+ * - Debug physics drawing
6937
+ */
6938
+
6939
+
6940
+
6941
+ /** Global Box2d Plugin object
6942
+ * @type {Box2dPlugin} */
6943
+ let box2d;
6944
+
6945
+ /** Enable Box2D debug drawing
6946
+ * @type {boolean}
6947
+ * @default */
6948
+ let box2dDebug = false;
6949
+
6950
+ /** Enable Box2D debug drawing
6951
+ * @param {boolean} enable */
6952
+ function box2dSetDebug(enable) { box2dDebug = enable; }
6953
+
6954
+ ///////////////////////////////////////////////////////////////////////////////
6955
+ /**
6956
+ * Box2D Object - extend with your own custom physics objects
6957
+ * - A LittleJS object with Box2D physics
6958
+ * - Each object has a Box2D body which can have multiple fixtures and joints
6959
+ * - Provides interface for Box2D body and fixture functions
6960
+ * @extends EngineObject
6961
+ */
6962
+ class Box2dObject extends EngineObject
6963
+ {
6964
+ /** Create a LittleJS object with Box2d physics
6965
+ * @param {Vector2} [pos]
6966
+ * @param {Vector2} [size]
6967
+ * @param {TileInfo} [tileInfo]
6968
+ * @param {number} [angle]
6969
+ * @param {Color} [color]
6970
+ * @param {number} [bodyType]
6971
+ * @param {number} [renderOrder] */
6972
+ constructor(pos=vec2(), size, tileInfo, angle=0, color, bodyType=box2d.bodyTypeDynamic, renderOrder=0)
6973
+ {
6974
+ super(pos, size, tileInfo, angle, color, renderOrder);
6975
+
6976
+ // create physics body
6977
+ const bodyDef = new box2d.instance.b2BodyDef();
6978
+ bodyDef.set_type(bodyType);
6979
+ bodyDef.set_position(box2d.vec2dTo(pos));
6980
+ bodyDef.set_angle(-angle);
6981
+ this.body = box2d.world.CreateBody(bodyDef);
6982
+ this.body.object = this;
6983
+ this.outlineColor = BLACK;
6984
+ }
6985
+
6986
+ /** Destroy this object and it's physics body */
6987
+ destroy()
6988
+ {
6989
+ // destroy physics body, fixtures, and joints
6990
+ this.body && box2d.world.DestroyBody(this.body);
6991
+ this.body = 0;
6992
+ super.destroy();
6993
+ }
6994
+
6995
+ /** Copy box2d update sim data */
6996
+ update()
6997
+ {
6998
+ // use box2d physics update
6999
+ this.pos = box2d.vec2From(this.body.GetPosition());
7000
+ this.angle = -this.body.GetAngle();
7001
+ }
7002
+
7003
+ /** Render the object, uses box2d drawing if no tile info exists */
7004
+ render()
7005
+ {
7006
+ // use default render or draw fixtures
7007
+ if (this.tileInfo)
7008
+ super.render();
7009
+ else
7010
+ this.drawFixtures(this.color, this.outlineColor, this.lineWidth, mainContext);
7011
+ }
7012
+
7013
+ /** Render debug info */
7014
+ renderDebugInfo()
7015
+ {
7016
+ const isAsleep = !this.getIsAwake();
7017
+ const isStatic = this.getBodyType() == box2d.bodyTypeStatic;
7018
+ const color = rgb(isAsleep?1:0, isAsleep?1:0, isStatic?1:0, .5);
7019
+ this.drawFixtures(color);
7020
+ }
7021
+
7022
+ /** Draws all this object's fixtures
7023
+ * @param {Color} [color]
7024
+ * @param {Color} [outlineColor]
7025
+ * @param {number} [lineWidth]
7026
+ * @param {CanvasRenderingContext2D} [context] */
7027
+ drawFixtures(color=WHITE, outlineColor, lineWidth=.1, context)
7028
+ {
7029
+ this.getFixtureList().forEach(fixture=>
7030
+ box2d.drawFixture(fixture, this.pos, this.angle, color, outlineColor, lineWidth, context));
7031
+ }
7032
+
7033
+ ///////////////////////////////////////////////////////////////////////////////
7034
+ // physics contact callbacks
7035
+
7036
+ /** Called when a contact begins
7037
+ * @param {Box2dObject} otherObject */
7038
+ beginContact(otherObject) {}
7039
+
7040
+ /** Called when a contact ends
7041
+ * @param {Box2dObject} otherObject */
7042
+ endContact(otherObject) {}
7043
+
7044
+ ///////////////////////////////////////////////////////////////////////////////
7045
+ // physics fixtures and shapes
7046
+
7047
+ /** Add a shape fixture to the body
7048
+ * @param {Object} shape
7049
+ * @param {number} [density]
7050
+ * @param {number} [friction]
7051
+ * @param {number} [restitution]
7052
+ * @param {boolean} [isSensor] */
7053
+ addShape(shape, density=1, friction=.2, restitution=0, isSensor=false)
7054
+ {
7055
+ const fd = new box2d.instance.b2FixtureDef();
7056
+ fd.set_shape(shape);
7057
+ fd.set_density(density);
7058
+ fd.set_friction(friction);
7059
+ fd.set_restitution(restitution);
7060
+ fd.set_isSensor(isSensor);
7061
+ return this.body.CreateFixture(fd);
7062
+ }
7063
+
7064
+ /** Add a box shape to the body
7065
+ * @param {Vector2} [size]
7066
+ * @param {Vector2} [offset]
7067
+ * @param {number} [angle]
7068
+ * @param {number} [density]
7069
+ * @param {number} [friction]
7070
+ * @param {number} [restitution]
7071
+ * @param {boolean} [isSensor] */
7072
+ addBox(size=vec2(1), offset=vec2(), angle=0, density, friction, restitution, isSensor)
7073
+ {
7074
+ const shape = new box2d.instance.b2PolygonShape();
7075
+ shape.SetAsBox(size.x/2, size.y/2, box2d.vec2dTo(offset), angle);
7076
+ return this.addShape(shape, density, friction, restitution, isSensor);
7077
+ }
7078
+
7079
+ /** Add a polygon shape to the body
7080
+ * @param {Array<Vector2>} points
7081
+ * @param {number} [density]
7082
+ * @param {number} [friction]
7083
+ * @param {number} [restitution]
7084
+ * @param {boolean} [isSensor] */
7085
+ addPoly(points, density, friction, restitution, isSensor)
7086
+ {
7087
+ function box2dCreatePolygonShape(points)
7088
+ {
7089
+ function box2dCreatePointList(points)
7090
+ {
7091
+ const buffer = box2d.instance._malloc(points.length * 8);
7092
+ for (let i=0, offset=0; i<points.length; ++i)
7093
+ {
7094
+ box2d.instance.HEAPF32[buffer + offset >> 2] = points[i].x;
7095
+ offset += 4;
7096
+ box2d.instance.HEAPF32[buffer + offset >> 2] = points[i].y;
7097
+ offset += 4;
7098
+ }
7099
+ return box2d.instance.wrapPointer(buffer, box2d.instance.b2Vec2);
7100
+ }
7101
+
7102
+ ASSERT(3 <= points.length && points.length <= 8);
7103
+ const shape = new box2d.instance.b2PolygonShape();
7104
+ const box2dPoints = box2dCreatePointList(points);
7105
+ shape.Set(box2dPoints, points.length);
7106
+ return shape;
7107
+ }
7108
+
7109
+ const shape = box2dCreatePolygonShape(points);
7110
+ return this.addShape(shape, density, friction, restitution, isSensor);
7111
+ }
7112
+
7113
+ /** Add a regular polygon shape to the body
7114
+ * @param {number} [diameter]
7115
+ * @param {number} [sides]
7116
+ * @param {number} [density]
7117
+ * @param {number} [friction]
7118
+ * @param {number} [restitution]
7119
+ * @param {boolean} [isSensor] */
7120
+ addRegularPoly(diameter=1, sides=8, density, friction, restitution, isSensor)
7121
+ {
7122
+ const points = [];
7123
+ const radius = diameter/2;
7124
+ for (let i=sides; i--;)
7125
+ points.push(vec2(radius,0).rotate((i+.5)/sides*PI*2));
7126
+ return this.addPoly(points, density, friction, restitution, isSensor);
7127
+ }
7128
+
7129
+ /** Add a random polygon shape to the body
7130
+ * @param {number} [diameter]
7131
+ * @param {number} [density]
7132
+ * @param {number} [friction]
7133
+ * @param {number} [restitution]
7134
+ * @param {boolean} [isSensor] */
7135
+ addRandomPoly(diameter=1, density, friction, restitution, isSensor)
7136
+ {
7137
+ const sides = randInt(3, 9);
7138
+ const points = [];
7139
+ const radius = diameter/2;
7140
+ for (let i=sides; i--;)
7141
+ points.push(vec2(rand(radius/2,radius*1.5),0).rotate(i/sides*PI*2));
7142
+ return this.addPoly(points, density, friction, restitution, isSensor);
7143
+ }
7144
+
7145
+ /** Add a circle shape to the body
7146
+ * @param {number} [diameter]
7147
+ * @param {Vector2} [offset]
7148
+ * @param {number} [density]
7149
+ * @param {number} [friction]
7150
+ * @param {number} [restitution]
7151
+ * @param {boolean} [isSensor] */
7152
+ addCircle(diameter=1, offset=vec2(), density, friction, restitution, isSensor)
7153
+ {
7154
+ const shape = new box2d.instance.b2CircleShape();
7155
+ shape.set_m_p(box2d.vec2dTo(offset));
7156
+ shape.set_m_radius(diameter/2);
7157
+ return this.addShape(shape, density, friction, restitution, isSensor);
7158
+ }
7159
+
7160
+ /** Add an edge shape to the body
7161
+ * @param {Vector2} point1
7162
+ * @param {Vector2} point2
7163
+ * @param {number} [density]
7164
+ * @param {number} [friction]
7165
+ * @param {number} [restitution]
7166
+ * @param {boolean} [isSensor] */
7167
+ addEdge(point1, point2, density, friction, restitution, isSensor)
7168
+ {
7169
+ const shape = new box2d.instance.b2EdgeShape();
7170
+ shape.Set(box2d.vec2dTo(point1), box2d.vec2dTo(point2));
7171
+ return this.addShape(shape, density, friction, restitution, isSensor);
7172
+ }
7173
+
7174
+ /** Add an edge loop to the body, an edge loop connects the end points
7175
+ * @param {Array<Vector2>} points
7176
+ * @param {number} [density]
7177
+ * @param {number} [friction]
7178
+ * @param {number} [restitution]
7179
+ * @param {boolean} [isSensor] */
7180
+ addEdgeLoop(points, density, friction, restitution, isSensor)
7181
+ {
7182
+ const fixtures = [];
7183
+ const getPoint = i=> points[mod(i,points.length)];
7184
+ for (let i=0; i<points.length; ++i)
7185
+ {
7186
+ const shape = new box2d.instance.b2EdgeShape();
7187
+ shape.set_m_vertex0(box2d.vec2dTo(getPoint(i-1)));
7188
+ shape.set_m_vertex1(box2d.vec2dTo(getPoint(i+0)));
7189
+ shape.set_m_vertex2(box2d.vec2dTo(getPoint(i+1)));
7190
+ shape.set_m_vertex3(box2d.vec2dTo(getPoint(i+2)));
7191
+ const f = this.addShape(shape, density, friction, restitution, isSensor);
7192
+ fixtures.push(f);
7193
+ }
7194
+ return fixtures;
7195
+ }
7196
+
7197
+ /** Add an edge list to the body
7198
+ * @param {Array<Vector2>} points
7199
+ * @param {number} [density]
7200
+ * @param {number} [friction]
7201
+ * @param {number} [restitution]
7202
+ * @param {boolean} [isSensor] */
7203
+ addEdgeList(points, density, friction, restitution, isSensor)
7204
+ {
7205
+ const fixtures = [];
7206
+ for (let i=0; i<points.length-1; ++i)
7207
+ {
7208
+ const shape = new box2d.instance.b2EdgeShape();
7209
+ points[i-1] && shape.set_m_vertex0(box2d.vec2dTo(points[i-1]));
7210
+ points[i+0] && shape.set_m_vertex1(box2d.vec2dTo(points[i+0]));
7211
+ points[i+1] && shape.set_m_vertex2(box2d.vec2dTo(points[i+1]));
7212
+ points[i+2] && shape.set_m_vertex3(box2d.vec2dTo(points[i+2]));
7213
+ const f = this.addShape(shape, density, friction, restitution, isSensor);
7214
+ fixtures.push(f);
7215
+ }
7216
+ return fixtures;
7217
+ }
7218
+
7219
+ ///////////////////////////////////////////////////////////////////////////////
7220
+ // physics get functions
7221
+
7222
+ /** Gets the center of mass
7223
+ * @return {Vector2} */
7224
+ getCenterOfMass() { return box2d.vec2From(this.body.GetWorldCenter()); }
7225
+
7226
+ /** Gets the linear velocity
7227
+ * @return {Vector2} */
7228
+ getLinearVelocity() { return box2d.vec2From(this.body.GetLinearVelocity()); }
7229
+
7230
+ /** Gets the angular velocity
7231
+ * @return {Vector2} */
7232
+ getAngularVelocity() { return this.body.GetAngularVelocity(); }
7233
+
7234
+ /** Gets the mass
7235
+ * @return {number} */
7236
+ getMass() { return this.body.GetMass(); }
7237
+
7238
+ /** Gets the rotational inertia
7239
+ * @return {number} */
7240
+ getInertia() { return this.body.GetInertia(); }
7241
+
7242
+ /** Check if this object is awake
7243
+ * @return {boolean} */
7244
+ getIsAwake() { return this.body.IsAwake(); }
7245
+
7246
+ /** Gets the physics body type
7247
+ * @return {number} */
7248
+ getBodyType() { return this.body.GetType(); }
7249
+
7250
+ ///////////////////////////////////////////////////////////////////////////////
7251
+ // physics set functions
7252
+
7253
+ /** Sets the position and angle
7254
+ * @param {Vector2} pos
7255
+ * @param {number} angle */
7256
+ setTransform(pos, angle)
7257
+ {
7258
+ this.pos = pos;
7259
+ this.angle = angle;
7260
+ this.body.SetTransform(box2d.vec2dTo(pos), angle);
7261
+ }
7262
+
7263
+ /** Sets the position
7264
+ * @param {Vector2} pos */
7265
+ setPosition(pos) { this.setTransform(pos, this.body.GetAngle()); }
7266
+
7267
+ /** Sets the angle
7268
+ * @param {number} angle */
7269
+ setAngle(angle) { this.setTransform(box2d.vec2From(this.body.GetPosition()), -angle); }
7270
+
7271
+ /** Sets the linear velocity
7272
+ * @param {Vector2} velocity */
7273
+ setLinearVelocity(velocity) { this.body.SetLinearVelocity(box2d.vec2dTo(velocity)); }
7274
+
7275
+ /** Sets the angular velocity
7276
+ * @param {number} angularVelocity */
7277
+ setAngularVelocity(angularVelocity) { this.body.SetAngularVelocity(angularVelocity); }
7278
+
7279
+ /** Sets the linear damping
7280
+ * @param {number} damping */
7281
+ setLinearDamping(damping) { this.body.SetLinearDamping(damping); }
7282
+
7283
+ /** Sets the angular damping
7284
+ * @param {number} damping */
7285
+ setAngularDamping(damping) { this.body.SetAngularDamping(damping); }
7286
+
7287
+ /** Sets the gravity scale
7288
+ * @param {number} [scale] */
7289
+ setGravityScale(scale=1) { this.body.SetGravityScale(this.gravityScale = scale); }
7290
+
7291
+ /** Should this body be treated like a bullet for continuous collision detection?
7292
+ * @param {boolean} [isBullet] */
7293
+ setBullet(isBullet=true) { this.body.SetBullet(isBullet); }
7294
+
7295
+ /** Set the sleep state of the body
7296
+ * @param {boolean} [isAwake] */
7297
+ setAwake(isAwake=true) { this.body.SetAwake(isAwake); }
7298
+
7299
+ /** Set the physics body type
7300
+ * @param {number} type */
7301
+ setBodyType(type) { this.body.SetType(type); }
7302
+
7303
+ /** Set whether the body is allowed to sleep
7304
+ * @param {boolean} [isAllowed] */
7305
+ setSleepingAllowed(isAllowed=true) { this.body.SetSleepingAllowed(isAllowed); }
7306
+
7307
+ /** Set whether the body can rotate
7308
+ * @param {boolean} [isFixed] */
7309
+ setFixedRotation(isFixed=true) { this.body.SetFixedRotation(isFixed); }
7310
+
7311
+ /** Set the center of mass of the body
7312
+ * @param {Vector2} center */
7313
+ setCenterOfMass(center) { this.setMassData(center) }
7314
+
7315
+ /** Set the mass of the body
7316
+ * @param {number} mass */
7317
+ setMass(mass) { this.setMassData(undefined, mass) }
7318
+
7319
+ /** Set the moment of inertia of the body
7320
+ * @param {number} momentOfInertia */
7321
+ setMomentOfInertia(momentOfInertia) { this.setMassData(undefined, undefined, momentOfInertia) }
7322
+
7323
+ /** Reset the mass, center of mass, and moment */
7324
+ resetMassData() { this.body.ResetMassData(); }
7325
+
7326
+ /** Set the mass data of the body
7327
+ * @param {Vector2} [localCenter]
7328
+ * @param {number} [mass]
7329
+ * @param {number} [momentOfInertia] */
7330
+ setMassData(localCenter, mass, momentOfInertia)
7331
+ {
7332
+ const data = new box2d.instance.b2MassData();
7333
+ this.body.GetMassData(data);
7334
+ localCenter && data.set_center(box2d.vec2dTo(localCenter));
7335
+ mass && data.set_mass(mass);
7336
+ momentOfInertia && data.set_I(momentOfInertia);
7337
+ this.body.SetMassData(data);
7338
+ }
7339
+
7340
+ /** Set the collision filter data for this body
7341
+ * @param {number} [categoryBits]
7342
+ * @param {number} [ignoreCategoryBits]
7343
+ * @param {number} [groupIndex] */
7344
+ setFilterData(categoryBits=0, ignoreCategoryBits=0, groupIndex=0)
7345
+ {
7346
+ this.getFixtureList().forEach(fixture=>
7347
+ {
7348
+ const filter = fixture.GetFilterData();
7349
+ filter.set_categoryBits(categoryBits);
7350
+ filter.set_maskBits(0xffff & ~ignoreCategoryBits);
7351
+ filter.set_groupIndex(groupIndex);
7352
+ });
7353
+ }
7354
+
7355
+ /** Set if this body is a sensor
7356
+ * @param {boolean} [isSensor] */
7357
+ setSensor(isSensor=true)
7358
+ { this.getFixtureList().forEach(f=>f.SetSensor(isSensor)); }
7359
+
7360
+ ///////////////////////////////////////////////////////////////////////////////
7361
+ // physics force and torque functions
7362
+
7363
+ /** Apply force to this object
7364
+ * @param {Vector2} force
7365
+ * @param {Vector2} [pos] */
7366
+ applyForce(force, pos)
7367
+ {
7368
+ pos ||= this.getCenterOfMass();
7369
+ this.setAwake();
7370
+ this.body.ApplyForce(box2d.vec2dTo(force), box2d.vec2dTo(pos));
7371
+ }
7372
+
7373
+ /** Apply acceleration to this object
7374
+ * @param {Vector2} acceleration
7375
+ * @param {Vector2} [pos] */
7376
+ applyAcceleration(acceleration, pos)
7377
+ {
7378
+ pos ||= this.getCenterOfMass();
7379
+ this.setAwake();
7380
+ this.body.ApplyLinearImpulse(box2d.vec2dTo(acceleration), box2d.vec2dTo(pos));
7381
+ }
7382
+
7383
+ /** Apply torque to this object
7384
+ * @param {number} torque */
7385
+ applyTorque(torque)
7386
+ {
7387
+ this.setAwake();
7388
+ this.body.ApplyTorque(torque);
7389
+ }
7390
+
7391
+ /** Apply angular acceleration to this object
7392
+ * @param {number} acceleration */
7393
+ applyAngularAcceleration(acceleration)
7394
+ {
7395
+ this.setAwake();
7396
+ this.body.ApplyAngularImpulse(acceleration);
7397
+ }
7398
+
7399
+ ///////////////////////////////////////////////////////////////////////////////
7400
+ // lists of fixtures and joints
7401
+
7402
+ /** Check if this object has any fixtures
7403
+ * @return {boolean} */
7404
+ hasFixtures() { return !box2d.isNull(this.body.GetFixtureList()); }
7405
+
7406
+ /** Get list of fixtures for this object
7407
+ * @return {Array<Object>} */
7408
+ getFixtureList()
7409
+ {
7410
+ const fixtures = [];
7411
+ for (let fixture=this.body.GetFixtureList(); !box2d.isNull(fixture); )
7412
+ {
7413
+ fixtures.push(fixture);
7414
+ fixture = fixture.GetNext();
7415
+ }
7416
+ return fixtures;
7417
+ }
7418
+
7419
+ /** Check if this object has any joints
7420
+ * @return {boolean} */
7421
+ hasJoints() { return !box2d.isNull(this.body.GetJointList()); }
7422
+
7423
+ /** Get list of joints for this object
7424
+ * @return {Array<Object>} */
7425
+ getJointList()
7426
+ {
7427
+ const joints = [];
7428
+ for (let joint=this.body.GetJointList(); !box2d.isNull(joint); )
7429
+ {
7430
+ joints.push(joint);
7431
+ joint = joint.get_next();
7432
+ }
7433
+ return joints;
7434
+ }
7435
+ }
7436
+
7437
+ ///////////////////////////////////////////////////////////////////////////////
7438
+ /**
7439
+ * Box2D Raycast Result
7440
+ * - Holds results from a box2d raycast queries
7441
+ * - Automatically created by box2d raycast functions
7442
+ */
7443
+ class Box2dRaycastResult
7444
+ {
7445
+ /** Create a raycast result
7446
+ * @param {Object} fixture
7447
+ * @param {Vector2} point
7448
+ * @param {Vector2} normal
7449
+ * @param {number} fraction */
7450
+ constructor(fixture, point, normal, fraction)
7451
+ {
7452
+ /** @property {Box2dObject} - The box2d object */
7453
+ this.object = fixture.GetBody().object;
7454
+ /** @property {Object} - The fixture that was hit */
7455
+ this.fixture = fixture;
7456
+ /** @property {Vector2} - The hit point */
7457
+ this.point = point;
7458
+ /** @property {Vector2} - The hit normal */
7459
+ this.normal = normal;
7460
+ /** @property {number} - Distance fraction at the point of intersection */
7461
+ this.fraction = fraction;
7462
+ }
7463
+ }
7464
+
7465
+ ///////////////////////////////////////////////////////////////////////////////
7466
+ /**
7467
+ * Box2D Joint
7468
+ * - Base class for Box2D joints
7469
+ * - A joint is used to connect objects together
7470
+ */
7471
+ class Box2dJoint
7472
+ {
7473
+ /** Create a box2d joint, the base class is not intended to be used directly
7474
+ * @param {Object} jointDef */
7475
+ constructor(jointDef)
7476
+ {
7477
+ this.box2dJoint = box2d.castObjectType(box2d.world.CreateJoint(jointDef));
7478
+ }
7479
+
7480
+ /** Destroy this joint */
7481
+ destroy() { box2d.world.DestroyJoint(this.box2dJoint); this.box2dJoint = 0; }
7482
+
7483
+ /** Get the first object attached to this joint
7484
+ * @return {Box2dObject} */
7485
+ getObjectA() { return this.box2dJoint.GetBodyA().object; }
7486
+
7487
+ /** Get the second object attached to this joint
7488
+ * @return {Box2dObject} */
7489
+ getObjectB() { return this.box2dJoint.GetBodyB().object; }
7490
+
7491
+ /** Get the first anchor for this joint in world coordinates
7492
+ * @return {Vector2} */
7493
+ getAnchorA() { return box2d.vec2From(this.box2dJoint.GetAnchorA());}
7494
+
7495
+ /** Get the second anchor for this joint in world coordinates
7496
+ * @return {Vector2} */
7497
+ getAnchorB() { return box2d.vec2From(this.box2dJoint.GetAnchorB());}
7498
+
7499
+ /** Get the reaction force on bodyB at the joint anchor given a time step
7500
+ * @param {number} time
7501
+ * @return {Vector2} */
7502
+ getReactionForce(time) { return box2d.vec2From(this.box2dJoint.GetReactionForce(1/time));}
7503
+
7504
+ /** Get the reaction torque on bodyB in N*m given a time step
7505
+ * @param {number} time
7506
+ * @return {number} */
7507
+ getReactionTorque(time) { return this.box2dJoint.GetReactionTorque(1/time);}
7508
+
7509
+ /** Check if the connected bodies should collide
7510
+ * @return {boolean} */
7511
+ getCollideConnected() { return this.box2dJoint.getCollideConnected();}
7512
+
7513
+ /** Check if either connected body is active
7514
+ * @return {boolean} */
7515
+ isActive() { return this.box2dJoint.IsActive();}
7516
+ }
7517
+
7518
+ ///////////////////////////////////////////////////////////////////////////////
7519
+ /**
7520
+ * Box2D Target Joint, also known as a mouse joint
7521
+ * - Used to make a point on a object track a specific world point target
7522
+ * - This a soft constraint with a max force
7523
+ * - This allows the constraint to stretch and without applying huge forces
7524
+ * @extends Box2dJoint
7525
+ */
7526
+ class Box2dTargetJoint extends Box2dJoint
7527
+ {
7528
+ /** Create a target joint
7529
+ * @param {Box2dObject} object
7530
+ * @param {Box2dObject} fixedObject
7531
+ * @param {Vector2} worldPos */
7532
+ constructor(object, fixedObject, worldPos)
7533
+ {
7534
+ object.setAwake();
7535
+ const jointDef = new box2d.instance.b2MouseJointDef();
7536
+ jointDef.set_bodyA(fixedObject.body);
7537
+ jointDef.set_bodyB(object.body);
7538
+ jointDef.set_target(box2d.vec2dTo(worldPos));
7539
+ jointDef.set_maxForce(2e3 * object.getMass());
7540
+ super(jointDef);
7541
+ }
7542
+
7543
+ /** Set the target point in world coordinates
7544
+ * @param {Vector2} pos */
7545
+ setTarget(pos) { this.box2dJoint.SetTarget(box2d.vec2dTo(pos)); }
7546
+
7547
+ /** Get the target point in world coordinates
7548
+ * @return {Vector2} */
7549
+ getTarget(){ return box2d.vec2From(this.box2dJoint.GetTarget()); }
7550
+
7551
+ /** Sets the maximum force in Newtons
7552
+ * @param {number} force */
7553
+ setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
7554
+
7555
+ /** Gets the maximum force in Newtons
7556
+ * @return {number} */
7557
+ getMaxForce() { return this.box2dJoint.GetMaxForce(); }
7558
+
7559
+ /** Sets the joint frequency in Hertz
7560
+ * @param {number} hz */
7561
+ setFrequency(hz) { this.box2dJoint.SetFrequency(hz); }
7562
+
7563
+ /** Gets the joint frequency in Hertz
7564
+ * @return {number} */
7565
+ getFrequency() { return this.box2dJoint.GetFrequency(); }
7566
+ }
7567
+
7568
+ ///////////////////////////////////////////////////////////////////////////////
7569
+ /**
7570
+ * Box2D Distance Joint
7571
+ * - Constrains two points on two objects to remain at a fixed distance
7572
+ * - You can view this as a massless, rigid rod
7573
+ * @extends Box2dJoint
7574
+ */
7575
+ class Box2dDistanceJoint extends Box2dJoint
7576
+ {
7577
+ /** Create a distance joint
7578
+ * @param {Box2dObject} objectA
7579
+ * @param {Box2dObject} objectB
7580
+ * @param {Vector2} anchorA
7581
+ * @param {Vector2} anchorB
7582
+ * @param {boolean} [collide] */
7583
+ constructor(objectA, objectB, anchorA, anchorB, collide=false)
7584
+ {
7585
+ anchorA ||= box2d.vec2From(objectA.body.GetPosition());
7586
+ anchorB ||= box2d.vec2From(objectB.body.GetPosition());
7587
+ const localAnchorA = objectA.worldToLocal(anchorA);
7588
+ const localAnchorB = objectB.worldToLocal(anchorB);
7589
+ const jointDef = new box2d.instance.b2DistanceJointDef();
7590
+ jointDef.set_bodyA(objectA.body);
7591
+ jointDef.set_bodyB(objectB.body);
7592
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
7593
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
7594
+ jointDef.set_length(anchorA.distance(anchorB));
7595
+ jointDef.set_collideConnected(collide);
7596
+ super(jointDef);
7597
+ }
7598
+
7599
+ /** Get the local anchor point relative to objectA's origin
7600
+ * @return {Vector2} */
7601
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
7602
+
7603
+ /** Get the local anchor point relative to objectB's origin
7604
+ * @return {Vector2} */
7605
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
7606
+
7607
+ /** Set the length of the joint
7608
+ * @param {number} length */
7609
+ setLength(length) { this.box2dJoint.SetLength(length); }
7610
+
7611
+ /** Get the length of the joint
7612
+ * @return {number} */
7613
+ getLength() { return this.box2dJoint.GetLength(); }
7614
+
7615
+ /** Set the frequency in Hertz
7616
+ * @param {number} hz */
7617
+ setFrequency(hz) { this.box2dJoint.SetFrequency(hz); }
7618
+
7619
+ /** Get the frequency in Hertz
7620
+ * @return {number} */
7621
+ getFrequency() { return this.box2dJoint.GetFrequency(); }
7622
+
7623
+ /** Set the damping ratio
7624
+ * @param {number} ratio */
7625
+ setDampingRatio(ratio) { this.box2dJoint.SetDampingRatio(ratio); }
7626
+
7627
+ /** Get the damping ratio
7628
+ * @return {number} */
7629
+ getDampingRatio() { return this.box2dJoint.GetDampingRatio(); }
7630
+ }
7631
+
7632
+ ///////////////////////////////////////////////////////////////////////////////
7633
+ /**
7634
+ * Box2D Pin Joint
7635
+ * - Pins two objects together at a point
7636
+ * @extends Box2dDistanceJoint
7637
+ */
7638
+ class Box2dPinJoint extends Box2dDistanceJoint
7639
+ {
7640
+ /** Create a pin joint
7641
+ * @param {Box2dObject} objectA
7642
+ * @param {Box2dObject} objectB
7643
+ * @param {Vector2} [pos]
7644
+ * @param {boolean} [collide] */
7645
+ constructor(objectA, objectB, pos=objectA.pos, collide=false)
7646
+ {
7647
+ super(objectA, objectB, undefined, pos, collide);
7648
+ }
7649
+ }
7650
+
7651
+ ///////////////////////////////////////////////////////////////////////////////
7652
+ /**
7653
+ * Box2D Rope Joint
7654
+ * - Enforces a maximum distance between two points on two objects
7655
+ * @extends Box2dJoint
7656
+ */
7657
+ class Box2dRopeJoint extends Box2dJoint
7658
+ {
7659
+ /** Create a rope joint
7660
+ * @param {Box2dObject} objectA
7661
+ * @param {Box2dObject} objectB
7662
+ * @param {Vector2} anchorA
7663
+ * @param {Vector2} anchorB
7664
+ * @param {number} extraLength
7665
+ * @param {boolean} [collide] */
7666
+ constructor(objectA, objectB, anchorA, anchorB, extraLength=0, collide=false)
7667
+ {
7668
+ anchorA ||= box2d.vec2From(objectA.body.GetPosition());
7669
+ anchorB ||= box2d.vec2From(objectB.body.GetPosition());
7670
+ const localAnchorA = objectA.worldToLocal(anchorA);
7671
+ const localAnchorB = objectB.worldToLocal(anchorB);
7672
+ const jointDef = new box2d.instance.b2RopeJointDef();
7673
+ jointDef.set_bodyA(objectA.body);
7674
+ jointDef.set_bodyB(objectB.body);
7675
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
7676
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
7677
+ jointDef.set_maxLength(anchorA.distance(anchorB)+extraLength);
7678
+ jointDef.set_collideConnected(collide);
7679
+ super(jointDef);
7680
+ }
7681
+
7682
+ /** Get the local anchor point relative to objectA's origin
7683
+ * @return {Vector2} */
7684
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
7685
+
7686
+ /** Get the local anchor point relative to objectB's origin
7687
+ * @return {Vector2} */
7688
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
7689
+
7690
+ /** Set the max length of the joint
7691
+ * @param {number} length */
7692
+ setMaxLength(length) { this.box2dJoint.SetMaxLength(length); }
7693
+
7694
+ /** Get the max length of the joint
7695
+ * @return {number} */
7696
+ getMaxLength() { return this.box2dJoint.GetMaxLength(); }
7697
+ }
7698
+
7699
+ ///////////////////////////////////////////////////////////////////////////////
7700
+ /**
7701
+ * Box2D Revolute Joint
7702
+ * - Constrains two objects to share a point while they are free to rotate around the point
7703
+ * - The relative rotation about the shared point is the joint angle
7704
+ * - You can limit the relative rotation with a joint limit
7705
+ * - You can use a motor to drive the relative rotation about the shared point
7706
+ * - A maximum motor torque is provided so that infinite forces are not generated
7707
+ * @extends Box2dJoint
7708
+ */
7709
+ class Box2dRevoluteJoint extends Box2dJoint
7710
+ {
7711
+ /** Create a revolute joint
7712
+ * @param {Box2dObject} objectA
7713
+ * @param {Box2dObject} objectB
7714
+ * @param {Vector2} anchor
7715
+ * @param {boolean} [collide] */
7716
+ constructor(objectA, objectB, anchor, collide=false)
7717
+ {
7718
+ anchor ||= box2d.vec2From(objectB.body.GetPosition());
7719
+ const localAnchorA = objectA.worldToLocal(anchor);
7720
+ const localAnchorB = objectB.worldToLocal(anchor);
7721
+ const jointDef = new box2d.instance.b2RevoluteJointDef();
7722
+ jointDef.set_bodyA(objectA.body);
7723
+ jointDef.set_bodyB(objectB.body);
7724
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
7725
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
7726
+ jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
7727
+ jointDef.set_collideConnected(collide);
7728
+ super(jointDef);
7729
+ }
7730
+
7731
+ /** Get the local anchor point relative to objectA's origin
7732
+ * @return {Vector2} */
7733
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
7734
+
7735
+ /** Get the local anchor point relative to objectB's origin
7736
+ * @return {Vector2} */
7737
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
7738
+
7739
+ /** Get the reference angle, objectB angle minus objectA angle in the reference state
7740
+ * @return {number} */
7741
+ getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
7742
+
7743
+ /** Get the current joint angle
7744
+ * @return {number} */
7745
+ getJointAngle() { return this.box2dJoint.GetJointAngle(); }
7746
+
7747
+ /** Get the current joint angle speed in radians per second
7748
+ * @return {number} */
7749
+ getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
7750
+
7751
+ /** Is the joint limit enabled?
7752
+ * @return {boolean} */
7753
+ isLimitEnabled() { return this.box2dJoint.IsLimitEnabled(); }
7754
+
7755
+ /** Enable/disable the joint limit
7756
+ * @param {boolean} [enable] */
7757
+ enableLimit(enable=true) { return this.box2dJoint.enableLimit(enable); }
7758
+
7759
+ /** Get the lower joint limit
7760
+ * @return {number} */
7761
+ getLowerLimit() { return this.box2dJoint.GetLowerLimit(); }
7762
+
7763
+ /** Get the upper joint limit
7764
+ * @return {number} */
7765
+ getUpperLimit() { return this.box2dJoint.GetUpperLimit(); }
7766
+
7767
+ /** Set the joint limits
7768
+ * @param {number} min
7769
+ * @param {number} max */
7770
+ setLimits(min, max) { return this.box2dJoint.SetLimits(min, max); }
7771
+
7772
+ /** Is the joint motor enabled?
7773
+ * @return {boolean} */
7774
+ isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
7775
+
7776
+ /** Enable/disable the joint motor
7777
+ * @param {boolean} [enable] */
7778
+ enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
7779
+
7780
+ /** Set the motor speed
7781
+ * @param {number} speed */
7782
+ setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
7783
+
7784
+ /** Get the motor speed
7785
+ * @return {number} */
7786
+ getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
7787
+
7788
+ /** Set the motor torque
7789
+ * @param {number} torque */
7790
+ setMaxMotorTorque(torque) { return this.box2dJoint.SetMaxMotorTorque(torque); }
7791
+
7792
+ /** Get the max motor torque
7793
+ * @return {number} */
7794
+ getMaxMotorTorque() { return this.box2dJoint.GetMaxMotorTorque(); }
7795
+
7796
+ /** Get the motor torque given a time step
7797
+ * @param {number} time
7798
+ * @return {number} */
7799
+ getMotorTorque(time) { return this.box2dJoint.GetMotorTorque(1/time); }
7800
+ }
7801
+
7802
+ ///////////////////////////////////////////////////////////////////////////////
7803
+ /**
7804
+ * Box2D Gear Joint
7805
+ * - A gear joint is used to connect two joints together
7806
+ * - Either joint can be a revolute or prismatic joint
7807
+ * - You specify a gear ratio to bind the motions together
7808
+ * @extends Box2dJoint
7809
+ */
7810
+ class Box2dGearJoint extends Box2dJoint
7811
+ {
7812
+ /** Create a gear joint
7813
+ * @param {Box2dObject} objectA
7814
+ * @param {Box2dObject} objectB
7815
+ * @param {Box2dJoint} joint1
7816
+ * @param {Box2dJoint} joint2
7817
+ * @param {ratio} [ratio] */
7818
+ constructor(objectA, objectB, joint1, joint2, ratio=1)
7819
+ {
7820
+ const jointDef = new box2d.instance.b2GearJointDef();
7821
+ jointDef.set_bodyA(objectA.body);
7822
+ jointDef.set_bodyB(objectB.body);
7823
+ jointDef.set_joint1(joint1.box2dJoint);
7824
+ jointDef.set_joint2(joint2.box2dJoint);
7825
+ jointDef.set_ratio(ratio);
7826
+ super(jointDef);
7827
+
7828
+ this.joint1 = joint1;
7829
+ this.joint2 = joint2;
7830
+ }
7831
+
7832
+ /** Get the first joint
7833
+ * @return {Box2dJoint} */
7834
+ getJoint1() { return this.joint1; }
7835
+
7836
+ /** Get the second joint
7837
+ * @return {Box2dJoint} */
7838
+ getJoint2() { return this.joint2; }
7839
+
7840
+ /** Set the gear ratio
7841
+ * @param {number} ratio */
7842
+ setRatio(ratio) { return this.box2dJoint.SetRatio(ratio); }
7843
+
7844
+ /** Get the gear ratio
7845
+ * @return {number} */
7846
+ getRatio() { return this.box2dJoint.GetRatio(); }
7847
+ }
7848
+
7849
+ ///////////////////////////////////////////////////////////////////////////////
7850
+ /**
7851
+ * Box2D Prismatic Joint
7852
+ * - Provides one degree of freedom: translation along an axis fixed in objectA
7853
+ * - Relative rotation is prevented
7854
+ * - You can use a joint limit to restrict the range of motion
7855
+ * - You can use a joint motor to drive the motion or to model joint friction
7856
+ * @extends Box2dJoint
7857
+ */
7858
+ class Box2dPrismaticJoint extends Box2dJoint
7859
+ {
7860
+ /** Create a prismatic joint
7861
+ * @param {Box2dObject} objectA
7862
+ * @param {Box2dObject} objectB
7863
+ * @param {Vector2} anchor
7864
+ * @param {Vector2} worldAxis
7865
+ * @param {boolean} [collide] */
7866
+ constructor(objectA, objectB, anchor, worldAxis=vec2(0,1), collide=false)
7867
+ {
7868
+ anchor ||= box2d.vec2From(objectB.body.GetPosition());
7869
+ const localAnchorA = objectA.worldToLocal(anchor);
7870
+ const localAnchorB = objectB.worldToLocal(anchor);
7871
+ const localAxisA = objectB.worldToLocalVector(worldAxis);
7872
+ const jointDef = new box2d.instance.b2PrismaticJointDef();
7873
+ jointDef.set_bodyA(objectA.body);
7874
+ jointDef.set_bodyB(objectB.body);
7875
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
7876
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
7877
+ jointDef.set_localAxisA(box2d.vec2dTo(localAxisA));
7878
+ jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
7879
+ jointDef.set_collideConnected(collide);
7880
+ super(jointDef);
7881
+ }
7882
+
7883
+ /** Get the local anchor point relative to objectA's origin
7884
+ * @return {Vector2} */
7885
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
7886
+
7887
+ /** Get the local anchor point relative to objectB's origin
7888
+ * @return {Vector2} */
7889
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
7890
+
7891
+ /** Get the local joint axis relative to bodyA
7892
+ * @return {Vector2} */
7893
+ getLocalAxisA() { return box2d.vec2From(this.box2dJoint.GetLocalAxisA()); }
7894
+
7895
+ /** Get the reference angle
7896
+ * @return {number} */
7897
+ getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
7898
+
7899
+ /** Get the current joint translation
7900
+ * @return {number} */
7901
+ getJointTranslation() { return this.box2dJoint.GetJointTranslation(); }
7902
+
7903
+ /** Get the current joint translation speed
7904
+ * @return {number} */
7905
+ getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
7906
+
7907
+ /** Is the joint limit enabled?
7908
+ * @return {boolean} */
7909
+ isLimitEnabled() { return this.box2dJoint.IsLimitEnabled(); }
7910
+
7911
+ /** Enable/disable the joint limit
7912
+ * @param {boolean} [enable] */
7913
+ enableLimit(enable=true) { return this.box2dJoint.enableLimit(enable); }
7914
+
7915
+ /** Get the lower joint limit
7916
+ * @return {number} */
7917
+ getLowerLimit() { return this.box2dJoint.GetLowerLimit(); }
7918
+
7919
+ /** Get the upper joint limit
7920
+ * @return {number} */
7921
+ getUpperLimit() { return this.box2dJoint.GetUpperLimit(); }
7922
+
7923
+ /** Set the joint limits
7924
+ * @param {number} min
7925
+ * @param {number} max */
7926
+ setLimits(min, max) { return this.box2dJoint.SetLimits(min, max); }
7927
+
7928
+ /** Is the motor enabled?
7929
+ * @return {boolean} */
7930
+ isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
7931
+
7932
+ /** Enable/disable the joint motor
7933
+ * @param {boolean} [enable] */
7934
+ enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
7935
+
7936
+ /** Set the motor speed
7937
+ * @param {number} speed */
7938
+ setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
7939
+
7940
+ /** Get the motor speed
7941
+ * @return {number} */
7942
+ getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
7943
+
7944
+ /** Set the maximum motor force
7945
+ * @param {number} force */
7946
+ setMaxMotorForce(force) { return this.box2dJoint.SetMaxMotorForce(force); }
7947
+
7948
+ /** Get the maximum motor force
7949
+ * @return {number} */
7950
+ getMaxMotorForce() { return this.box2dJoint.GetMaxMotorForce(); }
7951
+
7952
+ /** Get the motor force given a time step
7953
+ * @param {number} time
7954
+ * @return {number} */
7955
+ getMotorForce(time) { return this.box2dJoint.GetMotorForce(1/time); }
7956
+ }
7957
+
7958
+ ///////////////////////////////////////////////////////////////////////////////
7959
+ /**
7960
+ * Box2D Wheel Joint
7961
+ * - Provides two degrees of freedom: translation along an axis fixed in objectA and rotation
7962
+ * - You can use a joint limit to restrict the range of motion
7963
+ * - You can use a joint motor to drive the motion or to model joint friction
7964
+ * - This joint is designed for vehicle suspensions
7965
+ * @extends Box2dJoint
7966
+ */
7967
+ class Box2dWheelJoint extends Box2dJoint
7968
+ {
7969
+ /** Create a wheel joint
7970
+ * @param {Box2dObject} objectA
7971
+ * @param {Box2dObject} objectB
7972
+ * @param {Vector2} anchor
7973
+ * @param {Vector2} worldAxis
7974
+ * @param {boolean} [collide] */
7975
+ constructor(objectA, objectB, anchor, worldAxis=vec2(0,1), collide=false)
7976
+ {
7977
+ anchor ||= box2d.vec2From(objectB.body.GetPosition());
7978
+ const localAnchorA = objectA.worldToLocal(anchor);
7979
+ const localAnchorB = objectB.worldToLocal(anchor);
7980
+ const localAxisA = objectB.worldToLocalVector(worldAxis);
7981
+ const jointDef = new box2d.instance.b2WheelJointDef();
7982
+ jointDef.set_bodyA(objectA.body);
7983
+ jointDef.set_bodyB(objectB.body);
7984
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
7985
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
7986
+ jointDef.set_localAxisA(box2d.vec2dTo(localAxisA));
7987
+ jointDef.set_collideConnected(collide);
7988
+ super(jointDef);
7989
+ }
7990
+
7991
+ /** Get the local anchor point relative to objectA's origin
7992
+ * @return {Vector2} */
7993
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
7994
+
7995
+ /** Get the local anchor point relative to objectB's origin
7996
+ * @return {Vector2} */
7997
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
7998
+
7999
+ /** Get the local joint axis relative to bodyA
8000
+ * @return {Vector2} */
8001
+ getLocalAxisA() { return box2d.vec2From(this.box2dJoint.GetLocalAxisA()); }
8002
+
8003
+ /** Get the current joint translation
8004
+ * @return {number} */
8005
+ getJointTranslation() { return this.box2dJoint.GetJointTranslation(); }
8006
+
8007
+ /** Get the current joint translation speed
8008
+ * @return {number} */
8009
+ getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
8010
+
8011
+ /** Is the joint motor enabled?
8012
+ * @return {boolean} */
8013
+ isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
8014
+
8015
+ /** Enable/disable the joint motor
8016
+ * @param {boolean} [enable] */
8017
+ enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
8018
+
8019
+ /** Set the motor speed
8020
+ * @param {number} speed */
8021
+ setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
8022
+
8023
+ /** Get the motor speed
8024
+ * @return {number} */
8025
+ getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
8026
+
8027
+ /** Set the maximum motor torque
8028
+ * @param {number} torque */
8029
+ setMaxMotorTorque(torque) { return this.box2dJoint.SetMaxMotorTorque(torque); }
8030
+
8031
+ /** Get the max motor torque
8032
+ * @return {number} */
8033
+ getMaxMotorTorque() { return this.box2dJoint.GetMaxMotorTorque(); }
8034
+
8035
+ /** Get the motor torque for a time step
8036
+ * @return {number} */
8037
+ getMotorTorque(time) { return this.box2dJoint.GetMotorTorque(1/time); }
8038
+
8039
+ /** Set the spring frequency in Hertz
8040
+ * @param {number} hz */
8041
+ setSpringFrequencyHz(hz) { return this.box2dJoint.SetSpringFrequencyHz(hz); }
8042
+
8043
+ /** Get the spring frequency in Hertz
8044
+ * @return {number} */
8045
+ getSpringFrequencyHz() { return this.box2dJoint.GetSpringFrequencyHz(); }
8046
+
8047
+ /** Set the spring damping ratio
8048
+ * @param {number} ratio */
8049
+ setSpringDampingRatio(ratio) { return this.box2dJoint.SetSpringDampingRatio(ratio); }
8050
+
8051
+ /** Get the spring damping ratio
8052
+ * @return {number} */
8053
+ getSpringDampingRatio() { return this.box2dJoint.GetSpringDampingRatio(); }
8054
+ }
8055
+
8056
+ ///////////////////////////////////////////////////////////////////////////////
8057
+ /**
8058
+ * Box2D Weld Joint
8059
+ * - Glues two objects together
8060
+ * @extends Box2dJoint
8061
+ */
8062
+ class Box2dWeldJoint extends Box2dJoint
8063
+ {
8064
+ /** Create a weld joint
8065
+ * @param {Box2dObject} objectA
8066
+ * @param {Box2dObject} objectB
8067
+ * @param {Vector2} anchor
8068
+ * @param {boolean} [collide] */
8069
+ constructor(objectA, objectB, anchor, collide=false)
8070
+ {
8071
+ anchor ||= box2d.vec2From(objectB.body.GetPosition());
8072
+ const localAnchorA = objectA.worldToLocal(anchor);
8073
+ const localAnchorB = objectB.worldToLocal(anchor);
8074
+ const jointDef = new box2d.instance.b2WeldJointDef();
8075
+ jointDef.set_bodyA(objectA.body);
8076
+ jointDef.set_bodyB(objectB.body);
8077
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
8078
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
8079
+ jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
8080
+ jointDef.set_collideConnected(collide);
8081
+ super(jointDef);
8082
+ }
8083
+
8084
+ /** Get the local anchor point relative to objectA's origin
8085
+ * @return {Vector2} */
8086
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
8087
+
8088
+ /** Get the local anchor point relative to objectB's origin
8089
+ * @return {Vector2} */
8090
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
8091
+
8092
+ /** Get the reference angle
8093
+ * @return {number} */
8094
+ getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
8095
+
8096
+ /** Set the frequency in Hertz
8097
+ * @param {number} hz */
8098
+ setFrequency(hz) { return this.box2dJoint.SetFrequency(hz); }
8099
+
8100
+ /** Get the frequency in Hertz
8101
+ * @return {number} */
8102
+ getFrequency() { return this.box2dJoint.GetFrequency(); }
8103
+
8104
+ /** Set the damping ratio
8105
+ * @param {number} ratio */
8106
+ setSpringDampingRatio(ratio) { return this.box2dJoint.SetSpringDampingRatio(ratio); }
8107
+
8108
+ /** Get the damping ratio
8109
+ * @return {number} */
8110
+ getSpringDampingRatio() { return this.box2dJoint.GetSpringDampingRatio(); }
8111
+ }
8112
+
8113
+ ///////////////////////////////////////////////////////////////////////////////
8114
+ /**
8115
+ * Box2D Friction Joint
8116
+ * - Used to apply top-down friction
8117
+ * - Provides 2D translational friction and angular friction
8118
+ * @extends Box2dJoint
8119
+ */
8120
+ class Box2dFrictionJoint extends Box2dJoint
8121
+ {
8122
+ /** Create a friction joint
8123
+ * @param {Box2dObject} objectA
8124
+ * @param {Box2dObject} objectB
8125
+ * @param {Vector2} anchor
8126
+ * @param {boolean} [collide] */
8127
+ constructor(objectA, objectB, anchor, collide=false)
8128
+ {
8129
+ anchor ||= box2d.vec2From(objectB.body.GetPosition());
8130
+ const localAnchorA = objectA.worldToLocal(anchor);
8131
+ const localAnchorB = objectB.worldToLocal(anchor);
8132
+ const jointDef = new box2d.instance.b2FrictionJointDef();
8133
+ jointDef.set_bodyA(objectA.body);
8134
+ jointDef.set_bodyB(objectB.body);
8135
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
8136
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
8137
+ jointDef.set_collideConnected(collide);
8138
+ super(jointDef);
8139
+ }
8140
+
8141
+ /** Get the local anchor point relative to objectA's origin
8142
+ * @return {Vector2} */
8143
+ getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
8144
+
8145
+ /** Get the local anchor point relative to objectB's origin
8146
+ * @return {Vector2} */
8147
+ getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
8148
+
8149
+ /** Set the maximum friction force
8150
+ * @param {number} force */
8151
+ setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
8152
+
8153
+ /** Get the maximum friction force
8154
+ * @return {number} */
8155
+ getMaxForce() { return this.box2dJoint.GetMaxForce(); }
8156
+
8157
+ /** Set the maximum friction torque
8158
+ * @param {number} torque */
8159
+ setMaxTorque(torque) { this.box2dJoint.SetMaxTorque(torque); }
8160
+
8161
+ /** Get the maximum friction torque
8162
+ * @return {number} */
8163
+ getMaxTorque() { return this.box2dJoint.GetMaxTorque(); }
8164
+ }
8165
+
8166
+ ///////////////////////////////////////////////////////////////////////////////
8167
+ /**
8168
+ * Box2D Pulley Joint
8169
+ * - Connects to two objects and two fixed ground points
8170
+ * - The pulley supports a ratio such that: length1 + ratio * length2 <= constant
8171
+ * - The force transmitted is scaled by the ratio
8172
+ * @extends Box2dJoint
8173
+ */
8174
+ class Box2dPulleyJoint extends Box2dJoint
8175
+ {
8176
+ /** Create a pulley joint
8177
+ * @param {Box2dObject} objectA
8178
+ * @param {Box2dObject} objectB
8179
+ * @param {Vector2} groundAnchorA
8180
+ * @param {Vector2} groundAnchorB
8181
+ * @param {Vector2} anchorA
8182
+ * @param {Vector2} anchorB
8183
+ * @param {number} [ratio]
8184
+ * @param {boolean} [collide] */
8185
+ constructor(objectA, objectB, groundAnchorA, groundAnchorB, anchorA, anchorB, ratio=1, collide=false)
8186
+ {
8187
+ anchorA ||= box2d.vec2From(objectA.body.GetPosition());
8188
+ anchorB ||= box2d.vec2From(objectB.body.GetPosition());
8189
+ const localAnchorA = objectA.worldToLocal(anchorA);
8190
+ const localAnchorB = objectB.worldToLocal(anchorB);
8191
+ const jointDef = new box2d.instance.b2PulleyJointDef();
8192
+ jointDef.set_bodyA(objectA.body);
8193
+ jointDef.set_bodyB(objectB.body);
8194
+ jointDef.set_groundAnchorA(box2d.vec2dTo(groundAnchorA));
8195
+ jointDef.set_groundAnchorB(box2d.vec2dTo(groundAnchorB));
8196
+ jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
8197
+ jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
8198
+ jointDef.set_ratio(ratio);
8199
+ jointDef.set_lengthA(groundAnchorA.distance(anchorA));
8200
+ jointDef.set_lengthB(groundAnchorB.distance(anchorB));
8201
+ jointDef.set_collideConnected(collide);
8202
+ super(jointDef);
8203
+ }
8204
+
8205
+ /** Get the first ground anchor
8206
+ * @return {Vector2} */
8207
+ getGroundAnchorA() { return box2d.vec2From(this.box2dJoint.GetGroundAnchorA()); }
8208
+
8209
+ /** Get the second ground anchor
8210
+ * @return {Vector2} */
8211
+ getGroundAnchorB() { return box2d.vec2From(this.box2dJoint.GetGroundAnchorB()); }
8212
+
8213
+ /** Get the current length of the segment attached to objectA
8214
+ * @return {number} */
8215
+ getLengthA() { return this.box2dJoint.GetLengthA(); }
8216
+
8217
+ /** Get the current length of the segment attached to objectB
8218
+ * @return {number} */
8219
+ getLengthB(){ return this.box2dJoint.GetLengthB(); }
8220
+
8221
+ /** Get the pulley ratio
8222
+ * @return {number} */
8223
+ getRatio() { return this.box2dJoint.GetRatio(); }
8224
+
8225
+ /** Get the current length of the segment attached to objectA
8226
+ * @return {number} */
8227
+ getCurrentLengthA() { return this.box2dJoint.GetCurrentLengthA(); }
8228
+
8229
+ /** Get the current length of the segment attached to objectB
8230
+ * @return {number} */
8231
+ getCurrentLengthB() { return this.box2dJoint.GetCurrentLengthB(); }
8232
+ }
8233
+
8234
+ ///////////////////////////////////////////////////////////////////////////////
8235
+ /**
8236
+ * Box2D Motor Joint
8237
+ * - Controls the relative motion between two objects
8238
+ * - Typical usage is to control the movement of a object with respect to the ground
8239
+ * @extends Box2dJoint
8240
+ */
8241
+ class Box2dMotorJoint extends Box2dJoint
8242
+ {
8243
+ /** Create a motor joint
8244
+ * @param {Box2dObject} objectA
8245
+ * @param {Box2dObject} objectB */
8246
+ constructor(objectA, objectB)
8247
+ {
8248
+ const linearOffset = objectA.worldToLocal(box2d.vec2From(objectB.body.GetPosition()));
8249
+ const angularOffset = objectB.body.GetAngle() - objectA.body.GetAngle();
8250
+ const jointDef = new box2d.instance.b2MotorJointDef();
8251
+ jointDef.set_bodyA(objectA.body);
8252
+ jointDef.set_bodyB(objectB.body);
8253
+ jointDef.set_linearOffset(box2d.vec2dTo(linearOffset));
8254
+ jointDef.set_angularOffset(angularOffset);
8255
+ super(jointDef);
8256
+ }
8257
+
8258
+ /** Set the target linear offset, in frame A, in meters.
8259
+ * @param {Vector2} offset */
8260
+ setLinearOffset(offset) { this.box2dJoint.SetLinearOffset(box2d.vec2dTo(offset)); }
8261
+
8262
+ /** Get the target linear offset, in frame A, in meters.
8263
+ * @return {Vector2} */
8264
+ getLinearOffset() { return box2d.vec2From(this.box2dJoint.GetLinearOffset()); }
8265
+
8266
+ /** Set the target angular offset
8267
+ * @param {number} offset */
8268
+ setAngularOffset(offset) { this.box2dJoint.SetAngularOffset(offset); }
8269
+
8270
+ /** Get the target angular offset
8271
+ * @return {number} */
8272
+ getAngularOffset() { return this.box2dJoint.GetAngularOffset(); }
8273
+
8274
+ /** Set the maximum friction force
8275
+ * @param {number} force */
8276
+ setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
8277
+
8278
+ /** Get the maximum friction force
8279
+ * @return {number} */
8280
+ getMaxForce() { return this.box2dJoint.GetMaxForce(); }
8281
+
8282
+ /** Set the maximum torque
8283
+ * @param {number} torque */
8284
+ setMaxTorque(torque) { this.box2dJoint.SetMaxTorque(torque); }
8285
+
8286
+ /** Get the maximum torque
8287
+ * @return {number} */
8288
+ getMaxTorque() { return this.box2dJoint.GetMaxTorque(); }
8289
+
8290
+ /** Set the position correction factor in the range [0,1]
8291
+ * @param {number} factor */
8292
+ setCorrectionFactor(factor) { this.box2dJoint.SetCorrectionFactor(factor); }
8293
+
8294
+ /** Get the position correction factor in the range [0,1]
8295
+ * @return {number} */
8296
+ getCorrectionFactor() { return this.box2dJoint.GetCorrectionFactor(); }
8297
+ }
8298
+
8299
+ ///////////////////////////////////////////////////////////////////////////////
8300
+ /**
8301
+ * Box2D Global Object
8302
+ * - Wraps Box2d world and provides global functions
8303
+ */
8304
+ class Box2dPlugin
8305
+ {
8306
+ /** Create the global UI system object
8307
+ * @param {Object} instance */
8308
+ constructor(instance)
8309
+ {
8310
+ ASSERT(!box2d, 'Box2D already initialized');
8311
+ box2d = this;
8312
+ this.instance = instance;
8313
+ this.world = new box2d.instance.b2World();
8314
+
8315
+ /** @property {number} - Velocity iterations per update*/
8316
+ this.velocityIterations = 8;
8317
+ /** @property {number} - Position iterations per update*/
8318
+ this.positionIterations = 3;
8319
+ /** @property {number} - Static, zero mass, zero velocity, may be manually moved */
8320
+ this.bodyTypeStatic = instance.b2_staticBody;
8321
+ /** @property {number} - Kinematic, zero mass, non-zero velocity set by user, moved by solver */
8322
+ this.bodyTypeKinematic = instance.b2_kinematicBody;
8323
+ /** @property {number} - Dynamic, positive mass, non-zero velocity determined by forces, moved by solver */
8324
+ this.bodyTypeDynamic = instance.b2_dynamicBody;
8325
+
8326
+ // setup contact listener
8327
+ const listener = new box2d.instance.JSContactListener();
8328
+ listener.BeginContact = function(contactPtr)
8329
+ {
8330
+ const contact = box2d.instance.wrapPointer(contactPtr, box2d.instance.b2Contact);
8331
+ const fixtureA = contact.GetFixtureA();
8332
+ const fixtureB = contact.GetFixtureB();
8333
+ const objectA = fixtureA.GetBody().object;
8334
+ const objectB = fixtureB.GetBody().object;
8335
+ objectA.beginContact(objectB);
8336
+ objectB.beginContact(objectA);
8337
+ }
8338
+ listener.EndContact = function(contactPtr)
8339
+ {
8340
+ const contact = box2d.instance.wrapPointer(contactPtr, box2d.instance.b2Contact);
8341
+ const fixtureA = contact.GetFixtureA();
8342
+ const fixtureB = contact.GetFixtureB();
8343
+ const objectA = fixtureA.GetBody().object;
8344
+ const objectB = fixtureB.GetBody().object;
8345
+ objectA.endContact(objectB);
8346
+ objectB.endContact(objectA);
8347
+ };
8348
+ listener.PreSolve = function() {};
8349
+ listener.PostSolve = function() {};
8350
+ box2d.world.SetContactListener(listener);
8351
+ }
8352
+
8353
+ /** Step the physics world simulation
8354
+ * @param {number} [frames] */
8355
+ step(frames=1)
8356
+ {
8357
+ box2d.world.SetGravity(box2d.vec2dTo(gravity));
8358
+ for (let i=frames; i--;)
8359
+ box2d.world.Step(timeDelta, this.velocityIterations, this.positionIterations);
8360
+ }
8361
+
8362
+ ///////////////////////////////////////////////////////////////////////////////
8363
+ // raycasting and querying
8364
+
8365
+ /** raycast and return a list of all the results
8366
+ * @param {Vector2} start
8367
+ * @param {Vector2} end */
8368
+ raycastAll(start, end)
8369
+ {
8370
+ const raycastCallback = new box2d.instance.JSRayCastCallback();
8371
+ raycastCallback.ReportFixture = function(fixturePointer, point, normal, fraction)
8372
+ {
8373
+ const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
8374
+ point = box2d.vec2FromPointer(point);
8375
+ normal = box2d.vec2FromPointer(normal);
8376
+ raycastResults.push(new Box2dRaycastResult(fixture, point, normal, fraction));
8377
+ return 1; // continue getting results
8378
+ };
8379
+
8380
+ const raycastResults = [];
8381
+ box2d.world.RayCast(raycastCallback, box2d.vec2dTo(start), box2d.vec2dTo(end));
8382
+ debugRaycast && debugLine(start, end, raycastResults.length ? '#f00' : '#00f', .02);
8383
+ return raycastResults;
8384
+ }
8385
+
8386
+ /** raycast and return the first result
8387
+ * @param {Vector2} start
8388
+ * @param {Vector2} end */
8389
+ raycast(start, end)
8390
+ {
8391
+ const raycastResults = box2d.raycastAll(start, end);
8392
+ if (!raycastResults.length)
8393
+ return undefined;
8394
+ return raycastResults.reduce((a,b)=>a.fraction < b.fraction ? a : b);
8395
+ }
8396
+
8397
+ /** box aabb cast and return all the objects
8398
+ * @param {Vector2} pos
8399
+ * @param {Vector2} size */
8400
+ boxCastAll(pos, size)
8401
+ {
8402
+ const queryCallback = new box2d.instance.JSQueryCallback();
8403
+ queryCallback.ReportFixture = function(fixturePointer)
8404
+ {
8405
+ const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
8406
+ const o = fixture.GetBody().object;
8407
+ if (!queryObjects.includes(o))
8408
+ queryObjects.push(o); // add if not already in list
8409
+ return true; // continue getting results
8410
+ };
8411
+
8412
+ const aabb = new box2d.instance.b2AABB();
8413
+ aabb.set_lowerBound(box2d.vec2dTo(pos.subtract(size.scale(.5))));
8414
+ aabb.set_upperBound(box2d.vec2dTo(pos.add(size.scale(.5))));
8415
+
8416
+ let queryObjects = [];
8417
+ box2d.world.QueryAABB(queryCallback, aabb);
8418
+ debugRaycast && debugRect(pos, size, queryObjects.length ? '#f00' : '#00f', .02);
8419
+ return queryObjects;
8420
+ }
8421
+
8422
+ /** box aabb cast and return the first object
8423
+ * @param {Vector2} pos
8424
+ * @param {Vector2} size */
8425
+ boxCast(pos, size)
8426
+ {
8427
+ const queryCallback = new box2d.instance.JSQueryCallback();
8428
+ queryCallback.ReportFixture = function(fixturePointer)
8429
+ {
8430
+ const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
8431
+ queryObject = fixture.GetBody().object;
8432
+ return false; // stop getting results
8433
+ };
8434
+
8435
+ const aabb = new box2d.instance.b2AABB();
8436
+ aabb.set_lowerBound(box2d.vec2dTo(pos.subtract(size.scale(.5))));
8437
+ aabb.set_upperBound(box2d.vec2dTo(pos.add(size.scale(.5))));
8438
+
8439
+ let queryObject;
8440
+ box2d.world.QueryAABB(queryCallback, aabb);
8441
+ debugRaycast && debugRect(pos, size, queryObject ? '#f00' : '#00f', .02);
8442
+ return queryObject;
8443
+ }
8444
+
8445
+ /** circle cast and return all the objects
8446
+ * @param {Vector2} pos
8447
+ * @param {number} diameter */
8448
+ circleCastAll(pos, diameter)
8449
+ {
8450
+ const radius2 = (diameter/2)**2;
8451
+ const results = box2d.boxCastAll(pos, vec2(diameter));
8452
+ return results.filter(o=>o.pos.distanceSquared(pos) < radius2);
8453
+ }
8454
+
8455
+ /** circle cast and return the first object
8456
+ * @param {Vector2} pos
8457
+ * @param {number} diameter */
8458
+ circleCast(pos, diameter)
8459
+ {
8460
+ const radius2 = (diameter/2)**2;
8461
+ let results = box2d.boxCastAll(pos, vec2(diameter));
8462
+
8463
+ let bestResult, bestDistance2;
8464
+ for (const result of results)
8465
+ {
8466
+ const distance2 = result.pos.distanceSquared(pos);
8467
+ if (distance2 < radius2 && (!bestResult || distance2 < bestDistance2))
8468
+ {
8469
+ bestResult = result;
8470
+ bestDistance2 = distance2;
8471
+ }
8472
+ }
8473
+ return bestResult;
8474
+ }
8475
+
8476
+ /** point cast and return the first object
8477
+ * @param {Vector2} pos
8478
+ * @param {boolean} dynamicOnly */
8479
+ pointCast(pos, dynamicOnly=true)
8480
+ {
8481
+ const queryCallback = new box2d.instance.JSQueryCallback();
8482
+ queryCallback.ReportFixture = function(fixturePointer)
8483
+ {
8484
+ const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
8485
+ if (dynamicOnly && fixture.GetBody().GetType() != box2d.instance.b2_dynamicBody)
8486
+ return true; // continue getting results
8487
+ if (!fixture.TestPoint(box2d.vec2dTo(pos)))
8488
+ return true; // continue getting results
8489
+ queryObject = fixture.GetBody().object;
8490
+ return false; // stop getting results
8491
+ };
8492
+
8493
+ const aabb = new box2d.instance.b2AABB();
8494
+ aabb.set_lowerBound(box2d.vec2dTo(pos));
8495
+ aabb.set_upperBound(box2d.vec2dTo(pos));
8496
+
8497
+ let queryObject;
8498
+ box2d.world.QueryAABB(queryCallback, aabb);
8499
+ debugRaycast && debugRect(pos, vec2(), queryObject ? '#f00' : '#00f', .02);
8500
+ return queryObject;
8501
+ }
8502
+
8503
+ ///////////////////////////////////////////////////////////////////////////////
8504
+ // drawing
8505
+
8506
+ /** draws a fixture
8507
+ * @param {Object} fixture
8508
+ * @param {Vector2} pos
8509
+ * @param {number} angle
8510
+ * @param {Color} [color]
8511
+ * @param {Color} [outlineColor]
8512
+ * @param {number} [lineWidth]
8513
+ * @param {CanvasRenderingContext2D} [context] */
8514
+ drawFixture(fixture, pos, angle, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
8515
+ {
8516
+ const shape = box2d.castObjectType(fixture.GetShape());
8517
+ switch (shape.GetType())
8518
+ {
8519
+ case box2d.instance.b2Shape.e_polygon:
8520
+ {
8521
+ let points = [];
8522
+ for (let i=shape.GetVertexCount(); i--;)
8523
+ points.push(box2d.vec2From(shape.GetVertex(i)));
8524
+ box2d.drawPoly(pos, angle, points, color, outlineColor, lineWidth, context);
8525
+ break;
8526
+ }
8527
+ case box2d.instance.b2Shape.e_circle:
8528
+ {
8529
+ const radius = shape.get_m_radius();
8530
+ box2d.drawCircle(pos, radius, color, outlineColor, lineWidth, context);
8531
+ break;
8532
+ }
8533
+ case box2d.instance.b2Shape.e_edge:
8534
+ {
8535
+ const v1 = box2d.vec2From(shape.get_m_vertex1());
8536
+ const v2 = box2d.vec2From(shape.get_m_vertex2());
8537
+ box2d.drawLine(pos, angle, v1, v2, color, lineWidth, context);
8538
+ break;
8539
+ }
8540
+ }
8541
+ }
8542
+
8543
+ /** draws a circle
8544
+ * @param {Vector2} pos
8545
+ * @param {number} radius
8546
+ * @param {Color} [color]
8547
+ * @param {Color} [outlineColor]
8548
+ * @param {number} [lineWidth]
8549
+ * @param {CanvasRenderingContext2D} [context] */
8550
+ drawCircle(pos, radius, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
8551
+ {
8552
+ drawCanvas2D(pos, vec2(1), 0, 0, context=>
8553
+ {
8554
+ context.beginPath();
8555
+ context.arc(0, 0, radius, 0, 9);
8556
+ box2d.drawFillStroke(color, outlineColor, lineWidth, context);
8557
+ }, 0, context);
8558
+ }
8559
+
8560
+ /** draws a polygon
8561
+ * @param {Vector2} pos
8562
+ * @param {number} angle
8563
+ * @param {Array<Vector2>} points
8564
+ * @param {Color} [color]
8565
+ * @param {Color} [outlineColor]
8566
+ * @param {number} [lineWidth]
8567
+ * @param {CanvasRenderingContext2D} [context] */
8568
+ drawPoly(pos, angle, points, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
8569
+ {
8570
+ drawCanvas2D(pos, vec2(1), angle, 0, context=>
8571
+ {
8572
+ context.beginPath();
8573
+ points.forEach(p=>context.lineTo(p.x, p.y));
8574
+ context.closePath();
8575
+ box2d.drawFillStroke(color, outlineColor, lineWidth, context);
8576
+ }, 0, context);
8577
+ }
8578
+
8579
+ /** draws a line
8580
+ * @param {Vector2} pos
8581
+ * @param {number} angle
8582
+ * @param {Vector2} posA
8583
+ * @param {Vector2} posB
8584
+ * @param {Color} [color]
8585
+ * @param {number} [lineWidth]
8586
+ * @param {CanvasRenderingContext2D} [context] */
8587
+ drawLine(pos, angle, posA, posB, color=WHITE, lineWidth=.1, context=mainContext)
8588
+ {
8589
+ drawCanvas2D(pos, vec2(1), angle, 0, context=>
8590
+ {
8591
+ context.beginPath();
8592
+ context.lineTo(posA.x, posA.y);
8593
+ context.lineTo(posB.x, posB.y);
8594
+ box2d.drawFillStroke(0, color, lineWidth, context);
8595
+ }, 0, context);
8596
+ }
8597
+
8598
+ /** performs a fill or stroke as a helper to the other draw functions
8599
+ * @param {Color} [color]
8600
+ * @param {Color} [outlineColor]
8601
+ * @param {number} [lineWidth]
8602
+ * @param {CanvasRenderingContext2D} [context] */
8603
+ drawFillStroke(color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
8604
+ {
8605
+ if (color)
8606
+ {
8607
+ context.fillStyle = color.toString();
8608
+ context.fill();
8609
+ }
8610
+ if (outlineColor && lineWidth)
8611
+ {
8612
+ context.lineWidth = lineWidth;
8613
+ context.lineJoin = context.lineCap = 'round';
8614
+ context.strokeStyle = outlineColor.toString();
8615
+ context.stroke();
8616
+ }
8617
+ }
8618
+
8619
+ ///////////////////////////////////////////////////////////////////////////////
8620
+ // helper functions
8621
+
8622
+ /** converts a box2d vec2 to a Vector2
8623
+ * @param {Object} v */
8624
+ vec2From(v)
8625
+ {
8626
+ ASSERT(v instanceof box2d.instance.b2Vec2);
8627
+ return new Vector2(v.get_x(), v.get_y());
8628
+ }
8629
+
8630
+ /** converts a box2d vec2 pointer to a Vector2
8631
+ * @param {Object} v */
8632
+ vec2FromPointer(v)
8633
+ {
8634
+ return box2d.vec2From(box2d.instance.wrapPointer(v, box2d.instance.b2Vec2));
8635
+ }
8636
+
8637
+ /** converts a Vector2 to a box2 vec2
8638
+ * @param {Vector2} v */
8639
+ vec2dTo(v)
8640
+ {
8641
+ ASSERT(v instanceof Vector2);
8642
+ return new box2d.instance.b2Vec2(v.x, v.y);
8643
+ }
8644
+
8645
+ /** checks if a box2d object is null
8646
+ * @param {Object} o */
8647
+ isNull(o) { return !box2d.instance.getPointer(o); }
8648
+
8649
+ /** casts a box2d object to its correct type
8650
+ * @param {Object} o */
8651
+ castObjectType(o)
8652
+ {
8653
+ switch (o.GetType())
8654
+ {
8655
+ case box2d.instance.b2Shape.e_circle:
8656
+ return box2d.instance.castObject(o, box2d.instance.b2CircleShape);
8657
+ case box2d.instance.b2Shape.e_edge:
8658
+ return box2d.instance.castObject(o, box2d.instance.b2EdgeShape);
8659
+ case box2d.instance.b2Shape.e_polygon:
8660
+ return box2d.instance.castObject(o, box2d.instance.b2PolygonShape);
8661
+ case box2d.instance.b2Shape.e_chain:
8662
+ return box2d.instance.castObject(o, box2d.instance.b2ChainShape);
8663
+ case box2d.instance.e_revoluteJoint:
8664
+ return box2d.instance.castObject(o, box2d.instance.b2RevoluteJoint);
8665
+ case box2d.instance.e_prismaticJoint:
8666
+ return box2d.instance.castObject(o, box2d.instance.b2PrismaticJoint);
8667
+ case box2d.instance.e_distanceJoint:
8668
+ return box2d.instance.castObject(o, box2d.instance.b2DistanceJoint);
8669
+ case box2d.instance.e_pulleyJoint:
8670
+ return box2d.instance.castObject(o, box2d.instance.b2PulleyJoint);
8671
+ case box2d.instance.e_mouseJoint:
8672
+ return box2d.instance.castObject(o, box2d.instance.b2MouseJoint);
8673
+ case box2d.instance.e_gearJoint:
8674
+ return box2d.instance.castObject(o, box2d.instance.b2GearJoint);
8675
+ case box2d.instance.e_wheelJoint:
8676
+ return box2d.instance.castObject(o, box2d.instance.b2WheelJoint);
8677
+ case box2d.instance.e_weldJoint:
8678
+ return box2d.instance.castObject(o, box2d.instance.b2WeldJoint);
8679
+ case box2d.instance.e_frictionJoint:
8680
+ return box2d.instance.castObject(o, box2d.instance.b2FrictionJoint);
8681
+ case box2d.instance.e_ropeJoint:
8682
+ return box2d.instance.castObject(o, box2d.instance.b2RopeJoint);
8683
+ case box2d.instance.e_motorJoint:
8684
+ return box2d.instance.castObject(o, box2d.instance.b2MotorJoint);
8685
+ }
8686
+
8687
+ ASSERT(false, 'Unknown box2d object type');
8688
+ }
8689
+ }
8690
+
8691
+ ///////////////////////////////////////////////////////////////////////////////
8692
+ /** Box2d Init - Startup LittleJS engine with your callback functions
8693
+ * @param {Function|function():Promise} gameInit - Called once after the engine starts up
8694
+ * @param {Function} gameUpdate - Called every frame before objects are updated
8695
+ * @param {Function} gameUpdatePost - Called after physics and objects are updated, even when paused
8696
+ * @param {Function} gameRender - Called before objects are rendered, for drawing the background
8697
+ * @param {Function} gameRenderPost - Called after objects are rendered, useful for drawing UI
8698
+ * @param {Array<string>} [imageSources=[]] - List of images to load
8699
+ * @param {HTMLElement} [rootElement] - Root element to attach to, the document body by default */
8700
+ function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement)
8701
+ {
8702
+ Box2D().then(box2dInstance=>
8703
+ {
8704
+ // create box2d object
8705
+ new Box2dPlugin(box2dInstance);
8706
+ setupDebugDraw();
8707
+
8708
+ // start littlejs
8709
+ engineAddPlugin(box2dUpdate, box2dRender);
8710
+ engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement);
8711
+ });
8712
+
8713
+ // hook up box2d plugin to update and render
8714
+ function box2dUpdate()
8715
+ {
8716
+ if (!paused)
8717
+ box2d.step();
8718
+ }
8719
+ function box2dRender()
8720
+ {
8721
+ if (box2dDebug || debugPhysics && debugOverlay)
8722
+ box2d.world.DrawDebugData();
8723
+ }
8724
+
8725
+ // box2d debug drawing
8726
+ function setupDebugDraw()
8727
+ {
8728
+ // setup debug draw
8729
+ const debugDraw = new box2d.instance.JSDraw();
8730
+ const box2dColor = (c)=> new Color(c.get_r(), c.get_g(), c.get_b());
8731
+ const box2dColorPointer = (c)=>
8732
+ box2dColor(box2d.instance.wrapPointer(c, box2d.instance.b2Color));
8733
+ const getDebugColor = (color)=>box2dColorPointer(color).scale(1,.8);
8734
+ const getPointsList = (vertices, vertexCount) =>
8735
+ {
8736
+ const points = [];
8737
+ for (let i=vertexCount; i--;)
8738
+ points.push(box2d.vec2FromPointer(vertices+i*8));
8739
+ return points;
8740
+ }
8741
+ debugDraw.DrawSegment = function(point1, point2, color)
8742
+ {
8743
+ color = getDebugColor(color);
8744
+ point1 = box2d.vec2FromPointer(point1);
8745
+ point2 = box2d.vec2FromPointer(point2);
8746
+ box2d.drawLine(vec2(), 0, point1, point2, color, undefined, overlayContext);
8747
+ };
8748
+ debugDraw.DrawPolygon = function(vertices, vertexCount, color)
8749
+ {
8750
+ color = getDebugColor(color);
8751
+ const points = getPointsList(vertices, vertexCount);
8752
+ box2d.drawPoly(vec2(), 0, points, undefined, color, undefined, overlayContext);
8753
+ };
8754
+ debugDraw.DrawSolidPolygon = function(vertices, vertexCount, color)
8755
+ {
8756
+ color = getDebugColor(color);
8757
+ const points = getPointsList(vertices, vertexCount);
8758
+ box2d.drawPoly(vec2(), 0, points, color, color, undefined, overlayContext);
8759
+ };
8760
+ debugDraw.DrawCircle = function(center, radius, color)
8761
+ {
8762
+ color = getDebugColor(color);
8763
+ center = box2d.vec2FromPointer(center);
8764
+ box2d.drawCircle(center, radius, undefined, color, undefined, overlayContext);
8765
+ };
8766
+ debugDraw.DrawSolidCircle = function(center, radius, axis, color)
8767
+ {
8768
+ color = getDebugColor(color);
8769
+ center = box2d.vec2FromPointer(center);
8770
+ axis = box2d.vec2FromPointer(axis).scale(radius);
8771
+ box2d.drawCircle(center, radius, color, color, undefined, overlayContext);
8772
+ box2d.drawLine(center, 0, vec2(), axis, color, undefined, overlayContext);
8773
+ };
8774
+ debugDraw.DrawTransform = function(transform)
8775
+ {
8776
+ transform = box2d.instance.wrapPointer(transform, box2d.instance.b2Transform);
8777
+ const pos = vec2(transform.get_p());
8778
+ const angle = -transform.get_q().GetAngle();
8779
+ const p1 = vec2(1,0), c1 = rgb(.75,0,0,.8);
8780
+ const p2 = vec2(0,1), c2 = rgb(0,.75,0,.8);
8781
+ box2d.drawLine(pos, angle, vec2(), p1, c1, undefined, overlayContext);
8782
+ box2d.drawLine(pos, angle, vec2(), p2, c2, undefined, overlayContext);
8783
+ }
8784
+
8785
+ debugDraw.AppendFlags(box2d.instance.b2Draw.e_shapeBit);
8786
+ debugDraw.AppendFlags(box2d.instance.b2Draw.e_jointBit);
8787
+ //debugDraw.AppendFlags(box2d.instance.b2Draw.e_aabbBit);
8788
+ //debugDraw.AppendFlags(box2d.instance.b2Draw.e_pairBit);
8789
+ //debugDraw.AppendFlags(box2d.instance.b2Draw.e_centerOfMassBit);
8790
+ box2d.world.SetDebugDraw(debugDraw);
8791
+ }
8792
+ }
8793
+