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.
- package/dist/littlejs.d.ts +112 -43
- package/dist/littlejs.esm.js +586 -500
- package/dist/littlejs.esm.min.js +1 -1
- package/dist/littlejs.js +2993 -122
- package/dist/littlejs.min.js +1 -1
- package/dist/littlejs.release.js +2992 -119
- package/examples/box2d/game.js +70 -39
- package/examples/box2d/gameObjects.js +81 -77
- package/examples/box2d/index.html +2 -7
- package/examples/box2d/scenes.js +80 -90
- package/examples/breakout/game.js +63 -28
- package/examples/breakout/gameObjects.js +45 -47
- package/examples/breakout/index.html +1 -5
- package/examples/breakoutTutorial/game.js +36 -29
- package/examples/breakoutTutorial/index.html +1 -3
- package/examples/empty/game.js +5 -2
- package/examples/empty/index.html +1 -3
- package/examples/htmlMenu/game.js +24 -15
- package/examples/htmlMenu/index.html +5 -7
- package/examples/index.html +2 -3
- package/examples/module/game.js +32 -34
- package/examples/module/index.html +1 -1
- package/examples/particles/index.html +27 -22
- package/examples/platformer/game.js +71 -45
- package/examples/platformer/gameCharacter.js +42 -34
- package/examples/platformer/gameEffects.js +82 -75
- package/examples/platformer/gameLevel.js +67 -38
- package/examples/platformer/{gameLevelData.js → gameLevelData.json} +1 -11
- package/examples/platformer/gameObjects.js +70 -64
- package/examples/platformer/gamePlayer.js +12 -7
- package/examples/platformer/index.html +1 -9
- package/examples/puzzle/game.js +59 -40
- package/examples/puzzle/index.html +1 -3
- package/examples/shorts/base.html +2 -2
- package/examples/shorts/particles.js +1 -1
- package/examples/shorts/platformer.js +1 -1
- package/examples/shorts/tileLayer.js +5 -6
- package/examples/shorts/tiles.png +0 -0
- package/examples/starter/game.js +9 -12
- package/examples/starter/index.html +13 -13
- package/examples/stress/index.html +44 -44
- package/examples/typescript/build.js +17 -18
- package/examples/typescript/game.js +32 -34
- package/examples/typescript/game.ts +32 -34
- package/examples/typescript/index.html +1 -1
- package/examples/uiSystem/game.js +34 -31
- package/examples/uiSystem/index.html +1 -5
- package/package.json +1 -1
- package/plugins/box2d.js +6 -2
- package/plugins/pluginExport.js +1 -2
- package/reference.md +29 -27
- package/src/engine.js +1 -1
- package/src/engineBuild.js +13 -6
- package/src/engineDebug.js +1 -3
- package/src/engineExport.js +2 -6
- package/src/engineInput.js +3 -1
- package/src/engineObject.js +18 -14
- package/src/engineSettings.js +6 -6
- package/src/engineTileLayer.js +179 -96
- package/examples/typescript/build/dist/littlejs.esm.js +0 -4834
- package/examples/typescript/build/examples/typescript/build.js +0 -24
- package/examples/typescript/build/examples/typescript/game.js +0 -102
package/dist/littlejs.release.js
CHANGED
|
@@ -1002,7 +1002,7 @@ let canvasMaxSize = vec2(1920, 1080);
|
|
|
1002
1002
|
* @memberof Settings */
|
|
1003
1003
|
let canvasFixedSize = vec2();
|
|
1004
1004
|
|
|
1005
|
-
/** Use nearest neighbor scaling
|
|
1005
|
+
/** Use nearest neighbor canvas scaling for more pixelated look
|
|
1006
1006
|
* - Must be set before startup to take effect
|
|
1007
1007
|
* - If enabled sets css image-rendering:pixelated
|
|
1008
1008
|
* @type {boolean}
|
|
@@ -1112,11 +1112,11 @@ let objectDefaultFriction = .8;
|
|
|
1112
1112
|
* @memberof Settings */
|
|
1113
1113
|
let objectMaxSpeed = 1;
|
|
1114
1114
|
|
|
1115
|
-
/** How much gravity to apply to objects
|
|
1116
|
-
* @type {
|
|
1115
|
+
/** How much gravity to apply to objects, negative Y is down
|
|
1116
|
+
* @type {Vector2}
|
|
1117
1117
|
* @default
|
|
1118
1118
|
* @memberof Settings */
|
|
1119
|
-
let gravity =
|
|
1119
|
+
let gravity = vec2();
|
|
1120
1120
|
|
|
1121
1121
|
/** Scales emit rate of particles, useful for low graphics mode (0 disables particle emitters)
|
|
1122
1122
|
* @type {number}
|
|
@@ -1342,8 +1342,8 @@ function setObjectDefaultFriction(friction) { objectDefaultFriction = friction;
|
|
|
1342
1342
|
* @memberof Settings */
|
|
1343
1343
|
function setObjectMaxSpeed(speed) { objectMaxSpeed = speed; }
|
|
1344
1344
|
|
|
1345
|
-
/** Set how much gravity to apply to objects
|
|
1346
|
-
* @param {
|
|
1345
|
+
/** Set how much gravity to apply to objects
|
|
1346
|
+
* @param {Vector2} newGravity
|
|
1347
1347
|
* @memberof Settings */
|
|
1348
1348
|
function setGravity(newGravity) { gravity = newGravity; }
|
|
1349
1349
|
|
|
@@ -1611,7 +1611,10 @@ class EngineObject
|
|
|
1611
1611
|
this.velocity.x *= this.damping;
|
|
1612
1612
|
this.velocity.y *= this.damping;
|
|
1613
1613
|
if (this.mass) // don't apply gravity to static objects
|
|
1614
|
-
|
|
1614
|
+
{
|
|
1615
|
+
this.velocity.x += gravity.x * this.gravityScale;
|
|
1616
|
+
this.velocity.y += gravity.y * this.gravityScale;
|
|
1617
|
+
}
|
|
1615
1618
|
this.pos.x += this.velocity.x;
|
|
1616
1619
|
this.pos.y += this.velocity.y;
|
|
1617
1620
|
this.angle += this.angleVelocity *= this.angleDamping;
|
|
@@ -1626,9 +1629,9 @@ class EngineObject
|
|
|
1626
1629
|
if (this.groundObject)
|
|
1627
1630
|
{
|
|
1628
1631
|
// apply friction in local space of ground object
|
|
1629
|
-
const
|
|
1630
|
-
|
|
1631
|
-
this.velocity.x = groundSpeed + (this.velocity.x - groundSpeed) *
|
|
1632
|
+
const friction = max(this.friction, this.groundObject.friction);
|
|
1633
|
+
const groundSpeed = this.groundObject.velocity ? this.groundObject.velocity.x : 0;
|
|
1634
|
+
this.velocity.x = groundSpeed + (this.velocity.x - groundSpeed) * friction;
|
|
1632
1635
|
this.groundObject = undefined;
|
|
1633
1636
|
//debugOverlay && debugPhysics && debugPoint(this.pos.subtract(vec2(0,this.size.y/2)), '#0f0');
|
|
1634
1637
|
}
|
|
@@ -1670,7 +1673,7 @@ class EngineObject
|
|
|
1670
1673
|
|
|
1671
1674
|
// check for collision
|
|
1672
1675
|
const sizeBoth = this.size.add(o.size);
|
|
1673
|
-
const smallStepUp = (oldPos.y - o.pos.y)*2 > sizeBoth.y + gravity; // prefer to push up if small delta
|
|
1676
|
+
const smallStepUp = (oldPos.y - o.pos.y)*2 > sizeBoth.y + gravity.y; // prefer to push up if small delta
|
|
1674
1677
|
const isBlockedX = abs(oldPos.y - o.pos.y)*2 < sizeBoth.y;
|
|
1675
1678
|
const isBlockedY = abs(oldPos.x - o.pos.x)*2 < sizeBoth.x;
|
|
1676
1679
|
const elasticity = max(this.elasticity, o.elasticity);
|
|
@@ -1732,19 +1735,21 @@ class EngineObject
|
|
|
1732
1735
|
if (this.collideTiles)
|
|
1733
1736
|
{
|
|
1734
1737
|
// check collision against tiles
|
|
1735
|
-
|
|
1738
|
+
const hitLayer = tileCollisionTest(this.pos, this.size, this)
|
|
1739
|
+
if (hitLayer)
|
|
1736
1740
|
{
|
|
1737
1741
|
// if already was stuck in collision, don't do anything
|
|
1738
1742
|
// this should not happen unless something starts in collision
|
|
1739
1743
|
if (!tileCollisionTest(oldPos, this.size, this))
|
|
1740
1744
|
{
|
|
1741
1745
|
// test which side we bounced off (or both if a corner)
|
|
1742
|
-
const
|
|
1743
|
-
const
|
|
1744
|
-
if (
|
|
1746
|
+
const blockedLayerY = tileCollisionTest(vec2(oldPos.x, this.pos.y), this.size, this);
|
|
1747
|
+
const blockedLayerX = tileCollisionTest(vec2(this.pos.x, oldPos.y), this.size, this);
|
|
1748
|
+
if (blockedLayerY || !blockedLayerX)
|
|
1745
1749
|
{
|
|
1746
1750
|
// bounce velocity
|
|
1747
|
-
|
|
1751
|
+
const elasticity = max(this.elasticity, hitLayer.elasticity);
|
|
1752
|
+
this.velocity.y *= -elasticity;
|
|
1748
1753
|
|
|
1749
1754
|
if (wasMovingDown)
|
|
1750
1755
|
{
|
|
@@ -1753,9 +1758,8 @@ class EngineObject
|
|
|
1753
1758
|
const epsilon = .0001;
|
|
1754
1759
|
this.pos.y = (oldPos.y-this.size.y/2|0)+this.size.y/2+epsilon;
|
|
1755
1760
|
|
|
1756
|
-
// set ground object
|
|
1757
|
-
|
|
1758
|
-
this.groundObject = this;
|
|
1761
|
+
// set ground object for tile collision
|
|
1762
|
+
this.groundObject = hitLayer;
|
|
1759
1763
|
}
|
|
1760
1764
|
else
|
|
1761
1765
|
{
|
|
@@ -1764,7 +1768,7 @@ class EngineObject
|
|
|
1764
1768
|
this.groundObject = undefined;
|
|
1765
1769
|
}
|
|
1766
1770
|
}
|
|
1767
|
-
if (
|
|
1771
|
+
if (blockedLayerX)
|
|
1768
1772
|
{
|
|
1769
1773
|
// move to previous position and bounce
|
|
1770
1774
|
this.pos.x = oldPos.x;
|
|
@@ -2864,7 +2868,9 @@ function gamepadsUpdate()
|
|
|
2864
2868
|
const button = gamepad.buttons[j];
|
|
2865
2869
|
const wasDown = gamepadIsDown(j,i);
|
|
2866
2870
|
data[j] = button.pressed ? wasDown ? 1 : 3 : wasDown ? 4 : 0;
|
|
2867
|
-
|
|
2871
|
+
if (!button.value || button.value > .9) // must be a full press
|
|
2872
|
+
if (!i && button.pressed)
|
|
2873
|
+
isUsingGamepad = true;
|
|
2868
2874
|
}
|
|
2869
2875
|
|
|
2870
2876
|
if (gamepadDirectionEmulateStick)
|
|
@@ -3539,75 +3545,47 @@ function zzfxG
|
|
|
3539
3545
|
* LittleJS Tile Layer System
|
|
3540
3546
|
* - Caches arrays of tiles to off screen canvas for fast rendering
|
|
3541
3547
|
* - Unlimited numbers of layers, allocates canvases as needed
|
|
3542
|
-
* - Interfaces with EngineObject for collision
|
|
3543
|
-
* - Collision layer is separate from visible layers
|
|
3544
|
-
* - It is recommended to have a visible layer that matches the collision
|
|
3545
3548
|
* - Tile layers can be drawn to using their context with canvas2d
|
|
3546
3549
|
* - Drawn directly to the main canvas without using WebGL
|
|
3550
|
+
* - Tile layers can also have collision with EngineObjects
|
|
3547
3551
|
* @namespace TileCollision
|
|
3548
3552
|
*/
|
|
3549
3553
|
|
|
3550
3554
|
|
|
3551
3555
|
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
* @memberof TileCollision */
|
|
3555
|
-
let tileCollision = [];
|
|
3556
|
-
|
|
3557
|
-
/** Size of the tile collision layer 2d grid
|
|
3558
|
-
* @type {Vector2}
|
|
3559
|
-
* @memberof TileCollision */
|
|
3560
|
-
let tileCollisionSize = vec2();
|
|
3561
|
-
|
|
3562
|
-
/** Clear and initialize tile collision
|
|
3563
|
-
* @param {Vector2} size - width and height of tile collision 2d grid
|
|
3564
|
-
* @memberof TileCollision */
|
|
3565
|
-
function initTileCollision(size)
|
|
3566
|
-
{
|
|
3567
|
-
tileCollisionSize = size;
|
|
3568
|
-
tileCollision = [];
|
|
3569
|
-
for (let i=tileCollision.length = tileCollisionSize.area(); i--;)
|
|
3570
|
-
tileCollision[i] = 0;
|
|
3571
|
-
}
|
|
3556
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
3557
|
+
// Tile Layer System
|
|
3572
3558
|
|
|
3573
|
-
/**
|
|
3574
|
-
* @
|
|
3575
|
-
* @param {number} [data]
|
|
3559
|
+
/** Keep track of all tile layers with collision
|
|
3560
|
+
* @type {Array<TileCollisionLayer>}
|
|
3576
3561
|
* @memberof TileCollision */
|
|
3577
|
-
|
|
3578
|
-
{
|
|
3579
|
-
pos.arrayCheck(tileCollisionSize) && (tileCollision[(pos.y|0)*tileCollisionSize.x+pos.x|0] = data);
|
|
3580
|
-
}
|
|
3562
|
+
let tileCollisionLayers = [];
|
|
3581
3563
|
|
|
3582
3564
|
/** Get tile collision data for a given cell in the grid
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3565
|
+
* @param {Vector2} pos
|
|
3566
|
+
* @return {number}
|
|
3567
|
+
* @memberof TileCollision */
|
|
3586
3568
|
function getTileCollisionData(pos)
|
|
3587
3569
|
{
|
|
3588
|
-
|
|
3570
|
+
// check all tile collision layers
|
|
3571
|
+
for (const layer of tileCollisionLayers)
|
|
3572
|
+
if (pos.arrayCheck(layer.size))
|
|
3573
|
+
return layer.getCollisionData(pos);
|
|
3574
|
+
return 0;
|
|
3589
3575
|
}
|
|
3590
3576
|
|
|
3591
|
-
/** Check if
|
|
3577
|
+
/** Check if a tile layer collides with another object
|
|
3592
3578
|
* @param {Vector2} pos
|
|
3593
3579
|
* @param {Vector2} [size=(0,0)]
|
|
3594
3580
|
* @param {EngineObject} [object]
|
|
3595
|
-
* @return {
|
|
3581
|
+
* @return {TileCollisionLayer}
|
|
3596
3582
|
* @memberof TileCollision */
|
|
3597
3583
|
function tileCollisionTest(pos, size=vec2(), object)
|
|
3598
3584
|
{
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
for (let y = minY; y < maxY; ++y)
|
|
3604
|
-
for (let x = minX; x < maxX; ++x)
|
|
3605
|
-
{
|
|
3606
|
-
const tileData = tileCollision[y*tileCollisionSize.x+x];
|
|
3607
|
-
if (tileData && (!object || object.collideWithTile(tileData, vec2(x, y))))
|
|
3608
|
-
return true;
|
|
3609
|
-
}
|
|
3610
|
-
return false;
|
|
3585
|
+
// check all tile collision layers
|
|
3586
|
+
for (const layer of tileCollisionLayers)
|
|
3587
|
+
if (layer.collisionTest(pos, size, object))
|
|
3588
|
+
return layer;
|
|
3611
3589
|
}
|
|
3612
3590
|
|
|
3613
3591
|
/** Return the center of first tile hit, undefined if nothing was hit.
|
|
@@ -3619,49 +3597,17 @@ function tileCollisionTest(pos, size=vec2(), object)
|
|
|
3619
3597
|
* @memberof TileCollision */
|
|
3620
3598
|
function tileCollisionRaycast(posStart, posEnd, object)
|
|
3621
3599
|
{
|
|
3622
|
-
//
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
const flooredPosStart = posStart.floor();
|
|
3629
|
-
|
|
3630
|
-
// setup iteration variables
|
|
3631
|
-
let pos = flooredPosStart;
|
|
3632
|
-
let xi = unit.x * (delta.x < 0 ? posStart.x - pos.x : pos.x - posStart.x + 1);
|
|
3633
|
-
let yi = unit.y * (delta.y < 0 ? posStart.y - pos.y : pos.y - posStart.y + 1);
|
|
3634
|
-
|
|
3635
|
-
while (true)
|
|
3636
|
-
{
|
|
3637
|
-
// check for tile collision
|
|
3638
|
-
const tileData = getTileCollisionData(pos);
|
|
3639
|
-
if (tileData && (!object || object.collideWithTile(tileData, pos)))
|
|
3640
|
-
{
|
|
3641
|
-
debugRaycast && debugLine(posStart, posEnd, '#f00', .02);
|
|
3642
|
-
debugRaycast && debugPoint(pos.add(vec2(.5)), '#ff0');
|
|
3643
|
-
return pos.add(vec2(.5));
|
|
3644
|
-
}
|
|
3645
|
-
|
|
3646
|
-
// check if past the end
|
|
3647
|
-
if (xi > totalLength && yi > totalLength)
|
|
3648
|
-
break;
|
|
3649
|
-
|
|
3650
|
-
// get coordinates of the next tile to check
|
|
3651
|
-
if (xi > yi)
|
|
3652
|
-
pos.y += sign(delta.y), yi += unit.y;
|
|
3653
|
-
else
|
|
3654
|
-
pos.x += sign(delta.x), xi += unit.x;
|
|
3600
|
+
// check all tile collision layers
|
|
3601
|
+
for (const layer of tileCollisionLayers)
|
|
3602
|
+
{
|
|
3603
|
+
const hitPos = layer.collisionRaycast(posStart, posEnd, object)
|
|
3604
|
+
if (hitPos)
|
|
3605
|
+
return hitPos;
|
|
3655
3606
|
}
|
|
3656
|
-
|
|
3657
|
-
debugRaycast && debugLine(posStart, posEnd, '#00f', .02);
|
|
3658
3607
|
}
|
|
3659
3608
|
|
|
3660
|
-
///////////////////////////////////////////////////////////////////////////////
|
|
3661
|
-
// Tile Layer Rendering System
|
|
3662
|
-
|
|
3663
3609
|
/**
|
|
3664
|
-
* Tile layer data object stores info about how to
|
|
3610
|
+
* Tile layer data object stores info about how to draw a tile
|
|
3665
3611
|
* @example
|
|
3666
3612
|
* // create tile layer data with tile index 0 and random orientation and color
|
|
3667
3613
|
* const tileIndex = 0;
|
|
@@ -3693,28 +3639,27 @@ class TileLayerData
|
|
|
3693
3639
|
clear() { this.tile = this.direction = 0; this.mirror = false; this.color = new Color; }
|
|
3694
3640
|
}
|
|
3695
3641
|
|
|
3642
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
3696
3643
|
/**
|
|
3697
3644
|
* Tile Layer - cached rendering system for tile layers
|
|
3698
3645
|
* - Each Tile layer is rendered to an off screen canvas
|
|
3699
3646
|
* - To allow dynamic modifications, layers are rendered using canvas 2d
|
|
3700
3647
|
* - Some devices like mobile phones are limited to 4k texture resolution
|
|
3701
|
-
* -
|
|
3648
|
+
* - For with 16x16 tiles this limits layers to 256x256 on mobile devices
|
|
3702
3649
|
* @extends EngineObject
|
|
3703
3650
|
* @example
|
|
3704
|
-
*
|
|
3705
|
-
* initTileCollision(vec2(200,100));
|
|
3706
|
-
* const tileLayer = new TileLayer();
|
|
3651
|
+
* const tileLayer = new TileLayer(vec2(), vec2(200,100));
|
|
3707
3652
|
*/
|
|
3708
3653
|
class TileLayer extends EngineObject
|
|
3709
3654
|
{
|
|
3710
3655
|
/** Create a tile layer object
|
|
3711
|
-
* @param {Vector2} [position=(0,0)]
|
|
3712
|
-
* @param {Vector2} [size=
|
|
3713
|
-
* @param {TileInfo} [tileInfo]
|
|
3714
|
-
* @param {Vector2} [scale=(1,1)]
|
|
3715
|
-
* @param {number} [renderOrder]
|
|
3656
|
+
* @param {Vector2} [position=(0,0)] - World space position
|
|
3657
|
+
* @param {Vector2} [size=(1,1)] - World space size
|
|
3658
|
+
* @param {TileInfo} [tileInfo] - Tile info for layer
|
|
3659
|
+
* @param {Vector2} [scale=(1,1)] - How much to scale this layer when rendered
|
|
3660
|
+
* @param {number} [renderOrder] - Objects are sorted by renderOrder
|
|
3716
3661
|
*/
|
|
3717
|
-
constructor(position, size
|
|
3662
|
+
constructor(position, size, tileInfo=tile(), scale=vec2(1), renderOrder=0)
|
|
3718
3663
|
{
|
|
3719
3664
|
super(position, size, tileInfo, 0, undefined, renderOrder);
|
|
3720
3665
|
|
|
@@ -3726,6 +3671,10 @@ class TileLayer extends EngineObject
|
|
|
3726
3671
|
this.scale = scale;
|
|
3727
3672
|
/** @property {boolean} - If true this layer will render to overlay canvas and appear above all objects */
|
|
3728
3673
|
this.isOverlay = false;
|
|
3674
|
+
// set no friction by default, applied friction is max of both objects
|
|
3675
|
+
this.friction = 0;
|
|
3676
|
+
// set no elasticity by default, applied elasticity is max of both objects
|
|
3677
|
+
this.elasticity = 0;
|
|
3729
3678
|
|
|
3730
3679
|
// init tile data
|
|
3731
3680
|
this.data = [];
|
|
@@ -3922,6 +3871,146 @@ class TileLayer extends EngineObject
|
|
|
3922
3871
|
* @param {number} [angle=0] */
|
|
3923
3872
|
drawRect(pos, size, color, angle)
|
|
3924
3873
|
{ this.drawTile(pos, size, undefined, color, angle); }
|
|
3874
|
+
}
|
|
3875
|
+
|
|
3876
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
3877
|
+
/**
|
|
3878
|
+
* Tile Collision Layer - a tile layer with collision
|
|
3879
|
+
* - adds collision data and functions to TileLayer
|
|
3880
|
+
* - there can be multiple tile collision layers
|
|
3881
|
+
* - tile collison layers should not overlap each other
|
|
3882
|
+
* @extends TileLayer
|
|
3883
|
+
*/
|
|
3884
|
+
class TileCollisionLayer extends TileLayer
|
|
3885
|
+
{
|
|
3886
|
+
/** Create a tile layer object
|
|
3887
|
+
* @param {Vector2} [position=(0,0)] - World space position
|
|
3888
|
+
* @param {Vector2} [size=(0,0)] - World space size
|
|
3889
|
+
* @param {TileInfo} [tileInfo] - Tile info for layer
|
|
3890
|
+
* @param {number} [renderOrder] - Objects are sorted by renderOrder
|
|
3891
|
+
*/
|
|
3892
|
+
constructor(position, size, tileInfo=tile(), renderOrder=0)
|
|
3893
|
+
{
|
|
3894
|
+
const scale = vec2(1); // collision layers are not scaled
|
|
3895
|
+
super(position, size.floor(), tileInfo, scale, renderOrder);
|
|
3896
|
+
|
|
3897
|
+
/** @property {Array<number>} - The tile collision grid */
|
|
3898
|
+
this.collisionData = [];
|
|
3899
|
+
this.initCollision(this.size);
|
|
3900
|
+
|
|
3901
|
+
// keep track of all collision layers
|
|
3902
|
+
tileCollisionLayers.push(this);
|
|
3903
|
+
}
|
|
3904
|
+
|
|
3905
|
+
/** Destroy this collision layer */
|
|
3906
|
+
destroy()
|
|
3907
|
+
{
|
|
3908
|
+
if (this.destroyed)
|
|
3909
|
+
return;
|
|
3910
|
+
|
|
3911
|
+
// remove from collision layers array and destroy
|
|
3912
|
+
const index = tileCollisionLayers.indexOf(this);
|
|
3913
|
+
ASSERT(index >= 0, 'tile collision layer not found in array');
|
|
3914
|
+
tileCollisionLayers.splice(index, 1);
|
|
3915
|
+
super.destroy();
|
|
3916
|
+
}
|
|
3917
|
+
|
|
3918
|
+
/** Clear and initialize tile collision to new size
|
|
3919
|
+
* @param {Vector2} size - width and height of tile collision 2d grid */
|
|
3920
|
+
initCollision(size)
|
|
3921
|
+
{
|
|
3922
|
+
this.size = size.floor();
|
|
3923
|
+
this.collisionData = [];
|
|
3924
|
+
this.collisionData.length = size.area();
|
|
3925
|
+
this.collisionData.fill(0);
|
|
3926
|
+
}
|
|
3927
|
+
|
|
3928
|
+
/** Set tile collision data for a given cell in the grid
|
|
3929
|
+
* @param {Vector2} pos
|
|
3930
|
+
* @param {number} [data] */
|
|
3931
|
+
setCollisionData(pos, data=1)
|
|
3932
|
+
{
|
|
3933
|
+
const i = (pos.y|0)*this.size.x + pos.x|0;
|
|
3934
|
+
pos.arrayCheck(this.size) && (this.collisionData[i] = data);
|
|
3935
|
+
}
|
|
3936
|
+
|
|
3937
|
+
/** Get tile collision data for a given cell in the grid
|
|
3938
|
+
* @param {Vector2} pos
|
|
3939
|
+
* @return {number} */
|
|
3940
|
+
getCollisionData(pos)
|
|
3941
|
+
{
|
|
3942
|
+
const i = (pos.y|0)*this.size.x + pos.x|0;
|
|
3943
|
+
return pos.arrayCheck(this.size) ? this.collisionData[i] : 0;
|
|
3944
|
+
}
|
|
3945
|
+
|
|
3946
|
+
/** Check if collision with another object should occur
|
|
3947
|
+
* @param {Vector2} pos
|
|
3948
|
+
* @param {Vector2} [size=(0,0)]
|
|
3949
|
+
* @param {EngineObject} [object]
|
|
3950
|
+
* @return {boolean} */
|
|
3951
|
+
collisionTest(pos, size=vec2(), object)
|
|
3952
|
+
{
|
|
3953
|
+
const minX = max(pos.x - size.x/2|0, 0);
|
|
3954
|
+
const minY = max(pos.y - size.y/2|0, 0);
|
|
3955
|
+
const maxX = min(pos.x + size.x/2, this.size.x);
|
|
3956
|
+
const maxY = min(pos.y + size.y/2, this.size.y);
|
|
3957
|
+
for (let y = minY; y < maxY; ++y)
|
|
3958
|
+
for (let x = minX; x < maxX; ++x)
|
|
3959
|
+
{
|
|
3960
|
+
// check if the object should collide with this tile
|
|
3961
|
+
const tileData = this.collisionData[y*this.size.x+x];
|
|
3962
|
+
if (tileData && (!object || object.collideWithTile(tileData, vec2(x, y))))
|
|
3963
|
+
return true;
|
|
3964
|
+
}
|
|
3965
|
+
return false;
|
|
3966
|
+
}
|
|
3967
|
+
|
|
3968
|
+
/** Return the center of first tile hit, undefined if nothing was hit.
|
|
3969
|
+
* This does not return the exact intersection, but the center of the tile hit.
|
|
3970
|
+
* @param {Vector2} posStart
|
|
3971
|
+
* @param {Vector2} posEnd
|
|
3972
|
+
* @param {EngineObject} [object]
|
|
3973
|
+
* @return {Vector2} */
|
|
3974
|
+
collisionRaycast(posStart, posEnd, object)
|
|
3975
|
+
{
|
|
3976
|
+
// test if a ray collides with tiles from start to end
|
|
3977
|
+
// todo: a way to get the exact hit point, it must still be inside the hit tile
|
|
3978
|
+
const delta = posEnd.subtract(posStart);
|
|
3979
|
+
const totalLength = delta.length();
|
|
3980
|
+
const normalizedDelta = delta.normalize();
|
|
3981
|
+
const unit = vec2(abs(1/normalizedDelta.x), abs(1/normalizedDelta.y));
|
|
3982
|
+
const flooredPosStart = posStart.floor();
|
|
3983
|
+
|
|
3984
|
+
// setup iteration variables
|
|
3985
|
+
let pos = flooredPosStart;
|
|
3986
|
+
let xi = unit.x * (delta.x < 0 ? posStart.x - pos.x : pos.x - posStart.x + 1);
|
|
3987
|
+
let yi = unit.y * (delta.y < 0 ? posStart.y - pos.y : pos.y - posStart.y + 1);
|
|
3988
|
+
|
|
3989
|
+
// use line drawing algorithm to test for collisions
|
|
3990
|
+
while (true)
|
|
3991
|
+
{
|
|
3992
|
+
// check for tile collision
|
|
3993
|
+
const tileData = this.getCollisionData(pos);
|
|
3994
|
+
if (tileData && (!object || object.collideWithTile(tileData, pos)))
|
|
3995
|
+
{
|
|
3996
|
+
debugRaycast && debugLine(posStart, posEnd, '#f00', .02);
|
|
3997
|
+
debugRaycast && debugPoint(pos.add(vec2(.5)), '#ff0');
|
|
3998
|
+
return pos.add(vec2(.5));
|
|
3999
|
+
}
|
|
4000
|
+
|
|
4001
|
+
// check if past the end
|
|
4002
|
+
if (xi > totalLength && yi > totalLength)
|
|
4003
|
+
break;
|
|
4004
|
+
|
|
4005
|
+
// get coordinates of next tile to check
|
|
4006
|
+
if (xi > yi)
|
|
4007
|
+
pos.y += sign(delta.y), yi += unit.y;
|
|
4008
|
+
else
|
|
4009
|
+
pos.x += sign(delta.x), xi += unit.x;
|
|
4010
|
+
}
|
|
4011
|
+
|
|
4012
|
+
debugRaycast && debugLine(posStart, posEnd, '#00f', .02);
|
|
4013
|
+
}
|
|
3925
4014
|
}
|
|
3926
4015
|
/**
|
|
3927
4016
|
* LittleJS Particle System
|
|
@@ -4798,7 +4887,7 @@ const engineName = 'LittleJS';
|
|
|
4798
4887
|
* @type {string}
|
|
4799
4888
|
* @default
|
|
4800
4889
|
* @memberof Engine */
|
|
4801
|
-
const engineVersion = '1.
|
|
4890
|
+
const engineVersion = '1.12.4';
|
|
4802
4891
|
|
|
4803
4892
|
/** Frames per second to update
|
|
4804
4893
|
* @type {number}
|
|
@@ -5392,3 +5481,2787 @@ function drawEngineSplashScreen(t)
|
|
|
5392
5481
|
x.restore();
|
|
5393
5482
|
}
|
|
5394
5483
|
|
|
5484
|
+
/**
|
|
5485
|
+
* LittleJS Newgrounds API
|
|
5486
|
+
* - NewgroundsMedal extends Medal with Newgrounds API functionality
|
|
5487
|
+
* - Call new NewgroundsPlugin() to setup Newgrounds
|
|
5488
|
+
* - Uses CryptoJS for encryption if optional cipher is provided
|
|
5489
|
+
* - Keeps connection alive and logs views
|
|
5490
|
+
* - Functions to interact with scoreboards
|
|
5491
|
+
* - Functions to unlock medals
|
|
5492
|
+
*/
|
|
5493
|
+
|
|
5494
|
+
|
|
5495
|
+
|
|
5496
|
+
/** Global Newgrounds object
|
|
5497
|
+
* @type {NewgroundsPlugin}
|
|
5498
|
+
* @memberof Medal */
|
|
5499
|
+
let newgrounds;
|
|
5500
|
+
|
|
5501
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5502
|
+
/**
|
|
5503
|
+
* Newgrounds medal auto unlocks in newgrounds API
|
|
5504
|
+
* @extends Medal
|
|
5505
|
+
*/
|
|
5506
|
+
class NewgroundsMedal extends Medal
|
|
5507
|
+
{
|
|
5508
|
+
/** Create a newgrounds medal object and adds it to the list of medals
|
|
5509
|
+
* @param {Number} id - The unique identifier of the medal
|
|
5510
|
+
* @param {String} name - Name of the medal
|
|
5511
|
+
* @param {String} [description] - Description of the medal
|
|
5512
|
+
* @param {String} [icon] - Icon for the medal
|
|
5513
|
+
* @param {String} [src] - Image location for the medal
|
|
5514
|
+
*/
|
|
5515
|
+
constructor(id, name, description, icon, src)
|
|
5516
|
+
{ super(id, name, description, icon, src); }
|
|
5517
|
+
|
|
5518
|
+
/** Unlocks a medal if not already unlocked */
|
|
5519
|
+
unlock()
|
|
5520
|
+
{
|
|
5521
|
+
super.unlock();
|
|
5522
|
+
newgrounds && newgrounds.unlockMedal(this.id);
|
|
5523
|
+
}
|
|
5524
|
+
}
|
|
5525
|
+
|
|
5526
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5527
|
+
/**
|
|
5528
|
+
* Newgrounds API object
|
|
5529
|
+
*/
|
|
5530
|
+
class NewgroundsPlugin
|
|
5531
|
+
{
|
|
5532
|
+
/** Create the global newgrounds object
|
|
5533
|
+
* @param {string} app_id - The newgrounds App ID
|
|
5534
|
+
* @param {string} [cipher] - The encryption Key (AES-128/Base64)
|
|
5535
|
+
* @param {Object} [cryptoJS] - An instance of CryptoJS, if there is a cipher
|
|
5536
|
+
* @example
|
|
5537
|
+
* // create the newgrounds object, replace the app id with your own
|
|
5538
|
+
* const app_id = 'your_app_id_here';
|
|
5539
|
+
* new NewgroundsPlugin(app_id);
|
|
5540
|
+
*/
|
|
5541
|
+
constructor(app_id, cipher, cryptoJS)
|
|
5542
|
+
{
|
|
5543
|
+
ASSERT(!newgrounds, 'there can only be one newgrounds object');
|
|
5544
|
+
ASSERT(!cipher || cryptoJS, 'must provide cryptojs if there is a cipher');
|
|
5545
|
+
|
|
5546
|
+
newgrounds = this; // set global newgrounds object
|
|
5547
|
+
this.app_id = app_id;
|
|
5548
|
+
this.cipher = cipher;
|
|
5549
|
+
this.cryptoJS = cryptoJS;
|
|
5550
|
+
this.host = location ? location.hostname : '';
|
|
5551
|
+
|
|
5552
|
+
// get session id from url search params
|
|
5553
|
+
const url = new URL(location.href);
|
|
5554
|
+
this.session_id = url.searchParams.get('ngio_session_id');
|
|
5555
|
+
|
|
5556
|
+
if (!this.session_id)
|
|
5557
|
+
return; // only use newgrounds when logged in
|
|
5558
|
+
|
|
5559
|
+
// get medals
|
|
5560
|
+
const medalsResult = this.call('Medal.getList');
|
|
5561
|
+
this.medals = medalsResult ? medalsResult.result.data['medals'] : [];
|
|
5562
|
+
debugMedals && console.log(this.medals);
|
|
5563
|
+
for (const newgroundsMedal of this.medals)
|
|
5564
|
+
{
|
|
5565
|
+
const medal = medals[newgroundsMedal['id']];
|
|
5566
|
+
if (medal)
|
|
5567
|
+
{
|
|
5568
|
+
// copy newgrounds medal data
|
|
5569
|
+
medal.image = new Image;
|
|
5570
|
+
medal.image.src = newgroundsMedal['icon'];
|
|
5571
|
+
medal.name = newgroundsMedal['name'];
|
|
5572
|
+
medal.description = newgroundsMedal['description'];
|
|
5573
|
+
medal.unlocked = newgroundsMedal['unlocked'];
|
|
5574
|
+
medal.difficulty = newgroundsMedal['difficulty'];
|
|
5575
|
+
medal.value = newgroundsMedal['value'];
|
|
5576
|
+
|
|
5577
|
+
if (medal.value) // add value to description
|
|
5578
|
+
medal.description = medal.description + ` (${ medal.value })`;
|
|
5579
|
+
}
|
|
5580
|
+
}
|
|
5581
|
+
|
|
5582
|
+
// get scoreboards
|
|
5583
|
+
const scoreboardResult = this.call('ScoreBoard.getBoards');
|
|
5584
|
+
this.scoreboards = scoreboardResult ? scoreboardResult.result.data.scoreboards : [];
|
|
5585
|
+
debugMedals && console.log(this.scoreboards);
|
|
5586
|
+
|
|
5587
|
+
// keep the session alive with a ping every minute
|
|
5588
|
+
const keepAliveMS = 60 * 1e3;
|
|
5589
|
+
setInterval(()=>this.call('Gateway.ping', 0, true), keepAliveMS);
|
|
5590
|
+
}
|
|
5591
|
+
|
|
5592
|
+
/** Send message to unlock a medal by id
|
|
5593
|
+
* @param {number} id - The medal id */
|
|
5594
|
+
unlockMedal(id) { return this.call('Medal.unlock', {'id':id}, true); }
|
|
5595
|
+
|
|
5596
|
+
/** Send message to post score
|
|
5597
|
+
* @param {number} id - The scoreboard id
|
|
5598
|
+
* @param {number} value - The score value */
|
|
5599
|
+
postScore(id, value) { return this.call('ScoreBoard.postScore', {'id':id, 'value':value}, true); }
|
|
5600
|
+
|
|
5601
|
+
/** Get scores from a scoreboard
|
|
5602
|
+
* @param {number} id - The scoreboard id
|
|
5603
|
+
* @param {string} [user] - A user's id or name
|
|
5604
|
+
* @param {number} [social] - If true, only social scores will be loaded
|
|
5605
|
+
* @param {number} [skip] - Number of scores to skip before start
|
|
5606
|
+
* @param {number} [limit] - Number of scores to include in the list
|
|
5607
|
+
* @return {Object} - The response JSON object
|
|
5608
|
+
*/
|
|
5609
|
+
getScores(id, user, social=0, skip=0, limit=10)
|
|
5610
|
+
{ return this.call('ScoreBoard.getScores', {'id':id, 'user':user, 'social':social, 'skip':skip, 'limit':limit}); }
|
|
5611
|
+
|
|
5612
|
+
/** Send message to log a view */
|
|
5613
|
+
logView() { return this.call('App.logView', {'host':this.host}, true); }
|
|
5614
|
+
|
|
5615
|
+
/** Send a message to call a component of the Newgrounds API
|
|
5616
|
+
* @param {string} component - Name of the component
|
|
5617
|
+
* @param {Object} [parameters] - Parameters to use for call
|
|
5618
|
+
* @param {boolean} [async] - If true, don't wait for response before continuing
|
|
5619
|
+
* @return {Object} - The response JSON object
|
|
5620
|
+
*/
|
|
5621
|
+
call(component, parameters, async=false)
|
|
5622
|
+
{
|
|
5623
|
+
const call = {'component':component, 'parameters':parameters};
|
|
5624
|
+
if (this.cipher)
|
|
5625
|
+
{
|
|
5626
|
+
// encrypt using AES-128 Base64 with cryptoJS
|
|
5627
|
+
const cryptoJS = this.cryptoJS;
|
|
5628
|
+
const aesKey = cryptoJS['enc']['Base64']['parse'](this.cipher);
|
|
5629
|
+
const iv = cryptoJS['lib']['WordArray']['random'](16);
|
|
5630
|
+
const encrypted = cryptoJS['AES']['encrypt'](JSON.stringify(call), aesKey, {'iv':iv});
|
|
5631
|
+
call['secure'] = cryptoJS['enc']['Base64']['stringify'](iv.concat(encrypted['ciphertext']));
|
|
5632
|
+
call['parameters'] = 0;
|
|
5633
|
+
}
|
|
5634
|
+
|
|
5635
|
+
// build the input object
|
|
5636
|
+
const input =
|
|
5637
|
+
{
|
|
5638
|
+
'app_id': this.app_id,
|
|
5639
|
+
'session_id': this.session_id,
|
|
5640
|
+
'call': call
|
|
5641
|
+
};
|
|
5642
|
+
|
|
5643
|
+
// build post data
|
|
5644
|
+
const formData = new FormData();
|
|
5645
|
+
formData.append('input', JSON.stringify(input));
|
|
5646
|
+
|
|
5647
|
+
// send post data
|
|
5648
|
+
const xmlHttp = new XMLHttpRequest();
|
|
5649
|
+
const url = 'https://newgrounds.io/gateway_v3.php';
|
|
5650
|
+
xmlHttp.open('POST', url, !debugMedals && async);
|
|
5651
|
+
try { xmlHttp.send(formData); }
|
|
5652
|
+
catch(e)
|
|
5653
|
+
{
|
|
5654
|
+
debugMedals && console.log('newgrounds call failed', e);
|
|
5655
|
+
return;
|
|
5656
|
+
}
|
|
5657
|
+
debugMedals && console.log(xmlHttp.responseText);
|
|
5658
|
+
return xmlHttp.responseText && JSON.parse(xmlHttp.responseText);
|
|
5659
|
+
}
|
|
5660
|
+
}
|
|
5661
|
+
|
|
5662
|
+
/**
|
|
5663
|
+
* LittleJS Post Processing Plugin
|
|
5664
|
+
* - Supports shadertoy style post processing shaders
|
|
5665
|
+
* - call new new PostProcessPlugin() to setup post processing
|
|
5666
|
+
* - can be enabled to pass other canvases through a final shader
|
|
5667
|
+
*/
|
|
5668
|
+
|
|
5669
|
+
|
|
5670
|
+
|
|
5671
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5672
|
+
|
|
5673
|
+
/** Global Post Process plugin object
|
|
5674
|
+
* @type {PostProcessPlugin} */
|
|
5675
|
+
let postProcess;
|
|
5676
|
+
|
|
5677
|
+
/////////////////////////////////////////////////////////////////////////
|
|
5678
|
+
/**
|
|
5679
|
+
* UI System Global Object
|
|
5680
|
+
*/
|
|
5681
|
+
class PostProcessPlugin
|
|
5682
|
+
{
|
|
5683
|
+
/** Create global post processing shader
|
|
5684
|
+
* @param {string} shaderCode
|
|
5685
|
+
* @param {boolean} [includeOverlay]
|
|
5686
|
+
* @example
|
|
5687
|
+
* // create the post process plugin object
|
|
5688
|
+
* new PostProcessPlugin(shaderCode);
|
|
5689
|
+
*/
|
|
5690
|
+
constructor(shaderCode, includeOverlay=false)
|
|
5691
|
+
{
|
|
5692
|
+
ASSERT(!postProcess, 'Post process already initialized');
|
|
5693
|
+
postProcess = this;
|
|
5694
|
+
|
|
5695
|
+
if (headlessMode) return;
|
|
5696
|
+
if (!shaderCode) // default shader pass through
|
|
5697
|
+
shaderCode = 'void mainImage(out vec4 c,vec2 p){c=texture(iChannel0,p/iResolution.xy);}';
|
|
5698
|
+
|
|
5699
|
+
/** @property {WebGLProgram} - Shader for post processing */
|
|
5700
|
+
this.shader = glCreateProgram(
|
|
5701
|
+
'#version 300 es\n' + // specify GLSL ES version
|
|
5702
|
+
'precision highp float;'+ // use highp for better accuracy
|
|
5703
|
+
'in vec2 p;'+ // position
|
|
5704
|
+
'void main(){'+ // shader entry point
|
|
5705
|
+
'gl_Position=vec4(p+p-1.,1,1);'+ // set position
|
|
5706
|
+
'}' // end of shader
|
|
5707
|
+
,
|
|
5708
|
+
'#version 300 es\n' + // specify GLSL ES version
|
|
5709
|
+
'precision highp float;'+ // use highp for better accuracy
|
|
5710
|
+
'uniform sampler2D iChannel0;'+ // input texture
|
|
5711
|
+
'uniform vec3 iResolution;'+ // size of output texture
|
|
5712
|
+
'uniform float iTime;'+ // time
|
|
5713
|
+
'out vec4 c;'+ // out color
|
|
5714
|
+
'\n' + shaderCode + '\n'+ // insert custom shader code
|
|
5715
|
+
'void main(){'+ // shader entry point
|
|
5716
|
+
'mainImage(c,gl_FragCoord.xy);'+ // call post process function
|
|
5717
|
+
'c.a=1.;'+ // always use full alpha
|
|
5718
|
+
'}' // end of shader
|
|
5719
|
+
);
|
|
5720
|
+
|
|
5721
|
+
/** @property {WebGLTexture} - Texture for post processing */
|
|
5722
|
+
this.texture = glCreateTexture();
|
|
5723
|
+
|
|
5724
|
+
/** @property {boolean} - Should overlay canvas be included in post processing */
|
|
5725
|
+
this.includeOverlay = includeOverlay;
|
|
5726
|
+
|
|
5727
|
+
// Render the post processing shader, called automatically by the engine
|
|
5728
|
+
engineAddPlugin(undefined, postProcessRender);
|
|
5729
|
+
function postProcessRender()
|
|
5730
|
+
{
|
|
5731
|
+
if (headlessMode) return;
|
|
5732
|
+
|
|
5733
|
+
// prepare to render post process shader
|
|
5734
|
+
if (glEnable)
|
|
5735
|
+
{
|
|
5736
|
+
glFlush(); // clear out the buffer
|
|
5737
|
+
mainContext.drawImage(glCanvas, 0, 0); // copy to the main canvas
|
|
5738
|
+
}
|
|
5739
|
+
else
|
|
5740
|
+
{
|
|
5741
|
+
// set the viewport
|
|
5742
|
+
glContext.viewport(0, 0, glCanvas.width = mainCanvas.width, glCanvas.height = mainCanvas.height);
|
|
5743
|
+
}
|
|
5744
|
+
|
|
5745
|
+
if (postProcess.includeOverlay)
|
|
5746
|
+
{
|
|
5747
|
+
// copy overlay canvas so it will be included in post processing
|
|
5748
|
+
mainContext.drawImage(overlayCanvas, 0, 0);
|
|
5749
|
+
overlayCanvas.width |= 0;
|
|
5750
|
+
}
|
|
5751
|
+
|
|
5752
|
+
// setup shader program to draw one triangle
|
|
5753
|
+
glContext.useProgram(postProcess.shader);
|
|
5754
|
+
glContext.bindBuffer(glContext.ARRAY_BUFFER, glGeometryBuffer);
|
|
5755
|
+
glContext.pixelStorei(glContext.UNPACK_FLIP_Y_WEBGL, 1);
|
|
5756
|
+
glContext.disable(glContext.BLEND);
|
|
5757
|
+
|
|
5758
|
+
// set textures, pass in the 2d canvas and gl canvas in separate texture channels
|
|
5759
|
+
glContext.activeTexture(glContext.TEXTURE0);
|
|
5760
|
+
glContext.bindTexture(glContext.TEXTURE_2D, postProcess.texture);
|
|
5761
|
+
glContext.texImage2D(glContext.TEXTURE_2D, 0, glContext.RGBA, glContext.RGBA, glContext.UNSIGNED_BYTE, mainCanvas);
|
|
5762
|
+
|
|
5763
|
+
// set vertex position attribute
|
|
5764
|
+
const vertexByteStride = 8;
|
|
5765
|
+
const pLocation = glContext.getAttribLocation(postProcess.shader, 'p');
|
|
5766
|
+
glContext.enableVertexAttribArray(pLocation);
|
|
5767
|
+
glContext.vertexAttribPointer(pLocation, 2, glContext.FLOAT, false, vertexByteStride, 0);
|
|
5768
|
+
|
|
5769
|
+
// set uniforms and draw
|
|
5770
|
+
const uniformLocation = (name)=>glContext.getUniformLocation(postProcess.shader, name);
|
|
5771
|
+
glContext.uniform1i(uniformLocation('iChannel0'), 0);
|
|
5772
|
+
glContext.uniform1f(uniformLocation('iTime'), time);
|
|
5773
|
+
glContext.uniform3f(uniformLocation('iResolution'), mainCanvas.width, mainCanvas.height, 1);
|
|
5774
|
+
glContext.drawArrays(glContext.TRIANGLE_STRIP, 0, 4);
|
|
5775
|
+
}
|
|
5776
|
+
}
|
|
5777
|
+
}
|
|
5778
|
+
|
|
5779
|
+
/**
|
|
5780
|
+
* LittleJS ZzFXM Plugin
|
|
5781
|
+
*/
|
|
5782
|
+
|
|
5783
|
+
|
|
5784
|
+
|
|
5785
|
+
/**
|
|
5786
|
+
* Music Object - Stores a zzfx music track for later use
|
|
5787
|
+
*
|
|
5788
|
+
* <a href=https://keithclark.github.io/ZzFXM/>Create music with the ZzFXM tracker.</a>
|
|
5789
|
+
* @example
|
|
5790
|
+
* // create some music
|
|
5791
|
+
* const music_example = new Music(
|
|
5792
|
+
* [
|
|
5793
|
+
* [ // instruments
|
|
5794
|
+
* [,0,400] // simple note
|
|
5795
|
+
* ],
|
|
5796
|
+
* [ // patterns
|
|
5797
|
+
* [ // pattern 1
|
|
5798
|
+
* [ // channel 0
|
|
5799
|
+
* 0, -1, // instrument 0, left speaker
|
|
5800
|
+
* 1, 0, 9, 1 // channel notes
|
|
5801
|
+
* ],
|
|
5802
|
+
* [ // channel 1
|
|
5803
|
+
* 0, 1, // instrument 0, right speaker
|
|
5804
|
+
* 0, 12, 17, -1 // channel notes
|
|
5805
|
+
* ]
|
|
5806
|
+
* ],
|
|
5807
|
+
* ],
|
|
5808
|
+
* [0, 0, 0, 0], // sequence, play pattern 0 four times
|
|
5809
|
+
* 90 // BPM
|
|
5810
|
+
* ]);
|
|
5811
|
+
*
|
|
5812
|
+
* // play the music
|
|
5813
|
+
* music_example.play();
|
|
5814
|
+
*/
|
|
5815
|
+
class ZzFXMusic extends Sound
|
|
5816
|
+
{
|
|
5817
|
+
/** Create a music object and cache the zzfx music samples for later use
|
|
5818
|
+
* @param {[Array, Array, Array, number]} zzfxMusic - Array of zzfx music parameters
|
|
5819
|
+
*/
|
|
5820
|
+
constructor(zzfxMusic)
|
|
5821
|
+
{
|
|
5822
|
+
super(undefined);
|
|
5823
|
+
|
|
5824
|
+
if (!soundEnable || headlessMode) return;
|
|
5825
|
+
this.randomness = 0;
|
|
5826
|
+
this.sampleChannels = zzfxM(...zzfxMusic);
|
|
5827
|
+
this.sampleRate = zzfxR;
|
|
5828
|
+
}
|
|
5829
|
+
|
|
5830
|
+
/** Play the music
|
|
5831
|
+
* @param {number} [volume=1] - How much to scale volume by
|
|
5832
|
+
* @param {boolean} [loop] - True if the music should loop
|
|
5833
|
+
* @return {AudioBufferSourceNode} - The audio source node
|
|
5834
|
+
*/
|
|
5835
|
+
playMusic(volume, loop=false)
|
|
5836
|
+
{ return super.play(undefined, volume, 1, 1, loop); }
|
|
5837
|
+
}
|
|
5838
|
+
|
|
5839
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5840
|
+
// ZzFX Music Renderer v2.0.3 by Keith Clark and Frank Force
|
|
5841
|
+
|
|
5842
|
+
/** Generate samples for a ZzFM song with given parameters
|
|
5843
|
+
* @param {Array} instruments - Array of ZzFX sound parameters
|
|
5844
|
+
* @param {Array} patterns - Array of pattern data
|
|
5845
|
+
* @param {Array} sequence - Array of pattern indexes
|
|
5846
|
+
* @param {number} [BPM] - Playback speed of the song in BPM
|
|
5847
|
+
* @return {Array} - Left and right channel sample data */
|
|
5848
|
+
function zzfxM(instruments, patterns, sequence, BPM = 125)
|
|
5849
|
+
{
|
|
5850
|
+
let i, j, k;
|
|
5851
|
+
let instrumentParameters;
|
|
5852
|
+
let note;
|
|
5853
|
+
let sample;
|
|
5854
|
+
let patternChannel;
|
|
5855
|
+
let notFirstBeat;
|
|
5856
|
+
let stop;
|
|
5857
|
+
let instrument;
|
|
5858
|
+
let attenuation;
|
|
5859
|
+
let outSampleOffset;
|
|
5860
|
+
let isSequenceEnd;
|
|
5861
|
+
let sampleOffset = 0;
|
|
5862
|
+
let nextSampleOffset;
|
|
5863
|
+
let sampleBuffer = [];
|
|
5864
|
+
let leftChannelBuffer = [];
|
|
5865
|
+
let rightChannelBuffer = [];
|
|
5866
|
+
let channelIndex = 0;
|
|
5867
|
+
let panning = 0;
|
|
5868
|
+
let hasMore = 1;
|
|
5869
|
+
let sampleCache = {};
|
|
5870
|
+
let beatLength = zzfxR / BPM * 60 >> 2;
|
|
5871
|
+
|
|
5872
|
+
// for each channel in order until there are no more
|
|
5873
|
+
for (; hasMore; channelIndex++) {
|
|
5874
|
+
|
|
5875
|
+
// reset current values
|
|
5876
|
+
sampleBuffer = [hasMore = notFirstBeat = outSampleOffset = 0];
|
|
5877
|
+
|
|
5878
|
+
// for each pattern in sequence
|
|
5879
|
+
sequence.forEach((patternIndex, sequenceIndex) => {
|
|
5880
|
+
// get pattern for current channel, use empty 1 note pattern if none found
|
|
5881
|
+
patternChannel = patterns[patternIndex][channelIndex] || [0, 0, 0];
|
|
5882
|
+
|
|
5883
|
+
// check if there are more channels
|
|
5884
|
+
hasMore |= patterns[patternIndex][channelIndex]&&1;
|
|
5885
|
+
|
|
5886
|
+
// get next offset, use the length of first channel
|
|
5887
|
+
nextSampleOffset = outSampleOffset + (patterns[patternIndex][0].length - 2 - (notFirstBeat?0:1)) * beatLength;
|
|
5888
|
+
// for each beat in pattern, plus one extra if end of sequence
|
|
5889
|
+
isSequenceEnd = sequenceIndex == sequence.length - 1;
|
|
5890
|
+
for (i = 2, k = outSampleOffset; i < patternChannel.length + isSequenceEnd; notFirstBeat = ++i) {
|
|
5891
|
+
|
|
5892
|
+
// <channel-note>
|
|
5893
|
+
note = patternChannel[i];
|
|
5894
|
+
|
|
5895
|
+
// stop if end, different instrument or new note
|
|
5896
|
+
stop = i == patternChannel.length + isSequenceEnd - 1 && isSequenceEnd ||
|
|
5897
|
+
instrument != (patternChannel[0] || 0) || note | 0;
|
|
5898
|
+
|
|
5899
|
+
// fill buffer with samples for previous beat, most cpu intensive part
|
|
5900
|
+
for (j = 0; j < beatLength && notFirstBeat;
|
|
5901
|
+
|
|
5902
|
+
// fade off attenuation at end of beat if stopping note, prevents clicking
|
|
5903
|
+
j++ > beatLength - 99 && stop && attenuation < 1? attenuation += 1 / 99 : 0
|
|
5904
|
+
) {
|
|
5905
|
+
// copy sample to stereo buffers with panning
|
|
5906
|
+
sample = (1 - attenuation) * sampleBuffer[sampleOffset++] / 2 || 0;
|
|
5907
|
+
leftChannelBuffer[k] = (leftChannelBuffer[k] || 0) - sample * panning + sample;
|
|
5908
|
+
rightChannelBuffer[k] = (rightChannelBuffer[k++] || 0) + sample * panning + sample;
|
|
5909
|
+
}
|
|
5910
|
+
|
|
5911
|
+
// set up for next note
|
|
5912
|
+
if (note) {
|
|
5913
|
+
// set attenuation
|
|
5914
|
+
attenuation = note % 1;
|
|
5915
|
+
panning = patternChannel[1] || 0;
|
|
5916
|
+
if (note |= 0) {
|
|
5917
|
+
// get cached sample
|
|
5918
|
+
sampleBuffer = sampleCache[
|
|
5919
|
+
[
|
|
5920
|
+
instrument = patternChannel[sampleOffset = 0] || 0,
|
|
5921
|
+
note
|
|
5922
|
+
]
|
|
5923
|
+
] = sampleCache[[instrument, note]] || (
|
|
5924
|
+
// add sample to cache
|
|
5925
|
+
instrumentParameters = [...instruments[instrument]],
|
|
5926
|
+
instrumentParameters[2] = (instrumentParameters[2] || 220) * 2**(note / 12 - 1),
|
|
5927
|
+
|
|
5928
|
+
// allow negative values to stop notes
|
|
5929
|
+
note > 0 ? zzfxG(...instrumentParameters) : []
|
|
5930
|
+
);
|
|
5931
|
+
}
|
|
5932
|
+
}
|
|
5933
|
+
}
|
|
5934
|
+
|
|
5935
|
+
// update the sample offset
|
|
5936
|
+
outSampleOffset = nextSampleOffset;
|
|
5937
|
+
});
|
|
5938
|
+
}
|
|
5939
|
+
|
|
5940
|
+
return [leftChannelBuffer, rightChannelBuffer];
|
|
5941
|
+
}
|
|
5942
|
+
|
|
5943
|
+
/**
|
|
5944
|
+
* LittleJS User Interface Plugin
|
|
5945
|
+
* - call new UISystemPlugin() to setup the UI system
|
|
5946
|
+
* - Nested Menus
|
|
5947
|
+
* - Text
|
|
5948
|
+
* - Buttons
|
|
5949
|
+
* - Checkboxes
|
|
5950
|
+
* - Images
|
|
5951
|
+
*/
|
|
5952
|
+
|
|
5953
|
+
|
|
5954
|
+
|
|
5955
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5956
|
+
|
|
5957
|
+
/** Global UI system plugin object
|
|
5958
|
+
* @type {UISystemPlugin} */
|
|
5959
|
+
let uiSystem;
|
|
5960
|
+
|
|
5961
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
5962
|
+
/**
|
|
5963
|
+
* UI System Global Object
|
|
5964
|
+
*/
|
|
5965
|
+
class UISystemPlugin
|
|
5966
|
+
{
|
|
5967
|
+
/** Create the global UI system object
|
|
5968
|
+
* @param {CanvasRenderingContext2D} [context]
|
|
5969
|
+
* @example
|
|
5970
|
+
* // create the ui plugin object
|
|
5971
|
+
* new UISystemPlugin;
|
|
5972
|
+
*/
|
|
5973
|
+
constructor(context=overlayContext)
|
|
5974
|
+
{
|
|
5975
|
+
ASSERT(!uiSystem, 'UI system already initialized');
|
|
5976
|
+
uiSystem = this;
|
|
5977
|
+
|
|
5978
|
+
/** @property {Color} - Default fill color for UI elements */
|
|
5979
|
+
this.defaultColor = WHITE;
|
|
5980
|
+
/** @property {Color} - Default outline color for UI elements */
|
|
5981
|
+
this.defaultLineColor = BLACK;
|
|
5982
|
+
/** @property {Color} - Default text color for UI elements */
|
|
5983
|
+
this.defaultTextColor = BLACK;
|
|
5984
|
+
/** @property {Color} - Default button color for UI elements */
|
|
5985
|
+
this.defaultButtonColor = hsl(0,0,.5);
|
|
5986
|
+
/** @property {Color} - Default hover color for UI elements */
|
|
5987
|
+
this.defaultHoverColor = hsl(0,0,.7);
|
|
5988
|
+
/** @property {number} - Default line width for UI elements */
|
|
5989
|
+
this.defaultLineWidth = 4;
|
|
5990
|
+
/** @property {string} - Default font for UI elements */
|
|
5991
|
+
this.defaultFont = 'arial';
|
|
5992
|
+
/** @property {Array<UIObject>} - List of all UI elements */
|
|
5993
|
+
this.uiObjects = [];
|
|
5994
|
+
/** @property {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} - Context to render UI elements to */
|
|
5995
|
+
this.uiContext = context;
|
|
5996
|
+
|
|
5997
|
+
engineAddPlugin(uiUpdate, uiRender);
|
|
5998
|
+
|
|
5999
|
+
// setup recursive update and render
|
|
6000
|
+
function uiUpdate()
|
|
6001
|
+
{
|
|
6002
|
+
function updateObject(o)
|
|
6003
|
+
{
|
|
6004
|
+
if (!o.visible)
|
|
6005
|
+
return;
|
|
6006
|
+
if (o.parent)
|
|
6007
|
+
o.pos = o.localPos.add(o.parent.pos);
|
|
6008
|
+
o.update();
|
|
6009
|
+
for(const c of o.children)
|
|
6010
|
+
updateObject(c);
|
|
6011
|
+
}
|
|
6012
|
+
uiSystem.uiObjects.forEach(o=> o.parent || updateObject(o));
|
|
6013
|
+
}
|
|
6014
|
+
function uiRender()
|
|
6015
|
+
{
|
|
6016
|
+
function renderObject(o)
|
|
6017
|
+
{
|
|
6018
|
+
if (!o.visible)
|
|
6019
|
+
return;
|
|
6020
|
+
if (o.parent)
|
|
6021
|
+
o.pos = o.localPos.add(o.parent.pos);
|
|
6022
|
+
o.render();
|
|
6023
|
+
for(const c of o.children)
|
|
6024
|
+
renderObject(c);
|
|
6025
|
+
}
|
|
6026
|
+
uiSystem.uiObjects.forEach(o=> o.parent || renderObject(o));
|
|
6027
|
+
}
|
|
6028
|
+
}
|
|
6029
|
+
|
|
6030
|
+
/** Draw a rectangle to the UI context
|
|
6031
|
+
* @param {Vector2} pos
|
|
6032
|
+
* @param {Vector2} size
|
|
6033
|
+
* @param {Color} [color=uiSystem.defaultColor]
|
|
6034
|
+
* @param {number} [lineWidth=uiSystem.defaultLineWidth]
|
|
6035
|
+
* @param {Color} [lineColor=uiSystem.defaultLineColor] */
|
|
6036
|
+
drawRect(pos, size, color=uiSystem.defaultColor, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor)
|
|
6037
|
+
{
|
|
6038
|
+
uiSystem.uiContext.fillStyle = color.toString();
|
|
6039
|
+
uiSystem.uiContext.beginPath();
|
|
6040
|
+
uiSystem.uiContext.rect(pos.x-size.x/2, pos.y-size.y/2, size.x, size.y);
|
|
6041
|
+
uiSystem.uiContext.fill();
|
|
6042
|
+
if (lineWidth)
|
|
6043
|
+
{
|
|
6044
|
+
uiSystem.uiContext.strokeStyle = lineColor.toString();
|
|
6045
|
+
uiSystem.uiContext.lineWidth = lineWidth;
|
|
6046
|
+
uiSystem.uiContext.stroke();
|
|
6047
|
+
}
|
|
6048
|
+
}
|
|
6049
|
+
|
|
6050
|
+
/** Draw a line to the UI context
|
|
6051
|
+
* @param {Vector2} posA
|
|
6052
|
+
* @param {Vector2} posB
|
|
6053
|
+
* @param {number} [lineWidth=uiSystem.defaultLineWidth]
|
|
6054
|
+
* @param {Color} [lineColor=uiSystem.defaultLineColor] */
|
|
6055
|
+
drawLine(posA, posB, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor)
|
|
6056
|
+
{
|
|
6057
|
+
uiSystem.uiContext.strokeStyle = lineColor.toString();
|
|
6058
|
+
uiSystem.uiContext.lineWidth = lineWidth;
|
|
6059
|
+
uiSystem.uiContext.beginPath();
|
|
6060
|
+
uiSystem.uiContext.lineTo(posA.x, posA.y);
|
|
6061
|
+
uiSystem.uiContext.lineTo(posB.x, posB.y);
|
|
6062
|
+
uiSystem.uiContext.stroke();
|
|
6063
|
+
}
|
|
6064
|
+
|
|
6065
|
+
/** Draw a tile to the UI context
|
|
6066
|
+
* @param {Vector2} pos
|
|
6067
|
+
* @param {Vector2} size
|
|
6068
|
+
* @param {TileInfo} tileInfo
|
|
6069
|
+
* @param {Color} [color=uiSystem.defaultColor]
|
|
6070
|
+
* @param {number} [angle]
|
|
6071
|
+
* @param {boolean} [mirror] */
|
|
6072
|
+
drawTile(pos, size, tileInfo, color=uiSystem.defaultColor, angle=0, mirror=false)
|
|
6073
|
+
{
|
|
6074
|
+
drawTile(pos, size, tileInfo, color, angle, mirror, BLACK, false, true, uiSystem.uiContext);
|
|
6075
|
+
}
|
|
6076
|
+
|
|
6077
|
+
/** Draw text to the UI context
|
|
6078
|
+
* @param {string} text
|
|
6079
|
+
* @param {Vector2} pos
|
|
6080
|
+
* @param {Vector2} size
|
|
6081
|
+
* @param {Color} [color=uiSystem.defaultColor]
|
|
6082
|
+
* @param {number} [lineWidth=uiSystem.defaultLineWidth]
|
|
6083
|
+
* @param {Color} [lineColor=uiSystem.defaultLineColor]
|
|
6084
|
+
* @param {string} [align]
|
|
6085
|
+
* @param {string} [font=uiSystem.defaultFont] */
|
|
6086
|
+
drawText(text, pos, size, color=uiSystem.defaultColor, lineWidth=uiSystem.defaultLineWidth, lineColor=uiSystem.defaultLineColor, align='center', font=uiSystem.defaultFont)
|
|
6087
|
+
{
|
|
6088
|
+
drawTextScreen(text, pos, size.y, color, lineWidth, lineColor, align, font, size.x, uiSystem.uiContext);
|
|
6089
|
+
}
|
|
6090
|
+
}
|
|
6091
|
+
|
|
6092
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6093
|
+
/**
|
|
6094
|
+
* UI Object - Base level object for all UI elements
|
|
6095
|
+
*/
|
|
6096
|
+
class UIObject
|
|
6097
|
+
{
|
|
6098
|
+
/** Create a UIObject
|
|
6099
|
+
* @param {Vector2} [pos=(0,0)]
|
|
6100
|
+
* @param {Vector2} [size=(1,1)]
|
|
6101
|
+
*/
|
|
6102
|
+
constructor(pos=vec2(), size=vec2())
|
|
6103
|
+
{
|
|
6104
|
+
/** @property {Vector2} - Local position of the object */
|
|
6105
|
+
this.localPos = pos.copy();
|
|
6106
|
+
/** @property {Vector2} - Screen space position of the object */
|
|
6107
|
+
this.pos = pos.copy();
|
|
6108
|
+
/** @property {Vector2} - Screen space size of the object */
|
|
6109
|
+
this.size = size.copy();
|
|
6110
|
+
/** @property {Color} */
|
|
6111
|
+
this.color = uiSystem.defaultColor;
|
|
6112
|
+
/** @property {Color} */
|
|
6113
|
+
this.lineColor = uiSystem.defaultLineColor;
|
|
6114
|
+
/** @property {Color} */
|
|
6115
|
+
this.textColor = uiSystem.defaultTextColor;
|
|
6116
|
+
/** @property {Color} */
|
|
6117
|
+
this.hoverColor = uiSystem.defaultHoverColor;
|
|
6118
|
+
/** @property {number} */
|
|
6119
|
+
this.lineWidth = uiSystem.defaultLineWidth;
|
|
6120
|
+
/** @property {string} */
|
|
6121
|
+
this.font = uiSystem.defaultFont;
|
|
6122
|
+
/** @property {boolean} */
|
|
6123
|
+
this.visible = true;
|
|
6124
|
+
/** @property {Array<UIObject>} */
|
|
6125
|
+
this.children = [];
|
|
6126
|
+
/** @property {UIObject} */
|
|
6127
|
+
this.parent = undefined;
|
|
6128
|
+
uiSystem.uiObjects.push(this);
|
|
6129
|
+
}
|
|
6130
|
+
|
|
6131
|
+
/** Add a child UIObject to this object
|
|
6132
|
+
* @param {UIObject} child
|
|
6133
|
+
*/
|
|
6134
|
+
addChild(child)
|
|
6135
|
+
{
|
|
6136
|
+
ASSERT(!child.parent && !this.children.includes(child));
|
|
6137
|
+
this.children.push(child);
|
|
6138
|
+
child.parent = this;
|
|
6139
|
+
}
|
|
6140
|
+
|
|
6141
|
+
/** Remove a child UIObject from this object
|
|
6142
|
+
* @param {UIObject} child
|
|
6143
|
+
*/
|
|
6144
|
+
removeChild(child)
|
|
6145
|
+
{
|
|
6146
|
+
ASSERT(child.parent == this && this.children.includes(child));
|
|
6147
|
+
this.children.splice(this.children.indexOf(child), 1);
|
|
6148
|
+
child.parent = undefined;
|
|
6149
|
+
}
|
|
6150
|
+
|
|
6151
|
+
/** Update the object, called automatically by plugin once each frame */
|
|
6152
|
+
update()
|
|
6153
|
+
{
|
|
6154
|
+
// track mouse input
|
|
6155
|
+
const mouseWasOver = this.mouseIsOver;
|
|
6156
|
+
const mouseDown = mouseIsDown(0);
|
|
6157
|
+
if (!mouseDown || isTouchDevice)
|
|
6158
|
+
{
|
|
6159
|
+
this.mouseIsOver = isOverlapping(this.pos, this.size, mousePosScreen);
|
|
6160
|
+
if (!mouseDown && isTouchDevice)
|
|
6161
|
+
this.mouseIsOver = false;
|
|
6162
|
+
if (this.mouseIsOver && !mouseWasOver)
|
|
6163
|
+
this.onEnter();
|
|
6164
|
+
if (!this.mouseIsOver && mouseWasOver)
|
|
6165
|
+
this.onLeave();
|
|
6166
|
+
}
|
|
6167
|
+
if (mouseWasPressed(0) && this.mouseIsOver)
|
|
6168
|
+
{
|
|
6169
|
+
this.mouseIsHeld = true;
|
|
6170
|
+
this.onPress();
|
|
6171
|
+
if (isTouchDevice)
|
|
6172
|
+
this.mouseIsOver = false;
|
|
6173
|
+
}
|
|
6174
|
+
else if (this.mouseIsHeld && !mouseDown)
|
|
6175
|
+
{
|
|
6176
|
+
this.mouseIsHeld = false;
|
|
6177
|
+
this.onRelease();
|
|
6178
|
+
}
|
|
6179
|
+
}
|
|
6180
|
+
|
|
6181
|
+
/** Render the object, called automatically by plugin once each frame */
|
|
6182
|
+
render()
|
|
6183
|
+
{
|
|
6184
|
+
if (this.size.x && this.size.y)
|
|
6185
|
+
uiSystem.drawRect(this.pos, this.size, this.color, this.lineWidth, this.lineColor);
|
|
6186
|
+
}
|
|
6187
|
+
|
|
6188
|
+
/** Called when the mouse enters the object */
|
|
6189
|
+
onEnter() {}
|
|
6190
|
+
|
|
6191
|
+
/** Called when the mouse leaves the object */
|
|
6192
|
+
onLeave() {}
|
|
6193
|
+
|
|
6194
|
+
/** Called when the mouse is pressed while over the object */
|
|
6195
|
+
onPress() {}
|
|
6196
|
+
|
|
6197
|
+
/** Called when the mouse is released while over the object */
|
|
6198
|
+
onRelease() {}
|
|
6199
|
+
|
|
6200
|
+
/** Called when the state of this object changes */
|
|
6201
|
+
onChange() {}
|
|
6202
|
+
}
|
|
6203
|
+
|
|
6204
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6205
|
+
/**
|
|
6206
|
+
* UIText - A UI object that displays text
|
|
6207
|
+
* @extends UIObject
|
|
6208
|
+
*/
|
|
6209
|
+
class UIText extends UIObject
|
|
6210
|
+
{
|
|
6211
|
+
/** Create a UIText object
|
|
6212
|
+
* @param {Vector2} [pos]
|
|
6213
|
+
* @param {Vector2} [size]
|
|
6214
|
+
* @param {string} [text]
|
|
6215
|
+
* @param {string} [align]
|
|
6216
|
+
* @param {string} [font=uiSystem.defaultFont]
|
|
6217
|
+
*/
|
|
6218
|
+
constructor(pos, size, text='', align='center', font=uiSystem.defaultFont)
|
|
6219
|
+
{
|
|
6220
|
+
super(pos, size);
|
|
6221
|
+
|
|
6222
|
+
/** @property {string} */
|
|
6223
|
+
this.text = text;
|
|
6224
|
+
/** @property {string} */
|
|
6225
|
+
this.align = align;
|
|
6226
|
+
|
|
6227
|
+
this.font = font; // set font
|
|
6228
|
+
this.lineWidth = 0; // set text to not be outlined by default
|
|
6229
|
+
}
|
|
6230
|
+
render()
|
|
6231
|
+
{
|
|
6232
|
+
uiSystem.drawText(this.text, this.pos, this.size, this.textColor, this.lineWidth, this.lineColor, this.align, this.font);
|
|
6233
|
+
}
|
|
6234
|
+
}
|
|
6235
|
+
|
|
6236
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6237
|
+
/**
|
|
6238
|
+
* UITile - A UI object that displays a tile image
|
|
6239
|
+
* @extends UIObject
|
|
6240
|
+
*/
|
|
6241
|
+
class UITile extends UIObject
|
|
6242
|
+
{
|
|
6243
|
+
/** Create a UITile object
|
|
6244
|
+
* @param {Vector2} [pos]
|
|
6245
|
+
* @param {Vector2} [size]
|
|
6246
|
+
* @param {TileInfo} [tileInfo]
|
|
6247
|
+
* @param {Color} [color=WHITE]
|
|
6248
|
+
* @param {number} [angle]
|
|
6249
|
+
* @param {boolean} [mirror]
|
|
6250
|
+
*/
|
|
6251
|
+
constructor(pos, size, tileInfo, color=WHITE, angle=0, mirror=false)
|
|
6252
|
+
{
|
|
6253
|
+
super(pos, size);
|
|
6254
|
+
|
|
6255
|
+
/** @property {TileInfo} - Tile image to use */
|
|
6256
|
+
this.tileInfo = tileInfo;
|
|
6257
|
+
/** @property {number} - Angle to rotate in radians */
|
|
6258
|
+
this.angle = angle;
|
|
6259
|
+
/** @property {boolean} - Should it be mirrored? */
|
|
6260
|
+
this.mirror = mirror;
|
|
6261
|
+
this.color = color;
|
|
6262
|
+
}
|
|
6263
|
+
render()
|
|
6264
|
+
{
|
|
6265
|
+
uiSystem.drawTile(this.pos, this.size, this.tileInfo, this.color, this.angle, this.mirror);
|
|
6266
|
+
}
|
|
6267
|
+
}
|
|
6268
|
+
|
|
6269
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6270
|
+
/**
|
|
6271
|
+
* UIButton - A UI object that acts as a button
|
|
6272
|
+
* @extends UIObject
|
|
6273
|
+
*/
|
|
6274
|
+
class UIButton extends UIObject
|
|
6275
|
+
{
|
|
6276
|
+
/** Create a UIButton object
|
|
6277
|
+
* @param {Vector2} [pos]
|
|
6278
|
+
* @param {Vector2} [size]
|
|
6279
|
+
* @param {string} [text]
|
|
6280
|
+
* @param {Color} [color=uiSystem.defaultButtonColor]
|
|
6281
|
+
*/
|
|
6282
|
+
constructor(pos, size, text='', color=uiSystem.defaultButtonColor)
|
|
6283
|
+
{
|
|
6284
|
+
super(pos, size);
|
|
6285
|
+
|
|
6286
|
+
/** @property {string} */
|
|
6287
|
+
this.text = text;
|
|
6288
|
+
this.color = color;
|
|
6289
|
+
}
|
|
6290
|
+
render()
|
|
6291
|
+
{
|
|
6292
|
+
const lineColor = this.mouseIsHeld ? this.color : this.lineColor;
|
|
6293
|
+
const color = this.mouseIsOver? this.hoverColor : this.color;
|
|
6294
|
+
uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, lineColor);
|
|
6295
|
+
const textSize = vec2(this.size.x, this.size.y*.8);
|
|
6296
|
+
uiSystem.drawText(this.text, this.pos, textSize,
|
|
6297
|
+
this.textColor, 0, undefined, this.align, this.font);
|
|
6298
|
+
}
|
|
6299
|
+
}
|
|
6300
|
+
|
|
6301
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6302
|
+
/**
|
|
6303
|
+
* UICheckbox - A UI object that acts as a checkbox
|
|
6304
|
+
* @extends UIObject
|
|
6305
|
+
*/
|
|
6306
|
+
class UICheckbox extends UIObject
|
|
6307
|
+
{
|
|
6308
|
+
/** Create a UICheckbox object
|
|
6309
|
+
* @param {Vector2} [pos]
|
|
6310
|
+
* @param {Vector2} [size]
|
|
6311
|
+
* @param {boolean} [checked]
|
|
6312
|
+
*/
|
|
6313
|
+
constructor(pos, size, checked=false)
|
|
6314
|
+
{
|
|
6315
|
+
super(pos, size);
|
|
6316
|
+
|
|
6317
|
+
/** @property {boolean} */
|
|
6318
|
+
this.checked = checked;
|
|
6319
|
+
}
|
|
6320
|
+
onPress()
|
|
6321
|
+
{
|
|
6322
|
+
this.checked = !this.checked;
|
|
6323
|
+
this.onChange();
|
|
6324
|
+
}
|
|
6325
|
+
render()
|
|
6326
|
+
{
|
|
6327
|
+
const color = this.mouseIsOver? this.hoverColor : this.color;
|
|
6328
|
+
uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, this.lineColor);
|
|
6329
|
+
if (this.checked)
|
|
6330
|
+
{
|
|
6331
|
+
// draw an X if checked
|
|
6332
|
+
uiSystem.drawLine(this.pos.add(this.size.multiply(vec2(-.5,-.5))), this.pos.add(this.size.multiply(vec2(.5,.5))), this.lineWidth, this.lineColor);
|
|
6333
|
+
uiSystem.drawLine(this.pos.add(this.size.multiply(vec2(-.5,.5))), this.pos.add(this.size.multiply(vec2(.5,-.5))), this.lineWidth, this.lineColor);
|
|
6334
|
+
}
|
|
6335
|
+
}
|
|
6336
|
+
}
|
|
6337
|
+
|
|
6338
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6339
|
+
/**
|
|
6340
|
+
* UIScrollbar - A UI object that acts as a scrollbar
|
|
6341
|
+
* @extends UIObject
|
|
6342
|
+
*/
|
|
6343
|
+
class UIScrollbar extends UIObject
|
|
6344
|
+
{
|
|
6345
|
+
/** Create a UIScrollbar object
|
|
6346
|
+
* @param {Vector2} [pos]
|
|
6347
|
+
* @param {Vector2} [size]
|
|
6348
|
+
* @param {number} [value]
|
|
6349
|
+
* @param {string} [text]
|
|
6350
|
+
* @param {Color} [color=uiSystem.defaultButtonColor]
|
|
6351
|
+
* @param {Color} [handleColor=WHITE]
|
|
6352
|
+
*/
|
|
6353
|
+
constructor(pos, size, value=.5, text='', color=uiSystem.defaultButtonColor, handleColor=WHITE)
|
|
6354
|
+
{
|
|
6355
|
+
super(pos, size);
|
|
6356
|
+
|
|
6357
|
+
/** @property {number} */
|
|
6358
|
+
this.value = value;
|
|
6359
|
+
/** @property {string} */
|
|
6360
|
+
this.text = text;
|
|
6361
|
+
this.color = color;
|
|
6362
|
+
this.handleColor = handleColor;
|
|
6363
|
+
}
|
|
6364
|
+
update()
|
|
6365
|
+
{
|
|
6366
|
+
super.update();
|
|
6367
|
+
if (this.mouseIsHeld)
|
|
6368
|
+
{
|
|
6369
|
+
const handleSize = vec2(this.size.y);
|
|
6370
|
+
const handleWidth = this.size.x - handleSize.x;
|
|
6371
|
+
const p1 = this.pos.x - handleWidth/2;
|
|
6372
|
+
const p2 = this.pos.x + handleWidth/2;
|
|
6373
|
+
const oldValue = this.value;
|
|
6374
|
+
this.value = percent(mousePosScreen.x, p1, p2);
|
|
6375
|
+
this.value == oldValue || this.onChange();
|
|
6376
|
+
}
|
|
6377
|
+
}
|
|
6378
|
+
render()
|
|
6379
|
+
{
|
|
6380
|
+
const lineColor = this.mouseIsHeld ? this.color : this.lineColor;
|
|
6381
|
+
const color = this.mouseIsOver? this.hoverColor : this.color;
|
|
6382
|
+
uiSystem.drawRect(this.pos, this.size, color, this.lineWidth, lineColor);
|
|
6383
|
+
|
|
6384
|
+
const handleSize = vec2(this.size.y);
|
|
6385
|
+
const handleWidth = this.size.x - handleSize.x;
|
|
6386
|
+
const p1 = this.pos.x - handleWidth/2;
|
|
6387
|
+
const p2 = this.pos.x + handleWidth/2;
|
|
6388
|
+
const handlePos = vec2(lerp(this.value, p1, p2), this.pos.y);
|
|
6389
|
+
const barColor = this.mouseIsHeld ? this.color : this.handleColor;
|
|
6390
|
+
uiSystem.drawRect(handlePos, handleSize, barColor, this.lineWidth, this.lineColor);
|
|
6391
|
+
|
|
6392
|
+
const textSize = vec2(this.size.x, this.size.y*.8);
|
|
6393
|
+
uiSystem.drawText(this.text, this.pos, textSize,
|
|
6394
|
+
this.textColor, 0, undefined, this.align, this.font);
|
|
6395
|
+
}
|
|
6396
|
+
}
|
|
6397
|
+
|
|
6398
|
+
/**
|
|
6399
|
+
* LittleJS Box2D Physics Plugin
|
|
6400
|
+
* - Box2dObject extends EngineObject with Box2D physics
|
|
6401
|
+
* - Call box2dEngineInit() to start instead of normal engineInit()
|
|
6402
|
+
* - You will also need to include box2d.wasm.js
|
|
6403
|
+
* - Uses box2d.js super fast web assembly port of Box2D
|
|
6404
|
+
* - More info: https://github.com/kripken/box2d.js
|
|
6405
|
+
* - Fully wraps everything in Box2d
|
|
6406
|
+
* - Functions to create polygon, circle, and edge shapes
|
|
6407
|
+
* - Raycasting and querying
|
|
6408
|
+
* - Joint creation
|
|
6409
|
+
* - Contact begin and end callbacks
|
|
6410
|
+
* - Debug physics drawing
|
|
6411
|
+
*/
|
|
6412
|
+
|
|
6413
|
+
|
|
6414
|
+
|
|
6415
|
+
/** Global Box2d Plugin object
|
|
6416
|
+
* @type {Box2dPlugin} */
|
|
6417
|
+
let box2d;
|
|
6418
|
+
|
|
6419
|
+
/** Enable Box2D debug drawing
|
|
6420
|
+
* @type {boolean}
|
|
6421
|
+
* @default */
|
|
6422
|
+
let box2dDebug = false;
|
|
6423
|
+
|
|
6424
|
+
/** Enable Box2D debug drawing
|
|
6425
|
+
* @param {boolean} enable */
|
|
6426
|
+
function box2dSetDebug(enable) { box2dDebug = enable; }
|
|
6427
|
+
|
|
6428
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6429
|
+
/**
|
|
6430
|
+
* Box2D Object - extend with your own custom physics objects
|
|
6431
|
+
* - A LittleJS object with Box2D physics
|
|
6432
|
+
* - Each object has a Box2D body which can have multiple fixtures and joints
|
|
6433
|
+
* - Provides interface for Box2D body and fixture functions
|
|
6434
|
+
* @extends EngineObject
|
|
6435
|
+
*/
|
|
6436
|
+
class Box2dObject extends EngineObject
|
|
6437
|
+
{
|
|
6438
|
+
/** Create a LittleJS object with Box2d physics
|
|
6439
|
+
* @param {Vector2} [pos]
|
|
6440
|
+
* @param {Vector2} [size]
|
|
6441
|
+
* @param {TileInfo} [tileInfo]
|
|
6442
|
+
* @param {number} [angle]
|
|
6443
|
+
* @param {Color} [color]
|
|
6444
|
+
* @param {number} [bodyType]
|
|
6445
|
+
* @param {number} [renderOrder] */
|
|
6446
|
+
constructor(pos=vec2(), size, tileInfo, angle=0, color, bodyType=box2d.bodyTypeDynamic, renderOrder=0)
|
|
6447
|
+
{
|
|
6448
|
+
super(pos, size, tileInfo, angle, color, renderOrder);
|
|
6449
|
+
|
|
6450
|
+
// create physics body
|
|
6451
|
+
const bodyDef = new box2d.instance.b2BodyDef();
|
|
6452
|
+
bodyDef.set_type(bodyType);
|
|
6453
|
+
bodyDef.set_position(box2d.vec2dTo(pos));
|
|
6454
|
+
bodyDef.set_angle(-angle);
|
|
6455
|
+
this.body = box2d.world.CreateBody(bodyDef);
|
|
6456
|
+
this.body.object = this;
|
|
6457
|
+
this.outlineColor = BLACK;
|
|
6458
|
+
}
|
|
6459
|
+
|
|
6460
|
+
/** Destroy this object and it's physics body */
|
|
6461
|
+
destroy()
|
|
6462
|
+
{
|
|
6463
|
+
// destroy physics body, fixtures, and joints
|
|
6464
|
+
this.body && box2d.world.DestroyBody(this.body);
|
|
6465
|
+
this.body = 0;
|
|
6466
|
+
super.destroy();
|
|
6467
|
+
}
|
|
6468
|
+
|
|
6469
|
+
/** Copy box2d update sim data */
|
|
6470
|
+
update()
|
|
6471
|
+
{
|
|
6472
|
+
// use box2d physics update
|
|
6473
|
+
this.pos = box2d.vec2From(this.body.GetPosition());
|
|
6474
|
+
this.angle = -this.body.GetAngle();
|
|
6475
|
+
}
|
|
6476
|
+
|
|
6477
|
+
/** Render the object, uses box2d drawing if no tile info exists */
|
|
6478
|
+
render()
|
|
6479
|
+
{
|
|
6480
|
+
// use default render or draw fixtures
|
|
6481
|
+
if (this.tileInfo)
|
|
6482
|
+
super.render();
|
|
6483
|
+
else
|
|
6484
|
+
this.drawFixtures(this.color, this.outlineColor, this.lineWidth, mainContext);
|
|
6485
|
+
}
|
|
6486
|
+
|
|
6487
|
+
/** Render debug info */
|
|
6488
|
+
renderDebugInfo()
|
|
6489
|
+
{
|
|
6490
|
+
const isAsleep = !this.getIsAwake();
|
|
6491
|
+
const isStatic = this.getBodyType() == box2d.bodyTypeStatic;
|
|
6492
|
+
const color = rgb(isAsleep?1:0, isAsleep?1:0, isStatic?1:0, .5);
|
|
6493
|
+
this.drawFixtures(color);
|
|
6494
|
+
}
|
|
6495
|
+
|
|
6496
|
+
/** Draws all this object's fixtures
|
|
6497
|
+
* @param {Color} [color]
|
|
6498
|
+
* @param {Color} [outlineColor]
|
|
6499
|
+
* @param {number} [lineWidth]
|
|
6500
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
6501
|
+
drawFixtures(color=WHITE, outlineColor, lineWidth=.1, context)
|
|
6502
|
+
{
|
|
6503
|
+
this.getFixtureList().forEach(fixture=>
|
|
6504
|
+
box2d.drawFixture(fixture, this.pos, this.angle, color, outlineColor, lineWidth, context));
|
|
6505
|
+
}
|
|
6506
|
+
|
|
6507
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6508
|
+
// physics contact callbacks
|
|
6509
|
+
|
|
6510
|
+
/** Called when a contact begins
|
|
6511
|
+
* @param {Box2dObject} otherObject */
|
|
6512
|
+
beginContact(otherObject) {}
|
|
6513
|
+
|
|
6514
|
+
/** Called when a contact ends
|
|
6515
|
+
* @param {Box2dObject} otherObject */
|
|
6516
|
+
endContact(otherObject) {}
|
|
6517
|
+
|
|
6518
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6519
|
+
// physics fixtures and shapes
|
|
6520
|
+
|
|
6521
|
+
/** Add a shape fixture to the body
|
|
6522
|
+
* @param {Object} shape
|
|
6523
|
+
* @param {number} [density]
|
|
6524
|
+
* @param {number} [friction]
|
|
6525
|
+
* @param {number} [restitution]
|
|
6526
|
+
* @param {boolean} [isSensor] */
|
|
6527
|
+
addShape(shape, density=1, friction=.2, restitution=0, isSensor=false)
|
|
6528
|
+
{
|
|
6529
|
+
const fd = new box2d.instance.b2FixtureDef();
|
|
6530
|
+
fd.set_shape(shape);
|
|
6531
|
+
fd.set_density(density);
|
|
6532
|
+
fd.set_friction(friction);
|
|
6533
|
+
fd.set_restitution(restitution);
|
|
6534
|
+
fd.set_isSensor(isSensor);
|
|
6535
|
+
return this.body.CreateFixture(fd);
|
|
6536
|
+
}
|
|
6537
|
+
|
|
6538
|
+
/** Add a box shape to the body
|
|
6539
|
+
* @param {Vector2} [size]
|
|
6540
|
+
* @param {Vector2} [offset]
|
|
6541
|
+
* @param {number} [angle]
|
|
6542
|
+
* @param {number} [density]
|
|
6543
|
+
* @param {number} [friction]
|
|
6544
|
+
* @param {number} [restitution]
|
|
6545
|
+
* @param {boolean} [isSensor] */
|
|
6546
|
+
addBox(size=vec2(1), offset=vec2(), angle=0, density, friction, restitution, isSensor)
|
|
6547
|
+
{
|
|
6548
|
+
const shape = new box2d.instance.b2PolygonShape();
|
|
6549
|
+
shape.SetAsBox(size.x/2, size.y/2, box2d.vec2dTo(offset), angle);
|
|
6550
|
+
return this.addShape(shape, density, friction, restitution, isSensor);
|
|
6551
|
+
}
|
|
6552
|
+
|
|
6553
|
+
/** Add a polygon shape to the body
|
|
6554
|
+
* @param {Array<Vector2>} points
|
|
6555
|
+
* @param {number} [density]
|
|
6556
|
+
* @param {number} [friction]
|
|
6557
|
+
* @param {number} [restitution]
|
|
6558
|
+
* @param {boolean} [isSensor] */
|
|
6559
|
+
addPoly(points, density, friction, restitution, isSensor)
|
|
6560
|
+
{
|
|
6561
|
+
function box2dCreatePolygonShape(points)
|
|
6562
|
+
{
|
|
6563
|
+
function box2dCreatePointList(points)
|
|
6564
|
+
{
|
|
6565
|
+
const buffer = box2d.instance._malloc(points.length * 8);
|
|
6566
|
+
for (let i=0, offset=0; i<points.length; ++i)
|
|
6567
|
+
{
|
|
6568
|
+
box2d.instance.HEAPF32[buffer + offset >> 2] = points[i].x;
|
|
6569
|
+
offset += 4;
|
|
6570
|
+
box2d.instance.HEAPF32[buffer + offset >> 2] = points[i].y;
|
|
6571
|
+
offset += 4;
|
|
6572
|
+
}
|
|
6573
|
+
return box2d.instance.wrapPointer(buffer, box2d.instance.b2Vec2);
|
|
6574
|
+
}
|
|
6575
|
+
|
|
6576
|
+
ASSERT(3 <= points.length && points.length <= 8);
|
|
6577
|
+
const shape = new box2d.instance.b2PolygonShape();
|
|
6578
|
+
const box2dPoints = box2dCreatePointList(points);
|
|
6579
|
+
shape.Set(box2dPoints, points.length);
|
|
6580
|
+
return shape;
|
|
6581
|
+
}
|
|
6582
|
+
|
|
6583
|
+
const shape = box2dCreatePolygonShape(points);
|
|
6584
|
+
return this.addShape(shape, density, friction, restitution, isSensor);
|
|
6585
|
+
}
|
|
6586
|
+
|
|
6587
|
+
/** Add a regular polygon shape to the body
|
|
6588
|
+
* @param {number} [diameter]
|
|
6589
|
+
* @param {number} [sides]
|
|
6590
|
+
* @param {number} [density]
|
|
6591
|
+
* @param {number} [friction]
|
|
6592
|
+
* @param {number} [restitution]
|
|
6593
|
+
* @param {boolean} [isSensor] */
|
|
6594
|
+
addRegularPoly(diameter=1, sides=8, density, friction, restitution, isSensor)
|
|
6595
|
+
{
|
|
6596
|
+
const points = [];
|
|
6597
|
+
const radius = diameter/2;
|
|
6598
|
+
for (let i=sides; i--;)
|
|
6599
|
+
points.push(vec2(radius,0).rotate((i+.5)/sides*PI*2));
|
|
6600
|
+
return this.addPoly(points, density, friction, restitution, isSensor);
|
|
6601
|
+
}
|
|
6602
|
+
|
|
6603
|
+
/** Add a random polygon shape to the body
|
|
6604
|
+
* @param {number} [diameter]
|
|
6605
|
+
* @param {number} [density]
|
|
6606
|
+
* @param {number} [friction]
|
|
6607
|
+
* @param {number} [restitution]
|
|
6608
|
+
* @param {boolean} [isSensor] */
|
|
6609
|
+
addRandomPoly(diameter=1, density, friction, restitution, isSensor)
|
|
6610
|
+
{
|
|
6611
|
+
const sides = randInt(3, 9);
|
|
6612
|
+
const points = [];
|
|
6613
|
+
const radius = diameter/2;
|
|
6614
|
+
for (let i=sides; i--;)
|
|
6615
|
+
points.push(vec2(rand(radius/2,radius*1.5),0).rotate(i/sides*PI*2));
|
|
6616
|
+
return this.addPoly(points, density, friction, restitution, isSensor);
|
|
6617
|
+
}
|
|
6618
|
+
|
|
6619
|
+
/** Add a circle shape to the body
|
|
6620
|
+
* @param {number} [diameter]
|
|
6621
|
+
* @param {Vector2} [offset]
|
|
6622
|
+
* @param {number} [density]
|
|
6623
|
+
* @param {number} [friction]
|
|
6624
|
+
* @param {number} [restitution]
|
|
6625
|
+
* @param {boolean} [isSensor] */
|
|
6626
|
+
addCircle(diameter=1, offset=vec2(), density, friction, restitution, isSensor)
|
|
6627
|
+
{
|
|
6628
|
+
const shape = new box2d.instance.b2CircleShape();
|
|
6629
|
+
shape.set_m_p(box2d.vec2dTo(offset));
|
|
6630
|
+
shape.set_m_radius(diameter/2);
|
|
6631
|
+
return this.addShape(shape, density, friction, restitution, isSensor);
|
|
6632
|
+
}
|
|
6633
|
+
|
|
6634
|
+
/** Add an edge shape to the body
|
|
6635
|
+
* @param {Vector2} point1
|
|
6636
|
+
* @param {Vector2} point2
|
|
6637
|
+
* @param {number} [density]
|
|
6638
|
+
* @param {number} [friction]
|
|
6639
|
+
* @param {number} [restitution]
|
|
6640
|
+
* @param {boolean} [isSensor] */
|
|
6641
|
+
addEdge(point1, point2, density, friction, restitution, isSensor)
|
|
6642
|
+
{
|
|
6643
|
+
const shape = new box2d.instance.b2EdgeShape();
|
|
6644
|
+
shape.Set(box2d.vec2dTo(point1), box2d.vec2dTo(point2));
|
|
6645
|
+
return this.addShape(shape, density, friction, restitution, isSensor);
|
|
6646
|
+
}
|
|
6647
|
+
|
|
6648
|
+
/** Add an edge loop to the body, an edge loop connects the end points
|
|
6649
|
+
* @param {Array<Vector2>} points
|
|
6650
|
+
* @param {number} [density]
|
|
6651
|
+
* @param {number} [friction]
|
|
6652
|
+
* @param {number} [restitution]
|
|
6653
|
+
* @param {boolean} [isSensor] */
|
|
6654
|
+
addEdgeLoop(points, density, friction, restitution, isSensor)
|
|
6655
|
+
{
|
|
6656
|
+
const fixtures = [];
|
|
6657
|
+
const getPoint = i=> points[mod(i,points.length)];
|
|
6658
|
+
for (let i=0; i<points.length; ++i)
|
|
6659
|
+
{
|
|
6660
|
+
const shape = new box2d.instance.b2EdgeShape();
|
|
6661
|
+
shape.set_m_vertex0(box2d.vec2dTo(getPoint(i-1)));
|
|
6662
|
+
shape.set_m_vertex1(box2d.vec2dTo(getPoint(i+0)));
|
|
6663
|
+
shape.set_m_vertex2(box2d.vec2dTo(getPoint(i+1)));
|
|
6664
|
+
shape.set_m_vertex3(box2d.vec2dTo(getPoint(i+2)));
|
|
6665
|
+
const f = this.addShape(shape, density, friction, restitution, isSensor);
|
|
6666
|
+
fixtures.push(f);
|
|
6667
|
+
}
|
|
6668
|
+
return fixtures;
|
|
6669
|
+
}
|
|
6670
|
+
|
|
6671
|
+
/** Add an edge list to the body
|
|
6672
|
+
* @param {Array<Vector2>} points
|
|
6673
|
+
* @param {number} [density]
|
|
6674
|
+
* @param {number} [friction]
|
|
6675
|
+
* @param {number} [restitution]
|
|
6676
|
+
* @param {boolean} [isSensor] */
|
|
6677
|
+
addEdgeList(points, density, friction, restitution, isSensor)
|
|
6678
|
+
{
|
|
6679
|
+
const fixtures = [];
|
|
6680
|
+
for (let i=0; i<points.length-1; ++i)
|
|
6681
|
+
{
|
|
6682
|
+
const shape = new box2d.instance.b2EdgeShape();
|
|
6683
|
+
points[i-1] && shape.set_m_vertex0(box2d.vec2dTo(points[i-1]));
|
|
6684
|
+
points[i+0] && shape.set_m_vertex1(box2d.vec2dTo(points[i+0]));
|
|
6685
|
+
points[i+1] && shape.set_m_vertex2(box2d.vec2dTo(points[i+1]));
|
|
6686
|
+
points[i+2] && shape.set_m_vertex3(box2d.vec2dTo(points[i+2]));
|
|
6687
|
+
const f = this.addShape(shape, density, friction, restitution, isSensor);
|
|
6688
|
+
fixtures.push(f);
|
|
6689
|
+
}
|
|
6690
|
+
return fixtures;
|
|
6691
|
+
}
|
|
6692
|
+
|
|
6693
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6694
|
+
// physics get functions
|
|
6695
|
+
|
|
6696
|
+
/** Gets the center of mass
|
|
6697
|
+
* @return {Vector2} */
|
|
6698
|
+
getCenterOfMass() { return box2d.vec2From(this.body.GetWorldCenter()); }
|
|
6699
|
+
|
|
6700
|
+
/** Gets the linear velocity
|
|
6701
|
+
* @return {Vector2} */
|
|
6702
|
+
getLinearVelocity() { return box2d.vec2From(this.body.GetLinearVelocity()); }
|
|
6703
|
+
|
|
6704
|
+
/** Gets the angular velocity
|
|
6705
|
+
* @return {Vector2} */
|
|
6706
|
+
getAngularVelocity() { return this.body.GetAngularVelocity(); }
|
|
6707
|
+
|
|
6708
|
+
/** Gets the mass
|
|
6709
|
+
* @return {number} */
|
|
6710
|
+
getMass() { return this.body.GetMass(); }
|
|
6711
|
+
|
|
6712
|
+
/** Gets the rotational inertia
|
|
6713
|
+
* @return {number} */
|
|
6714
|
+
getInertia() { return this.body.GetInertia(); }
|
|
6715
|
+
|
|
6716
|
+
/** Check if this object is awake
|
|
6717
|
+
* @return {boolean} */
|
|
6718
|
+
getIsAwake() { return this.body.IsAwake(); }
|
|
6719
|
+
|
|
6720
|
+
/** Gets the physics body type
|
|
6721
|
+
* @return {number} */
|
|
6722
|
+
getBodyType() { return this.body.GetType(); }
|
|
6723
|
+
|
|
6724
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6725
|
+
// physics set functions
|
|
6726
|
+
|
|
6727
|
+
/** Sets the position and angle
|
|
6728
|
+
* @param {Vector2} pos
|
|
6729
|
+
* @param {number} angle */
|
|
6730
|
+
setTransform(pos, angle)
|
|
6731
|
+
{
|
|
6732
|
+
this.pos = pos;
|
|
6733
|
+
this.angle = angle;
|
|
6734
|
+
this.body.SetTransform(box2d.vec2dTo(pos), angle);
|
|
6735
|
+
}
|
|
6736
|
+
|
|
6737
|
+
/** Sets the position
|
|
6738
|
+
* @param {Vector2} pos */
|
|
6739
|
+
setPosition(pos) { this.setTransform(pos, this.body.GetAngle()); }
|
|
6740
|
+
|
|
6741
|
+
/** Sets the angle
|
|
6742
|
+
* @param {number} angle */
|
|
6743
|
+
setAngle(angle) { this.setTransform(box2d.vec2From(this.body.GetPosition()), -angle); }
|
|
6744
|
+
|
|
6745
|
+
/** Sets the linear velocity
|
|
6746
|
+
* @param {Vector2} velocity */
|
|
6747
|
+
setLinearVelocity(velocity) { this.body.SetLinearVelocity(box2d.vec2dTo(velocity)); }
|
|
6748
|
+
|
|
6749
|
+
/** Sets the angular velocity
|
|
6750
|
+
* @param {number} angularVelocity */
|
|
6751
|
+
setAngularVelocity(angularVelocity) { this.body.SetAngularVelocity(angularVelocity); }
|
|
6752
|
+
|
|
6753
|
+
/** Sets the linear damping
|
|
6754
|
+
* @param {number} damping */
|
|
6755
|
+
setLinearDamping(damping) { this.body.SetLinearDamping(damping); }
|
|
6756
|
+
|
|
6757
|
+
/** Sets the angular damping
|
|
6758
|
+
* @param {number} damping */
|
|
6759
|
+
setAngularDamping(damping) { this.body.SetAngularDamping(damping); }
|
|
6760
|
+
|
|
6761
|
+
/** Sets the gravity scale
|
|
6762
|
+
* @param {number} [scale] */
|
|
6763
|
+
setGravityScale(scale=1) { this.body.SetGravityScale(this.gravityScale = scale); }
|
|
6764
|
+
|
|
6765
|
+
/** Should this body be treated like a bullet for continuous collision detection?
|
|
6766
|
+
* @param {boolean} [isBullet] */
|
|
6767
|
+
setBullet(isBullet=true) { this.body.SetBullet(isBullet); }
|
|
6768
|
+
|
|
6769
|
+
/** Set the sleep state of the body
|
|
6770
|
+
* @param {boolean} [isAwake] */
|
|
6771
|
+
setAwake(isAwake=true) { this.body.SetAwake(isAwake); }
|
|
6772
|
+
|
|
6773
|
+
/** Set the physics body type
|
|
6774
|
+
* @param {number} type */
|
|
6775
|
+
setBodyType(type) { this.body.SetType(type); }
|
|
6776
|
+
|
|
6777
|
+
/** Set whether the body is allowed to sleep
|
|
6778
|
+
* @param {boolean} [isAllowed] */
|
|
6779
|
+
setSleepingAllowed(isAllowed=true) { this.body.SetSleepingAllowed(isAllowed); }
|
|
6780
|
+
|
|
6781
|
+
/** Set whether the body can rotate
|
|
6782
|
+
* @param {boolean} [isFixed] */
|
|
6783
|
+
setFixedRotation(isFixed=true) { this.body.SetFixedRotation(isFixed); }
|
|
6784
|
+
|
|
6785
|
+
/** Set the center of mass of the body
|
|
6786
|
+
* @param {Vector2} center */
|
|
6787
|
+
setCenterOfMass(center) { this.setMassData(center) }
|
|
6788
|
+
|
|
6789
|
+
/** Set the mass of the body
|
|
6790
|
+
* @param {number} mass */
|
|
6791
|
+
setMass(mass) { this.setMassData(undefined, mass) }
|
|
6792
|
+
|
|
6793
|
+
/** Set the moment of inertia of the body
|
|
6794
|
+
* @param {number} momentOfInertia */
|
|
6795
|
+
setMomentOfInertia(momentOfInertia) { this.setMassData(undefined, undefined, momentOfInertia) }
|
|
6796
|
+
|
|
6797
|
+
/** Reset the mass, center of mass, and moment */
|
|
6798
|
+
resetMassData() { this.body.ResetMassData(); }
|
|
6799
|
+
|
|
6800
|
+
/** Set the mass data of the body
|
|
6801
|
+
* @param {Vector2} [localCenter]
|
|
6802
|
+
* @param {number} [mass]
|
|
6803
|
+
* @param {number} [momentOfInertia] */
|
|
6804
|
+
setMassData(localCenter, mass, momentOfInertia)
|
|
6805
|
+
{
|
|
6806
|
+
const data = new box2d.instance.b2MassData();
|
|
6807
|
+
this.body.GetMassData(data);
|
|
6808
|
+
localCenter && data.set_center(box2d.vec2dTo(localCenter));
|
|
6809
|
+
mass && data.set_mass(mass);
|
|
6810
|
+
momentOfInertia && data.set_I(momentOfInertia);
|
|
6811
|
+
this.body.SetMassData(data);
|
|
6812
|
+
}
|
|
6813
|
+
|
|
6814
|
+
/** Set the collision filter data for this body
|
|
6815
|
+
* @param {number} [categoryBits]
|
|
6816
|
+
* @param {number} [ignoreCategoryBits]
|
|
6817
|
+
* @param {number} [groupIndex] */
|
|
6818
|
+
setFilterData(categoryBits=0, ignoreCategoryBits=0, groupIndex=0)
|
|
6819
|
+
{
|
|
6820
|
+
this.getFixtureList().forEach(fixture=>
|
|
6821
|
+
{
|
|
6822
|
+
const filter = fixture.GetFilterData();
|
|
6823
|
+
filter.set_categoryBits(categoryBits);
|
|
6824
|
+
filter.set_maskBits(0xffff & ~ignoreCategoryBits);
|
|
6825
|
+
filter.set_groupIndex(groupIndex);
|
|
6826
|
+
});
|
|
6827
|
+
}
|
|
6828
|
+
|
|
6829
|
+
/** Set if this body is a sensor
|
|
6830
|
+
* @param {boolean} [isSensor] */
|
|
6831
|
+
setSensor(isSensor=true)
|
|
6832
|
+
{ this.getFixtureList().forEach(f=>f.SetSensor(isSensor)); }
|
|
6833
|
+
|
|
6834
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6835
|
+
// physics force and torque functions
|
|
6836
|
+
|
|
6837
|
+
/** Apply force to this object
|
|
6838
|
+
* @param {Vector2} force
|
|
6839
|
+
* @param {Vector2} [pos] */
|
|
6840
|
+
applyForce(force, pos)
|
|
6841
|
+
{
|
|
6842
|
+
pos ||= this.getCenterOfMass();
|
|
6843
|
+
this.setAwake();
|
|
6844
|
+
this.body.ApplyForce(box2d.vec2dTo(force), box2d.vec2dTo(pos));
|
|
6845
|
+
}
|
|
6846
|
+
|
|
6847
|
+
/** Apply acceleration to this object
|
|
6848
|
+
* @param {Vector2} acceleration
|
|
6849
|
+
* @param {Vector2} [pos] */
|
|
6850
|
+
applyAcceleration(acceleration, pos)
|
|
6851
|
+
{
|
|
6852
|
+
pos ||= this.getCenterOfMass();
|
|
6853
|
+
this.setAwake();
|
|
6854
|
+
this.body.ApplyLinearImpulse(box2d.vec2dTo(acceleration), box2d.vec2dTo(pos));
|
|
6855
|
+
}
|
|
6856
|
+
|
|
6857
|
+
/** Apply torque to this object
|
|
6858
|
+
* @param {number} torque */
|
|
6859
|
+
applyTorque(torque)
|
|
6860
|
+
{
|
|
6861
|
+
this.setAwake();
|
|
6862
|
+
this.body.ApplyTorque(torque);
|
|
6863
|
+
}
|
|
6864
|
+
|
|
6865
|
+
/** Apply angular acceleration to this object
|
|
6866
|
+
* @param {number} acceleration */
|
|
6867
|
+
applyAngularAcceleration(acceleration)
|
|
6868
|
+
{
|
|
6869
|
+
this.setAwake();
|
|
6870
|
+
this.body.ApplyAngularImpulse(acceleration);
|
|
6871
|
+
}
|
|
6872
|
+
|
|
6873
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6874
|
+
// lists of fixtures and joints
|
|
6875
|
+
|
|
6876
|
+
/** Check if this object has any fixtures
|
|
6877
|
+
* @return {boolean} */
|
|
6878
|
+
hasFixtures() { return !box2d.isNull(this.body.GetFixtureList()); }
|
|
6879
|
+
|
|
6880
|
+
/** Get list of fixtures for this object
|
|
6881
|
+
* @return {Array<Object>} */
|
|
6882
|
+
getFixtureList()
|
|
6883
|
+
{
|
|
6884
|
+
const fixtures = [];
|
|
6885
|
+
for (let fixture=this.body.GetFixtureList(); !box2d.isNull(fixture); )
|
|
6886
|
+
{
|
|
6887
|
+
fixtures.push(fixture);
|
|
6888
|
+
fixture = fixture.GetNext();
|
|
6889
|
+
}
|
|
6890
|
+
return fixtures;
|
|
6891
|
+
}
|
|
6892
|
+
|
|
6893
|
+
/** Check if this object has any joints
|
|
6894
|
+
* @return {boolean} */
|
|
6895
|
+
hasJoints() { return !box2d.isNull(this.body.GetJointList()); }
|
|
6896
|
+
|
|
6897
|
+
/** Get list of joints for this object
|
|
6898
|
+
* @return {Array<Object>} */
|
|
6899
|
+
getJointList()
|
|
6900
|
+
{
|
|
6901
|
+
const joints = [];
|
|
6902
|
+
for (let joint=this.body.GetJointList(); !box2d.isNull(joint); )
|
|
6903
|
+
{
|
|
6904
|
+
joints.push(joint);
|
|
6905
|
+
joint = joint.get_next();
|
|
6906
|
+
}
|
|
6907
|
+
return joints;
|
|
6908
|
+
}
|
|
6909
|
+
}
|
|
6910
|
+
|
|
6911
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6912
|
+
/**
|
|
6913
|
+
* Box2D Raycast Result
|
|
6914
|
+
* - Holds results from a box2d raycast queries
|
|
6915
|
+
* - Automatically created by box2d raycast functions
|
|
6916
|
+
*/
|
|
6917
|
+
class Box2dRaycastResult
|
|
6918
|
+
{
|
|
6919
|
+
/** Create a raycast result
|
|
6920
|
+
* @param {Object} fixture
|
|
6921
|
+
* @param {Vector2} point
|
|
6922
|
+
* @param {Vector2} normal
|
|
6923
|
+
* @param {number} fraction */
|
|
6924
|
+
constructor(fixture, point, normal, fraction)
|
|
6925
|
+
{
|
|
6926
|
+
/** @property {Box2dObject} - The box2d object */
|
|
6927
|
+
this.object = fixture.GetBody().object;
|
|
6928
|
+
/** @property {Object} - The fixture that was hit */
|
|
6929
|
+
this.fixture = fixture;
|
|
6930
|
+
/** @property {Vector2} - The hit point */
|
|
6931
|
+
this.point = point;
|
|
6932
|
+
/** @property {Vector2} - The hit normal */
|
|
6933
|
+
this.normal = normal;
|
|
6934
|
+
/** @property {number} - Distance fraction at the point of intersection */
|
|
6935
|
+
this.fraction = fraction;
|
|
6936
|
+
}
|
|
6937
|
+
}
|
|
6938
|
+
|
|
6939
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6940
|
+
/**
|
|
6941
|
+
* Box2D Joint
|
|
6942
|
+
* - Base class for Box2D joints
|
|
6943
|
+
* - A joint is used to connect objects together
|
|
6944
|
+
*/
|
|
6945
|
+
class Box2dJoint
|
|
6946
|
+
{
|
|
6947
|
+
/** Create a box2d joint, the base class is not intended to be used directly
|
|
6948
|
+
* @param {Object} jointDef */
|
|
6949
|
+
constructor(jointDef)
|
|
6950
|
+
{
|
|
6951
|
+
this.box2dJoint = box2d.castObjectType(box2d.world.CreateJoint(jointDef));
|
|
6952
|
+
}
|
|
6953
|
+
|
|
6954
|
+
/** Destroy this joint */
|
|
6955
|
+
destroy() { box2d.world.DestroyJoint(this.box2dJoint); this.box2dJoint = 0; }
|
|
6956
|
+
|
|
6957
|
+
/** Get the first object attached to this joint
|
|
6958
|
+
* @return {Box2dObject} */
|
|
6959
|
+
getObjectA() { return this.box2dJoint.GetBodyA().object; }
|
|
6960
|
+
|
|
6961
|
+
/** Get the second object attached to this joint
|
|
6962
|
+
* @return {Box2dObject} */
|
|
6963
|
+
getObjectB() { return this.box2dJoint.GetBodyB().object; }
|
|
6964
|
+
|
|
6965
|
+
/** Get the first anchor for this joint in world coordinates
|
|
6966
|
+
* @return {Vector2} */
|
|
6967
|
+
getAnchorA() { return box2d.vec2From(this.box2dJoint.GetAnchorA());}
|
|
6968
|
+
|
|
6969
|
+
/** Get the second anchor for this joint in world coordinates
|
|
6970
|
+
* @return {Vector2} */
|
|
6971
|
+
getAnchorB() { return box2d.vec2From(this.box2dJoint.GetAnchorB());}
|
|
6972
|
+
|
|
6973
|
+
/** Get the reaction force on bodyB at the joint anchor given a time step
|
|
6974
|
+
* @param {number} time
|
|
6975
|
+
* @return {Vector2} */
|
|
6976
|
+
getReactionForce(time) { return box2d.vec2From(this.box2dJoint.GetReactionForce(1/time));}
|
|
6977
|
+
|
|
6978
|
+
/** Get the reaction torque on bodyB in N*m given a time step
|
|
6979
|
+
* @param {number} time
|
|
6980
|
+
* @return {number} */
|
|
6981
|
+
getReactionTorque(time) { return this.box2dJoint.GetReactionTorque(1/time);}
|
|
6982
|
+
|
|
6983
|
+
/** Check if the connected bodies should collide
|
|
6984
|
+
* @return {boolean} */
|
|
6985
|
+
getCollideConnected() { return this.box2dJoint.getCollideConnected();}
|
|
6986
|
+
|
|
6987
|
+
/** Check if either connected body is active
|
|
6988
|
+
* @return {boolean} */
|
|
6989
|
+
isActive() { return this.box2dJoint.IsActive();}
|
|
6990
|
+
}
|
|
6991
|
+
|
|
6992
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6993
|
+
/**
|
|
6994
|
+
* Box2D Target Joint, also known as a mouse joint
|
|
6995
|
+
* - Used to make a point on a object track a specific world point target
|
|
6996
|
+
* - This a soft constraint with a max force
|
|
6997
|
+
* - This allows the constraint to stretch and without applying huge forces
|
|
6998
|
+
* @extends Box2dJoint
|
|
6999
|
+
*/
|
|
7000
|
+
class Box2dTargetJoint extends Box2dJoint
|
|
7001
|
+
{
|
|
7002
|
+
/** Create a target joint
|
|
7003
|
+
* @param {Box2dObject} object
|
|
7004
|
+
* @param {Box2dObject} fixedObject
|
|
7005
|
+
* @param {Vector2} worldPos */
|
|
7006
|
+
constructor(object, fixedObject, worldPos)
|
|
7007
|
+
{
|
|
7008
|
+
object.setAwake();
|
|
7009
|
+
const jointDef = new box2d.instance.b2MouseJointDef();
|
|
7010
|
+
jointDef.set_bodyA(fixedObject.body);
|
|
7011
|
+
jointDef.set_bodyB(object.body);
|
|
7012
|
+
jointDef.set_target(box2d.vec2dTo(worldPos));
|
|
7013
|
+
jointDef.set_maxForce(2e3 * object.getMass());
|
|
7014
|
+
super(jointDef);
|
|
7015
|
+
}
|
|
7016
|
+
|
|
7017
|
+
/** Set the target point in world coordinates
|
|
7018
|
+
* @param {Vector2} pos */
|
|
7019
|
+
setTarget(pos) { this.box2dJoint.SetTarget(box2d.vec2dTo(pos)); }
|
|
7020
|
+
|
|
7021
|
+
/** Get the target point in world coordinates
|
|
7022
|
+
* @return {Vector2} */
|
|
7023
|
+
getTarget(){ return box2d.vec2From(this.box2dJoint.GetTarget()); }
|
|
7024
|
+
|
|
7025
|
+
/** Sets the maximum force in Newtons
|
|
7026
|
+
* @param {number} force */
|
|
7027
|
+
setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
|
|
7028
|
+
|
|
7029
|
+
/** Gets the maximum force in Newtons
|
|
7030
|
+
* @return {number} */
|
|
7031
|
+
getMaxForce() { return this.box2dJoint.GetMaxForce(); }
|
|
7032
|
+
|
|
7033
|
+
/** Sets the joint frequency in Hertz
|
|
7034
|
+
* @param {number} hz */
|
|
7035
|
+
setFrequency(hz) { this.box2dJoint.SetFrequency(hz); }
|
|
7036
|
+
|
|
7037
|
+
/** Gets the joint frequency in Hertz
|
|
7038
|
+
* @return {number} */
|
|
7039
|
+
getFrequency() { return this.box2dJoint.GetFrequency(); }
|
|
7040
|
+
}
|
|
7041
|
+
|
|
7042
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7043
|
+
/**
|
|
7044
|
+
* Box2D Distance Joint
|
|
7045
|
+
* - Constrains two points on two objects to remain at a fixed distance
|
|
7046
|
+
* - You can view this as a massless, rigid rod
|
|
7047
|
+
* @extends Box2dJoint
|
|
7048
|
+
*/
|
|
7049
|
+
class Box2dDistanceJoint extends Box2dJoint
|
|
7050
|
+
{
|
|
7051
|
+
/** Create a distance joint
|
|
7052
|
+
* @param {Box2dObject} objectA
|
|
7053
|
+
* @param {Box2dObject} objectB
|
|
7054
|
+
* @param {Vector2} anchorA
|
|
7055
|
+
* @param {Vector2} anchorB
|
|
7056
|
+
* @param {boolean} [collide] */
|
|
7057
|
+
constructor(objectA, objectB, anchorA, anchorB, collide=false)
|
|
7058
|
+
{
|
|
7059
|
+
anchorA ||= box2d.vec2From(objectA.body.GetPosition());
|
|
7060
|
+
anchorB ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7061
|
+
const localAnchorA = objectA.worldToLocal(anchorA);
|
|
7062
|
+
const localAnchorB = objectB.worldToLocal(anchorB);
|
|
7063
|
+
const jointDef = new box2d.instance.b2DistanceJointDef();
|
|
7064
|
+
jointDef.set_bodyA(objectA.body);
|
|
7065
|
+
jointDef.set_bodyB(objectB.body);
|
|
7066
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7067
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7068
|
+
jointDef.set_length(anchorA.distance(anchorB));
|
|
7069
|
+
jointDef.set_collideConnected(collide);
|
|
7070
|
+
super(jointDef);
|
|
7071
|
+
}
|
|
7072
|
+
|
|
7073
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7074
|
+
* @return {Vector2} */
|
|
7075
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7076
|
+
|
|
7077
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7078
|
+
* @return {Vector2} */
|
|
7079
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7080
|
+
|
|
7081
|
+
/** Set the length of the joint
|
|
7082
|
+
* @param {number} length */
|
|
7083
|
+
setLength(length) { this.box2dJoint.SetLength(length); }
|
|
7084
|
+
|
|
7085
|
+
/** Get the length of the joint
|
|
7086
|
+
* @return {number} */
|
|
7087
|
+
getLength() { return this.box2dJoint.GetLength(); }
|
|
7088
|
+
|
|
7089
|
+
/** Set the frequency in Hertz
|
|
7090
|
+
* @param {number} hz */
|
|
7091
|
+
setFrequency(hz) { this.box2dJoint.SetFrequency(hz); }
|
|
7092
|
+
|
|
7093
|
+
/** Get the frequency in Hertz
|
|
7094
|
+
* @return {number} */
|
|
7095
|
+
getFrequency() { return this.box2dJoint.GetFrequency(); }
|
|
7096
|
+
|
|
7097
|
+
/** Set the damping ratio
|
|
7098
|
+
* @param {number} ratio */
|
|
7099
|
+
setDampingRatio(ratio) { this.box2dJoint.SetDampingRatio(ratio); }
|
|
7100
|
+
|
|
7101
|
+
/** Get the damping ratio
|
|
7102
|
+
* @return {number} */
|
|
7103
|
+
getDampingRatio() { return this.box2dJoint.GetDampingRatio(); }
|
|
7104
|
+
}
|
|
7105
|
+
|
|
7106
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7107
|
+
/**
|
|
7108
|
+
* Box2D Pin Joint
|
|
7109
|
+
* - Pins two objects together at a point
|
|
7110
|
+
* @extends Box2dDistanceJoint
|
|
7111
|
+
*/
|
|
7112
|
+
class Box2dPinJoint extends Box2dDistanceJoint
|
|
7113
|
+
{
|
|
7114
|
+
/** Create a pin joint
|
|
7115
|
+
* @param {Box2dObject} objectA
|
|
7116
|
+
* @param {Box2dObject} objectB
|
|
7117
|
+
* @param {Vector2} [pos]
|
|
7118
|
+
* @param {boolean} [collide] */
|
|
7119
|
+
constructor(objectA, objectB, pos=objectA.pos, collide=false)
|
|
7120
|
+
{
|
|
7121
|
+
super(objectA, objectB, undefined, pos, collide);
|
|
7122
|
+
}
|
|
7123
|
+
}
|
|
7124
|
+
|
|
7125
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7126
|
+
/**
|
|
7127
|
+
* Box2D Rope Joint
|
|
7128
|
+
* - Enforces a maximum distance between two points on two objects
|
|
7129
|
+
* @extends Box2dJoint
|
|
7130
|
+
*/
|
|
7131
|
+
class Box2dRopeJoint extends Box2dJoint
|
|
7132
|
+
{
|
|
7133
|
+
/** Create a rope joint
|
|
7134
|
+
* @param {Box2dObject} objectA
|
|
7135
|
+
* @param {Box2dObject} objectB
|
|
7136
|
+
* @param {Vector2} anchorA
|
|
7137
|
+
* @param {Vector2} anchorB
|
|
7138
|
+
* @param {number} extraLength
|
|
7139
|
+
* @param {boolean} [collide] */
|
|
7140
|
+
constructor(objectA, objectB, anchorA, anchorB, extraLength=0, collide=false)
|
|
7141
|
+
{
|
|
7142
|
+
anchorA ||= box2d.vec2From(objectA.body.GetPosition());
|
|
7143
|
+
anchorB ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7144
|
+
const localAnchorA = objectA.worldToLocal(anchorA);
|
|
7145
|
+
const localAnchorB = objectB.worldToLocal(anchorB);
|
|
7146
|
+
const jointDef = new box2d.instance.b2RopeJointDef();
|
|
7147
|
+
jointDef.set_bodyA(objectA.body);
|
|
7148
|
+
jointDef.set_bodyB(objectB.body);
|
|
7149
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7150
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7151
|
+
jointDef.set_maxLength(anchorA.distance(anchorB)+extraLength);
|
|
7152
|
+
jointDef.set_collideConnected(collide);
|
|
7153
|
+
super(jointDef);
|
|
7154
|
+
}
|
|
7155
|
+
|
|
7156
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7157
|
+
* @return {Vector2} */
|
|
7158
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7159
|
+
|
|
7160
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7161
|
+
* @return {Vector2} */
|
|
7162
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7163
|
+
|
|
7164
|
+
/** Set the max length of the joint
|
|
7165
|
+
* @param {number} length */
|
|
7166
|
+
setMaxLength(length) { this.box2dJoint.SetMaxLength(length); }
|
|
7167
|
+
|
|
7168
|
+
/** Get the max length of the joint
|
|
7169
|
+
* @return {number} */
|
|
7170
|
+
getMaxLength() { return this.box2dJoint.GetMaxLength(); }
|
|
7171
|
+
}
|
|
7172
|
+
|
|
7173
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7174
|
+
/**
|
|
7175
|
+
* Box2D Revolute Joint
|
|
7176
|
+
* - Constrains two objects to share a point while they are free to rotate around the point
|
|
7177
|
+
* - The relative rotation about the shared point is the joint angle
|
|
7178
|
+
* - You can limit the relative rotation with a joint limit
|
|
7179
|
+
* - You can use a motor to drive the relative rotation about the shared point
|
|
7180
|
+
* - A maximum motor torque is provided so that infinite forces are not generated
|
|
7181
|
+
* @extends Box2dJoint
|
|
7182
|
+
*/
|
|
7183
|
+
class Box2dRevoluteJoint extends Box2dJoint
|
|
7184
|
+
{
|
|
7185
|
+
/** Create a revolute joint
|
|
7186
|
+
* @param {Box2dObject} objectA
|
|
7187
|
+
* @param {Box2dObject} objectB
|
|
7188
|
+
* @param {Vector2} anchor
|
|
7189
|
+
* @param {boolean} [collide] */
|
|
7190
|
+
constructor(objectA, objectB, anchor, collide=false)
|
|
7191
|
+
{
|
|
7192
|
+
anchor ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7193
|
+
const localAnchorA = objectA.worldToLocal(anchor);
|
|
7194
|
+
const localAnchorB = objectB.worldToLocal(anchor);
|
|
7195
|
+
const jointDef = new box2d.instance.b2RevoluteJointDef();
|
|
7196
|
+
jointDef.set_bodyA(objectA.body);
|
|
7197
|
+
jointDef.set_bodyB(objectB.body);
|
|
7198
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7199
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7200
|
+
jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
|
|
7201
|
+
jointDef.set_collideConnected(collide);
|
|
7202
|
+
super(jointDef);
|
|
7203
|
+
}
|
|
7204
|
+
|
|
7205
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7206
|
+
* @return {Vector2} */
|
|
7207
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7208
|
+
|
|
7209
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7210
|
+
* @return {Vector2} */
|
|
7211
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7212
|
+
|
|
7213
|
+
/** Get the reference angle, objectB angle minus objectA angle in the reference state
|
|
7214
|
+
* @return {number} */
|
|
7215
|
+
getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
|
|
7216
|
+
|
|
7217
|
+
/** Get the current joint angle
|
|
7218
|
+
* @return {number} */
|
|
7219
|
+
getJointAngle() { return this.box2dJoint.GetJointAngle(); }
|
|
7220
|
+
|
|
7221
|
+
/** Get the current joint angle speed in radians per second
|
|
7222
|
+
* @return {number} */
|
|
7223
|
+
getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
|
|
7224
|
+
|
|
7225
|
+
/** Is the joint limit enabled?
|
|
7226
|
+
* @return {boolean} */
|
|
7227
|
+
isLimitEnabled() { return this.box2dJoint.IsLimitEnabled(); }
|
|
7228
|
+
|
|
7229
|
+
/** Enable/disable the joint limit
|
|
7230
|
+
* @param {boolean} [enable] */
|
|
7231
|
+
enableLimit(enable=true) { return this.box2dJoint.enableLimit(enable); }
|
|
7232
|
+
|
|
7233
|
+
/** Get the lower joint limit
|
|
7234
|
+
* @return {number} */
|
|
7235
|
+
getLowerLimit() { return this.box2dJoint.GetLowerLimit(); }
|
|
7236
|
+
|
|
7237
|
+
/** Get the upper joint limit
|
|
7238
|
+
* @return {number} */
|
|
7239
|
+
getUpperLimit() { return this.box2dJoint.GetUpperLimit(); }
|
|
7240
|
+
|
|
7241
|
+
/** Set the joint limits
|
|
7242
|
+
* @param {number} min
|
|
7243
|
+
* @param {number} max */
|
|
7244
|
+
setLimits(min, max) { return this.box2dJoint.SetLimits(min, max); }
|
|
7245
|
+
|
|
7246
|
+
/** Is the joint motor enabled?
|
|
7247
|
+
* @return {boolean} */
|
|
7248
|
+
isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
|
|
7249
|
+
|
|
7250
|
+
/** Enable/disable the joint motor
|
|
7251
|
+
* @param {boolean} [enable] */
|
|
7252
|
+
enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
|
|
7253
|
+
|
|
7254
|
+
/** Set the motor speed
|
|
7255
|
+
* @param {number} speed */
|
|
7256
|
+
setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
|
|
7257
|
+
|
|
7258
|
+
/** Get the motor speed
|
|
7259
|
+
* @return {number} */
|
|
7260
|
+
getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
|
|
7261
|
+
|
|
7262
|
+
/** Set the motor torque
|
|
7263
|
+
* @param {number} torque */
|
|
7264
|
+
setMaxMotorTorque(torque) { return this.box2dJoint.SetMaxMotorTorque(torque); }
|
|
7265
|
+
|
|
7266
|
+
/** Get the max motor torque
|
|
7267
|
+
* @return {number} */
|
|
7268
|
+
getMaxMotorTorque() { return this.box2dJoint.GetMaxMotorTorque(); }
|
|
7269
|
+
|
|
7270
|
+
/** Get the motor torque given a time step
|
|
7271
|
+
* @param {number} time
|
|
7272
|
+
* @return {number} */
|
|
7273
|
+
getMotorTorque(time) { return this.box2dJoint.GetMotorTorque(1/time); }
|
|
7274
|
+
}
|
|
7275
|
+
|
|
7276
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7277
|
+
/**
|
|
7278
|
+
* Box2D Gear Joint
|
|
7279
|
+
* - A gear joint is used to connect two joints together
|
|
7280
|
+
* - Either joint can be a revolute or prismatic joint
|
|
7281
|
+
* - You specify a gear ratio to bind the motions together
|
|
7282
|
+
* @extends Box2dJoint
|
|
7283
|
+
*/
|
|
7284
|
+
class Box2dGearJoint extends Box2dJoint
|
|
7285
|
+
{
|
|
7286
|
+
/** Create a gear joint
|
|
7287
|
+
* @param {Box2dObject} objectA
|
|
7288
|
+
* @param {Box2dObject} objectB
|
|
7289
|
+
* @param {Box2dJoint} joint1
|
|
7290
|
+
* @param {Box2dJoint} joint2
|
|
7291
|
+
* @param {ratio} [ratio] */
|
|
7292
|
+
constructor(objectA, objectB, joint1, joint2, ratio=1)
|
|
7293
|
+
{
|
|
7294
|
+
const jointDef = new box2d.instance.b2GearJointDef();
|
|
7295
|
+
jointDef.set_bodyA(objectA.body);
|
|
7296
|
+
jointDef.set_bodyB(objectB.body);
|
|
7297
|
+
jointDef.set_joint1(joint1.box2dJoint);
|
|
7298
|
+
jointDef.set_joint2(joint2.box2dJoint);
|
|
7299
|
+
jointDef.set_ratio(ratio);
|
|
7300
|
+
super(jointDef);
|
|
7301
|
+
|
|
7302
|
+
this.joint1 = joint1;
|
|
7303
|
+
this.joint2 = joint2;
|
|
7304
|
+
}
|
|
7305
|
+
|
|
7306
|
+
/** Get the first joint
|
|
7307
|
+
* @return {Box2dJoint} */
|
|
7308
|
+
getJoint1() { return this.joint1; }
|
|
7309
|
+
|
|
7310
|
+
/** Get the second joint
|
|
7311
|
+
* @return {Box2dJoint} */
|
|
7312
|
+
getJoint2() { return this.joint2; }
|
|
7313
|
+
|
|
7314
|
+
/** Set the gear ratio
|
|
7315
|
+
* @param {number} ratio */
|
|
7316
|
+
setRatio(ratio) { return this.box2dJoint.SetRatio(ratio); }
|
|
7317
|
+
|
|
7318
|
+
/** Get the gear ratio
|
|
7319
|
+
* @return {number} */
|
|
7320
|
+
getRatio() { return this.box2dJoint.GetRatio(); }
|
|
7321
|
+
}
|
|
7322
|
+
|
|
7323
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7324
|
+
/**
|
|
7325
|
+
* Box2D Prismatic Joint
|
|
7326
|
+
* - Provides one degree of freedom: translation along an axis fixed in objectA
|
|
7327
|
+
* - Relative rotation is prevented
|
|
7328
|
+
* - You can use a joint limit to restrict the range of motion
|
|
7329
|
+
* - You can use a joint motor to drive the motion or to model joint friction
|
|
7330
|
+
* @extends Box2dJoint
|
|
7331
|
+
*/
|
|
7332
|
+
class Box2dPrismaticJoint extends Box2dJoint
|
|
7333
|
+
{
|
|
7334
|
+
/** Create a prismatic joint
|
|
7335
|
+
* @param {Box2dObject} objectA
|
|
7336
|
+
* @param {Box2dObject} objectB
|
|
7337
|
+
* @param {Vector2} anchor
|
|
7338
|
+
* @param {Vector2} worldAxis
|
|
7339
|
+
* @param {boolean} [collide] */
|
|
7340
|
+
constructor(objectA, objectB, anchor, worldAxis=vec2(0,1), collide=false)
|
|
7341
|
+
{
|
|
7342
|
+
anchor ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7343
|
+
const localAnchorA = objectA.worldToLocal(anchor);
|
|
7344
|
+
const localAnchorB = objectB.worldToLocal(anchor);
|
|
7345
|
+
const localAxisA = objectB.worldToLocalVector(worldAxis);
|
|
7346
|
+
const jointDef = new box2d.instance.b2PrismaticJointDef();
|
|
7347
|
+
jointDef.set_bodyA(objectA.body);
|
|
7348
|
+
jointDef.set_bodyB(objectB.body);
|
|
7349
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7350
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7351
|
+
jointDef.set_localAxisA(box2d.vec2dTo(localAxisA));
|
|
7352
|
+
jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
|
|
7353
|
+
jointDef.set_collideConnected(collide);
|
|
7354
|
+
super(jointDef);
|
|
7355
|
+
}
|
|
7356
|
+
|
|
7357
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7358
|
+
* @return {Vector2} */
|
|
7359
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7360
|
+
|
|
7361
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7362
|
+
* @return {Vector2} */
|
|
7363
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7364
|
+
|
|
7365
|
+
/** Get the local joint axis relative to bodyA
|
|
7366
|
+
* @return {Vector2} */
|
|
7367
|
+
getLocalAxisA() { return box2d.vec2From(this.box2dJoint.GetLocalAxisA()); }
|
|
7368
|
+
|
|
7369
|
+
/** Get the reference angle
|
|
7370
|
+
* @return {number} */
|
|
7371
|
+
getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
|
|
7372
|
+
|
|
7373
|
+
/** Get the current joint translation
|
|
7374
|
+
* @return {number} */
|
|
7375
|
+
getJointTranslation() { return this.box2dJoint.GetJointTranslation(); }
|
|
7376
|
+
|
|
7377
|
+
/** Get the current joint translation speed
|
|
7378
|
+
* @return {number} */
|
|
7379
|
+
getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
|
|
7380
|
+
|
|
7381
|
+
/** Is the joint limit enabled?
|
|
7382
|
+
* @return {boolean} */
|
|
7383
|
+
isLimitEnabled() { return this.box2dJoint.IsLimitEnabled(); }
|
|
7384
|
+
|
|
7385
|
+
/** Enable/disable the joint limit
|
|
7386
|
+
* @param {boolean} [enable] */
|
|
7387
|
+
enableLimit(enable=true) { return this.box2dJoint.enableLimit(enable); }
|
|
7388
|
+
|
|
7389
|
+
/** Get the lower joint limit
|
|
7390
|
+
* @return {number} */
|
|
7391
|
+
getLowerLimit() { return this.box2dJoint.GetLowerLimit(); }
|
|
7392
|
+
|
|
7393
|
+
/** Get the upper joint limit
|
|
7394
|
+
* @return {number} */
|
|
7395
|
+
getUpperLimit() { return this.box2dJoint.GetUpperLimit(); }
|
|
7396
|
+
|
|
7397
|
+
/** Set the joint limits
|
|
7398
|
+
* @param {number} min
|
|
7399
|
+
* @param {number} max */
|
|
7400
|
+
setLimits(min, max) { return this.box2dJoint.SetLimits(min, max); }
|
|
7401
|
+
|
|
7402
|
+
/** Is the motor enabled?
|
|
7403
|
+
* @return {boolean} */
|
|
7404
|
+
isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
|
|
7405
|
+
|
|
7406
|
+
/** Enable/disable the joint motor
|
|
7407
|
+
* @param {boolean} [enable] */
|
|
7408
|
+
enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
|
|
7409
|
+
|
|
7410
|
+
/** Set the motor speed
|
|
7411
|
+
* @param {number} speed */
|
|
7412
|
+
setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
|
|
7413
|
+
|
|
7414
|
+
/** Get the motor speed
|
|
7415
|
+
* @return {number} */
|
|
7416
|
+
getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
|
|
7417
|
+
|
|
7418
|
+
/** Set the maximum motor force
|
|
7419
|
+
* @param {number} force */
|
|
7420
|
+
setMaxMotorForce(force) { return this.box2dJoint.SetMaxMotorForce(force); }
|
|
7421
|
+
|
|
7422
|
+
/** Get the maximum motor force
|
|
7423
|
+
* @return {number} */
|
|
7424
|
+
getMaxMotorForce() { return this.box2dJoint.GetMaxMotorForce(); }
|
|
7425
|
+
|
|
7426
|
+
/** Get the motor force given a time step
|
|
7427
|
+
* @param {number} time
|
|
7428
|
+
* @return {number} */
|
|
7429
|
+
getMotorForce(time) { return this.box2dJoint.GetMotorForce(1/time); }
|
|
7430
|
+
}
|
|
7431
|
+
|
|
7432
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7433
|
+
/**
|
|
7434
|
+
* Box2D Wheel Joint
|
|
7435
|
+
* - Provides two degrees of freedom: translation along an axis fixed in objectA and rotation
|
|
7436
|
+
* - You can use a joint limit to restrict the range of motion
|
|
7437
|
+
* - You can use a joint motor to drive the motion or to model joint friction
|
|
7438
|
+
* - This joint is designed for vehicle suspensions
|
|
7439
|
+
* @extends Box2dJoint
|
|
7440
|
+
*/
|
|
7441
|
+
class Box2dWheelJoint extends Box2dJoint
|
|
7442
|
+
{
|
|
7443
|
+
/** Create a wheel joint
|
|
7444
|
+
* @param {Box2dObject} objectA
|
|
7445
|
+
* @param {Box2dObject} objectB
|
|
7446
|
+
* @param {Vector2} anchor
|
|
7447
|
+
* @param {Vector2} worldAxis
|
|
7448
|
+
* @param {boolean} [collide] */
|
|
7449
|
+
constructor(objectA, objectB, anchor, worldAxis=vec2(0,1), collide=false)
|
|
7450
|
+
{
|
|
7451
|
+
anchor ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7452
|
+
const localAnchorA = objectA.worldToLocal(anchor);
|
|
7453
|
+
const localAnchorB = objectB.worldToLocal(anchor);
|
|
7454
|
+
const localAxisA = objectB.worldToLocalVector(worldAxis);
|
|
7455
|
+
const jointDef = new box2d.instance.b2WheelJointDef();
|
|
7456
|
+
jointDef.set_bodyA(objectA.body);
|
|
7457
|
+
jointDef.set_bodyB(objectB.body);
|
|
7458
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7459
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7460
|
+
jointDef.set_localAxisA(box2d.vec2dTo(localAxisA));
|
|
7461
|
+
jointDef.set_collideConnected(collide);
|
|
7462
|
+
super(jointDef);
|
|
7463
|
+
}
|
|
7464
|
+
|
|
7465
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7466
|
+
* @return {Vector2} */
|
|
7467
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7468
|
+
|
|
7469
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7470
|
+
* @return {Vector2} */
|
|
7471
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7472
|
+
|
|
7473
|
+
/** Get the local joint axis relative to bodyA
|
|
7474
|
+
* @return {Vector2} */
|
|
7475
|
+
getLocalAxisA() { return box2d.vec2From(this.box2dJoint.GetLocalAxisA()); }
|
|
7476
|
+
|
|
7477
|
+
/** Get the current joint translation
|
|
7478
|
+
* @return {number} */
|
|
7479
|
+
getJointTranslation() { return this.box2dJoint.GetJointTranslation(); }
|
|
7480
|
+
|
|
7481
|
+
/** Get the current joint translation speed
|
|
7482
|
+
* @return {number} */
|
|
7483
|
+
getJointSpeed() { return this.box2dJoint.GetJointSpeed(); }
|
|
7484
|
+
|
|
7485
|
+
/** Is the joint motor enabled?
|
|
7486
|
+
* @return {boolean} */
|
|
7487
|
+
isMotorEnabled() { return this.box2dJoint.IsMotorEnabled(); }
|
|
7488
|
+
|
|
7489
|
+
/** Enable/disable the joint motor
|
|
7490
|
+
* @param {boolean} [enable] */
|
|
7491
|
+
enableMotor(enable=true) { return this.box2dJoint.EnableMotor(enable); }
|
|
7492
|
+
|
|
7493
|
+
/** Set the motor speed
|
|
7494
|
+
* @param {number} speed */
|
|
7495
|
+
setMotorSpeed(speed) { return this.box2dJoint.SetMotorSpeed(speed); }
|
|
7496
|
+
|
|
7497
|
+
/** Get the motor speed
|
|
7498
|
+
* @return {number} */
|
|
7499
|
+
getMotorSpeed() { return this.box2dJoint.GetMotorSpeed(); }
|
|
7500
|
+
|
|
7501
|
+
/** Set the maximum motor torque
|
|
7502
|
+
* @param {number} torque */
|
|
7503
|
+
setMaxMotorTorque(torque) { return this.box2dJoint.SetMaxMotorTorque(torque); }
|
|
7504
|
+
|
|
7505
|
+
/** Get the max motor torque
|
|
7506
|
+
* @return {number} */
|
|
7507
|
+
getMaxMotorTorque() { return this.box2dJoint.GetMaxMotorTorque(); }
|
|
7508
|
+
|
|
7509
|
+
/** Get the motor torque for a time step
|
|
7510
|
+
* @return {number} */
|
|
7511
|
+
getMotorTorque(time) { return this.box2dJoint.GetMotorTorque(1/time); }
|
|
7512
|
+
|
|
7513
|
+
/** Set the spring frequency in Hertz
|
|
7514
|
+
* @param {number} hz */
|
|
7515
|
+
setSpringFrequencyHz(hz) { return this.box2dJoint.SetSpringFrequencyHz(hz); }
|
|
7516
|
+
|
|
7517
|
+
/** Get the spring frequency in Hertz
|
|
7518
|
+
* @return {number} */
|
|
7519
|
+
getSpringFrequencyHz() { return this.box2dJoint.GetSpringFrequencyHz(); }
|
|
7520
|
+
|
|
7521
|
+
/** Set the spring damping ratio
|
|
7522
|
+
* @param {number} ratio */
|
|
7523
|
+
setSpringDampingRatio(ratio) { return this.box2dJoint.SetSpringDampingRatio(ratio); }
|
|
7524
|
+
|
|
7525
|
+
/** Get the spring damping ratio
|
|
7526
|
+
* @return {number} */
|
|
7527
|
+
getSpringDampingRatio() { return this.box2dJoint.GetSpringDampingRatio(); }
|
|
7528
|
+
}
|
|
7529
|
+
|
|
7530
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7531
|
+
/**
|
|
7532
|
+
* Box2D Weld Joint
|
|
7533
|
+
* - Glues two objects together
|
|
7534
|
+
* @extends Box2dJoint
|
|
7535
|
+
*/
|
|
7536
|
+
class Box2dWeldJoint extends Box2dJoint
|
|
7537
|
+
{
|
|
7538
|
+
/** Create a weld joint
|
|
7539
|
+
* @param {Box2dObject} objectA
|
|
7540
|
+
* @param {Box2dObject} objectB
|
|
7541
|
+
* @param {Vector2} anchor
|
|
7542
|
+
* @param {boolean} [collide] */
|
|
7543
|
+
constructor(objectA, objectB, anchor, collide=false)
|
|
7544
|
+
{
|
|
7545
|
+
anchor ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7546
|
+
const localAnchorA = objectA.worldToLocal(anchor);
|
|
7547
|
+
const localAnchorB = objectB.worldToLocal(anchor);
|
|
7548
|
+
const jointDef = new box2d.instance.b2WeldJointDef();
|
|
7549
|
+
jointDef.set_bodyA(objectA.body);
|
|
7550
|
+
jointDef.set_bodyB(objectB.body);
|
|
7551
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7552
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7553
|
+
jointDef.set_referenceAngle(objectA.body.GetAngle() - objectB.body.GetAngle());
|
|
7554
|
+
jointDef.set_collideConnected(collide);
|
|
7555
|
+
super(jointDef);
|
|
7556
|
+
}
|
|
7557
|
+
|
|
7558
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7559
|
+
* @return {Vector2} */
|
|
7560
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7561
|
+
|
|
7562
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7563
|
+
* @return {Vector2} */
|
|
7564
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7565
|
+
|
|
7566
|
+
/** Get the reference angle
|
|
7567
|
+
* @return {number} */
|
|
7568
|
+
getReferenceAngle() { return this.box2dJoint.GetReferenceAngle(); }
|
|
7569
|
+
|
|
7570
|
+
/** Set the frequency in Hertz
|
|
7571
|
+
* @param {number} hz */
|
|
7572
|
+
setFrequency(hz) { return this.box2dJoint.SetFrequency(hz); }
|
|
7573
|
+
|
|
7574
|
+
/** Get the frequency in Hertz
|
|
7575
|
+
* @return {number} */
|
|
7576
|
+
getFrequency() { return this.box2dJoint.GetFrequency(); }
|
|
7577
|
+
|
|
7578
|
+
/** Set the damping ratio
|
|
7579
|
+
* @param {number} ratio */
|
|
7580
|
+
setSpringDampingRatio(ratio) { return this.box2dJoint.SetSpringDampingRatio(ratio); }
|
|
7581
|
+
|
|
7582
|
+
/** Get the damping ratio
|
|
7583
|
+
* @return {number} */
|
|
7584
|
+
getSpringDampingRatio() { return this.box2dJoint.GetSpringDampingRatio(); }
|
|
7585
|
+
}
|
|
7586
|
+
|
|
7587
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7588
|
+
/**
|
|
7589
|
+
* Box2D Friction Joint
|
|
7590
|
+
* - Used to apply top-down friction
|
|
7591
|
+
* - Provides 2D translational friction and angular friction
|
|
7592
|
+
* @extends Box2dJoint
|
|
7593
|
+
*/
|
|
7594
|
+
class Box2dFrictionJoint extends Box2dJoint
|
|
7595
|
+
{
|
|
7596
|
+
/** Create a friction joint
|
|
7597
|
+
* @param {Box2dObject} objectA
|
|
7598
|
+
* @param {Box2dObject} objectB
|
|
7599
|
+
* @param {Vector2} anchor
|
|
7600
|
+
* @param {boolean} [collide] */
|
|
7601
|
+
constructor(objectA, objectB, anchor, collide=false)
|
|
7602
|
+
{
|
|
7603
|
+
anchor ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7604
|
+
const localAnchorA = objectA.worldToLocal(anchor);
|
|
7605
|
+
const localAnchorB = objectB.worldToLocal(anchor);
|
|
7606
|
+
const jointDef = new box2d.instance.b2FrictionJointDef();
|
|
7607
|
+
jointDef.set_bodyA(objectA.body);
|
|
7608
|
+
jointDef.set_bodyB(objectB.body);
|
|
7609
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7610
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7611
|
+
jointDef.set_collideConnected(collide);
|
|
7612
|
+
super(jointDef);
|
|
7613
|
+
}
|
|
7614
|
+
|
|
7615
|
+
/** Get the local anchor point relative to objectA's origin
|
|
7616
|
+
* @return {Vector2} */
|
|
7617
|
+
getLocalAnchorA() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorA()); }
|
|
7618
|
+
|
|
7619
|
+
/** Get the local anchor point relative to objectB's origin
|
|
7620
|
+
* @return {Vector2} */
|
|
7621
|
+
getLocalAnchorB() { return box2d.vec2From(this.box2dJoint.GetLocalAnchorB()); }
|
|
7622
|
+
|
|
7623
|
+
/** Set the maximum friction force
|
|
7624
|
+
* @param {number} force */
|
|
7625
|
+
setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
|
|
7626
|
+
|
|
7627
|
+
/** Get the maximum friction force
|
|
7628
|
+
* @return {number} */
|
|
7629
|
+
getMaxForce() { return this.box2dJoint.GetMaxForce(); }
|
|
7630
|
+
|
|
7631
|
+
/** Set the maximum friction torque
|
|
7632
|
+
* @param {number} torque */
|
|
7633
|
+
setMaxTorque(torque) { this.box2dJoint.SetMaxTorque(torque); }
|
|
7634
|
+
|
|
7635
|
+
/** Get the maximum friction torque
|
|
7636
|
+
* @return {number} */
|
|
7637
|
+
getMaxTorque() { return this.box2dJoint.GetMaxTorque(); }
|
|
7638
|
+
}
|
|
7639
|
+
|
|
7640
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7641
|
+
/**
|
|
7642
|
+
* Box2D Pulley Joint
|
|
7643
|
+
* - Connects to two objects and two fixed ground points
|
|
7644
|
+
* - The pulley supports a ratio such that: length1 + ratio * length2 <= constant
|
|
7645
|
+
* - The force transmitted is scaled by the ratio
|
|
7646
|
+
* @extends Box2dJoint
|
|
7647
|
+
*/
|
|
7648
|
+
class Box2dPulleyJoint extends Box2dJoint
|
|
7649
|
+
{
|
|
7650
|
+
/** Create a pulley joint
|
|
7651
|
+
* @param {Box2dObject} objectA
|
|
7652
|
+
* @param {Box2dObject} objectB
|
|
7653
|
+
* @param {Vector2} groundAnchorA
|
|
7654
|
+
* @param {Vector2} groundAnchorB
|
|
7655
|
+
* @param {Vector2} anchorA
|
|
7656
|
+
* @param {Vector2} anchorB
|
|
7657
|
+
* @param {number} [ratio]
|
|
7658
|
+
* @param {boolean} [collide] */
|
|
7659
|
+
constructor(objectA, objectB, groundAnchorA, groundAnchorB, anchorA, anchorB, ratio=1, collide=false)
|
|
7660
|
+
{
|
|
7661
|
+
anchorA ||= box2d.vec2From(objectA.body.GetPosition());
|
|
7662
|
+
anchorB ||= box2d.vec2From(objectB.body.GetPosition());
|
|
7663
|
+
const localAnchorA = objectA.worldToLocal(anchorA);
|
|
7664
|
+
const localAnchorB = objectB.worldToLocal(anchorB);
|
|
7665
|
+
const jointDef = new box2d.instance.b2PulleyJointDef();
|
|
7666
|
+
jointDef.set_bodyA(objectA.body);
|
|
7667
|
+
jointDef.set_bodyB(objectB.body);
|
|
7668
|
+
jointDef.set_groundAnchorA(box2d.vec2dTo(groundAnchorA));
|
|
7669
|
+
jointDef.set_groundAnchorB(box2d.vec2dTo(groundAnchorB));
|
|
7670
|
+
jointDef.set_localAnchorA(box2d.vec2dTo(localAnchorA));
|
|
7671
|
+
jointDef.set_localAnchorB(box2d.vec2dTo(localAnchorB));
|
|
7672
|
+
jointDef.set_ratio(ratio);
|
|
7673
|
+
jointDef.set_lengthA(groundAnchorA.distance(anchorA));
|
|
7674
|
+
jointDef.set_lengthB(groundAnchorB.distance(anchorB));
|
|
7675
|
+
jointDef.set_collideConnected(collide);
|
|
7676
|
+
super(jointDef);
|
|
7677
|
+
}
|
|
7678
|
+
|
|
7679
|
+
/** Get the first ground anchor
|
|
7680
|
+
* @return {Vector2} */
|
|
7681
|
+
getGroundAnchorA() { return box2d.vec2From(this.box2dJoint.GetGroundAnchorA()); }
|
|
7682
|
+
|
|
7683
|
+
/** Get the second ground anchor
|
|
7684
|
+
* @return {Vector2} */
|
|
7685
|
+
getGroundAnchorB() { return box2d.vec2From(this.box2dJoint.GetGroundAnchorB()); }
|
|
7686
|
+
|
|
7687
|
+
/** Get the current length of the segment attached to objectA
|
|
7688
|
+
* @return {number} */
|
|
7689
|
+
getLengthA() { return this.box2dJoint.GetLengthA(); }
|
|
7690
|
+
|
|
7691
|
+
/** Get the current length of the segment attached to objectB
|
|
7692
|
+
* @return {number} */
|
|
7693
|
+
getLengthB(){ return this.box2dJoint.GetLengthB(); }
|
|
7694
|
+
|
|
7695
|
+
/** Get the pulley ratio
|
|
7696
|
+
* @return {number} */
|
|
7697
|
+
getRatio() { return this.box2dJoint.GetRatio(); }
|
|
7698
|
+
|
|
7699
|
+
/** Get the current length of the segment attached to objectA
|
|
7700
|
+
* @return {number} */
|
|
7701
|
+
getCurrentLengthA() { return this.box2dJoint.GetCurrentLengthA(); }
|
|
7702
|
+
|
|
7703
|
+
/** Get the current length of the segment attached to objectB
|
|
7704
|
+
* @return {number} */
|
|
7705
|
+
getCurrentLengthB() { return this.box2dJoint.GetCurrentLengthB(); }
|
|
7706
|
+
}
|
|
7707
|
+
|
|
7708
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7709
|
+
/**
|
|
7710
|
+
* Box2D Motor Joint
|
|
7711
|
+
* - Controls the relative motion between two objects
|
|
7712
|
+
* - Typical usage is to control the movement of a object with respect to the ground
|
|
7713
|
+
* @extends Box2dJoint
|
|
7714
|
+
*/
|
|
7715
|
+
class Box2dMotorJoint extends Box2dJoint
|
|
7716
|
+
{
|
|
7717
|
+
/** Create a motor joint
|
|
7718
|
+
* @param {Box2dObject} objectA
|
|
7719
|
+
* @param {Box2dObject} objectB */
|
|
7720
|
+
constructor(objectA, objectB)
|
|
7721
|
+
{
|
|
7722
|
+
const linearOffset = objectA.worldToLocal(box2d.vec2From(objectB.body.GetPosition()));
|
|
7723
|
+
const angularOffset = objectB.body.GetAngle() - objectA.body.GetAngle();
|
|
7724
|
+
const jointDef = new box2d.instance.b2MotorJointDef();
|
|
7725
|
+
jointDef.set_bodyA(objectA.body);
|
|
7726
|
+
jointDef.set_bodyB(objectB.body);
|
|
7727
|
+
jointDef.set_linearOffset(box2d.vec2dTo(linearOffset));
|
|
7728
|
+
jointDef.set_angularOffset(angularOffset);
|
|
7729
|
+
super(jointDef);
|
|
7730
|
+
}
|
|
7731
|
+
|
|
7732
|
+
/** Set the target linear offset, in frame A, in meters.
|
|
7733
|
+
* @param {Vector2} offset */
|
|
7734
|
+
setLinearOffset(offset) { this.box2dJoint.SetLinearOffset(box2d.vec2dTo(offset)); }
|
|
7735
|
+
|
|
7736
|
+
/** Get the target linear offset, in frame A, in meters.
|
|
7737
|
+
* @return {Vector2} */
|
|
7738
|
+
getLinearOffset() { return box2d.vec2From(this.box2dJoint.GetLinearOffset()); }
|
|
7739
|
+
|
|
7740
|
+
/** Set the target angular offset
|
|
7741
|
+
* @param {number} offset */
|
|
7742
|
+
setAngularOffset(offset) { this.box2dJoint.SetAngularOffset(offset); }
|
|
7743
|
+
|
|
7744
|
+
/** Get the target angular offset
|
|
7745
|
+
* @return {number} */
|
|
7746
|
+
getAngularOffset() { return this.box2dJoint.GetAngularOffset(); }
|
|
7747
|
+
|
|
7748
|
+
/** Set the maximum friction force
|
|
7749
|
+
* @param {number} force */
|
|
7750
|
+
setMaxForce(force) { this.box2dJoint.SetMaxForce(force); }
|
|
7751
|
+
|
|
7752
|
+
/** Get the maximum friction force
|
|
7753
|
+
* @return {number} */
|
|
7754
|
+
getMaxForce() { return this.box2dJoint.GetMaxForce(); }
|
|
7755
|
+
|
|
7756
|
+
/** Set the maximum torque
|
|
7757
|
+
* @param {number} torque */
|
|
7758
|
+
setMaxTorque(torque) { this.box2dJoint.SetMaxTorque(torque); }
|
|
7759
|
+
|
|
7760
|
+
/** Get the maximum torque
|
|
7761
|
+
* @return {number} */
|
|
7762
|
+
getMaxTorque() { return this.box2dJoint.GetMaxTorque(); }
|
|
7763
|
+
|
|
7764
|
+
/** Set the position correction factor in the range [0,1]
|
|
7765
|
+
* @param {number} factor */
|
|
7766
|
+
setCorrectionFactor(factor) { this.box2dJoint.SetCorrectionFactor(factor); }
|
|
7767
|
+
|
|
7768
|
+
/** Get the position correction factor in the range [0,1]
|
|
7769
|
+
* @return {number} */
|
|
7770
|
+
getCorrectionFactor() { return this.box2dJoint.GetCorrectionFactor(); }
|
|
7771
|
+
}
|
|
7772
|
+
|
|
7773
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7774
|
+
/**
|
|
7775
|
+
* Box2D Global Object
|
|
7776
|
+
* - Wraps Box2d world and provides global functions
|
|
7777
|
+
*/
|
|
7778
|
+
class Box2dPlugin
|
|
7779
|
+
{
|
|
7780
|
+
/** Create the global UI system object
|
|
7781
|
+
* @param {Object} instance */
|
|
7782
|
+
constructor(instance)
|
|
7783
|
+
{
|
|
7784
|
+
ASSERT(!box2d, 'Box2D already initialized');
|
|
7785
|
+
box2d = this;
|
|
7786
|
+
this.instance = instance;
|
|
7787
|
+
this.world = new box2d.instance.b2World();
|
|
7788
|
+
|
|
7789
|
+
/** @property {number} - Velocity iterations per update*/
|
|
7790
|
+
this.velocityIterations = 8;
|
|
7791
|
+
/** @property {number} - Position iterations per update*/
|
|
7792
|
+
this.positionIterations = 3;
|
|
7793
|
+
/** @property {number} - Static, zero mass, zero velocity, may be manually moved */
|
|
7794
|
+
this.bodyTypeStatic = instance.b2_staticBody;
|
|
7795
|
+
/** @property {number} - Kinematic, zero mass, non-zero velocity set by user, moved by solver */
|
|
7796
|
+
this.bodyTypeKinematic = instance.b2_kinematicBody;
|
|
7797
|
+
/** @property {number} - Dynamic, positive mass, non-zero velocity determined by forces, moved by solver */
|
|
7798
|
+
this.bodyTypeDynamic = instance.b2_dynamicBody;
|
|
7799
|
+
|
|
7800
|
+
// setup contact listener
|
|
7801
|
+
const listener = new box2d.instance.JSContactListener();
|
|
7802
|
+
listener.BeginContact = function(contactPtr)
|
|
7803
|
+
{
|
|
7804
|
+
const contact = box2d.instance.wrapPointer(contactPtr, box2d.instance.b2Contact);
|
|
7805
|
+
const fixtureA = contact.GetFixtureA();
|
|
7806
|
+
const fixtureB = contact.GetFixtureB();
|
|
7807
|
+
const objectA = fixtureA.GetBody().object;
|
|
7808
|
+
const objectB = fixtureB.GetBody().object;
|
|
7809
|
+
objectA.beginContact(objectB);
|
|
7810
|
+
objectB.beginContact(objectA);
|
|
7811
|
+
}
|
|
7812
|
+
listener.EndContact = function(contactPtr)
|
|
7813
|
+
{
|
|
7814
|
+
const contact = box2d.instance.wrapPointer(contactPtr, box2d.instance.b2Contact);
|
|
7815
|
+
const fixtureA = contact.GetFixtureA();
|
|
7816
|
+
const fixtureB = contact.GetFixtureB();
|
|
7817
|
+
const objectA = fixtureA.GetBody().object;
|
|
7818
|
+
const objectB = fixtureB.GetBody().object;
|
|
7819
|
+
objectA.endContact(objectB);
|
|
7820
|
+
objectB.endContact(objectA);
|
|
7821
|
+
};
|
|
7822
|
+
listener.PreSolve = function() {};
|
|
7823
|
+
listener.PostSolve = function() {};
|
|
7824
|
+
box2d.world.SetContactListener(listener);
|
|
7825
|
+
}
|
|
7826
|
+
|
|
7827
|
+
/** Step the physics world simulation
|
|
7828
|
+
* @param {number} [frames] */
|
|
7829
|
+
step(frames=1)
|
|
7830
|
+
{
|
|
7831
|
+
box2d.world.SetGravity(box2d.vec2dTo(gravity));
|
|
7832
|
+
for (let i=frames; i--;)
|
|
7833
|
+
box2d.world.Step(timeDelta, this.velocityIterations, this.positionIterations);
|
|
7834
|
+
}
|
|
7835
|
+
|
|
7836
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7837
|
+
// raycasting and querying
|
|
7838
|
+
|
|
7839
|
+
/** raycast and return a list of all the results
|
|
7840
|
+
* @param {Vector2} start
|
|
7841
|
+
* @param {Vector2} end */
|
|
7842
|
+
raycastAll(start, end)
|
|
7843
|
+
{
|
|
7844
|
+
const raycastCallback = new box2d.instance.JSRayCastCallback();
|
|
7845
|
+
raycastCallback.ReportFixture = function(fixturePointer, point, normal, fraction)
|
|
7846
|
+
{
|
|
7847
|
+
const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
|
|
7848
|
+
point = box2d.vec2FromPointer(point);
|
|
7849
|
+
normal = box2d.vec2FromPointer(normal);
|
|
7850
|
+
raycastResults.push(new Box2dRaycastResult(fixture, point, normal, fraction));
|
|
7851
|
+
return 1; // continue getting results
|
|
7852
|
+
};
|
|
7853
|
+
|
|
7854
|
+
const raycastResults = [];
|
|
7855
|
+
box2d.world.RayCast(raycastCallback, box2d.vec2dTo(start), box2d.vec2dTo(end));
|
|
7856
|
+
debugRaycast && debugLine(start, end, raycastResults.length ? '#f00' : '#00f', .02);
|
|
7857
|
+
return raycastResults;
|
|
7858
|
+
}
|
|
7859
|
+
|
|
7860
|
+
/** raycast and return the first result
|
|
7861
|
+
* @param {Vector2} start
|
|
7862
|
+
* @param {Vector2} end */
|
|
7863
|
+
raycast(start, end)
|
|
7864
|
+
{
|
|
7865
|
+
const raycastResults = box2d.raycastAll(start, end);
|
|
7866
|
+
if (!raycastResults.length)
|
|
7867
|
+
return undefined;
|
|
7868
|
+
return raycastResults.reduce((a,b)=>a.fraction < b.fraction ? a : b);
|
|
7869
|
+
}
|
|
7870
|
+
|
|
7871
|
+
/** box aabb cast and return all the objects
|
|
7872
|
+
* @param {Vector2} pos
|
|
7873
|
+
* @param {Vector2} size */
|
|
7874
|
+
boxCastAll(pos, size)
|
|
7875
|
+
{
|
|
7876
|
+
const queryCallback = new box2d.instance.JSQueryCallback();
|
|
7877
|
+
queryCallback.ReportFixture = function(fixturePointer)
|
|
7878
|
+
{
|
|
7879
|
+
const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
|
|
7880
|
+
const o = fixture.GetBody().object;
|
|
7881
|
+
if (!queryObjects.includes(o))
|
|
7882
|
+
queryObjects.push(o); // add if not already in list
|
|
7883
|
+
return true; // continue getting results
|
|
7884
|
+
};
|
|
7885
|
+
|
|
7886
|
+
const aabb = new box2d.instance.b2AABB();
|
|
7887
|
+
aabb.set_lowerBound(box2d.vec2dTo(pos.subtract(size.scale(.5))));
|
|
7888
|
+
aabb.set_upperBound(box2d.vec2dTo(pos.add(size.scale(.5))));
|
|
7889
|
+
|
|
7890
|
+
let queryObjects = [];
|
|
7891
|
+
box2d.world.QueryAABB(queryCallback, aabb);
|
|
7892
|
+
debugRaycast && debugRect(pos, size, queryObjects.length ? '#f00' : '#00f', .02);
|
|
7893
|
+
return queryObjects;
|
|
7894
|
+
}
|
|
7895
|
+
|
|
7896
|
+
/** box aabb cast and return the first object
|
|
7897
|
+
* @param {Vector2} pos
|
|
7898
|
+
* @param {Vector2} size */
|
|
7899
|
+
boxCast(pos, size)
|
|
7900
|
+
{
|
|
7901
|
+
const queryCallback = new box2d.instance.JSQueryCallback();
|
|
7902
|
+
queryCallback.ReportFixture = function(fixturePointer)
|
|
7903
|
+
{
|
|
7904
|
+
const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
|
|
7905
|
+
queryObject = fixture.GetBody().object;
|
|
7906
|
+
return false; // stop getting results
|
|
7907
|
+
};
|
|
7908
|
+
|
|
7909
|
+
const aabb = new box2d.instance.b2AABB();
|
|
7910
|
+
aabb.set_lowerBound(box2d.vec2dTo(pos.subtract(size.scale(.5))));
|
|
7911
|
+
aabb.set_upperBound(box2d.vec2dTo(pos.add(size.scale(.5))));
|
|
7912
|
+
|
|
7913
|
+
let queryObject;
|
|
7914
|
+
box2d.world.QueryAABB(queryCallback, aabb);
|
|
7915
|
+
debugRaycast && debugRect(pos, size, queryObject ? '#f00' : '#00f', .02);
|
|
7916
|
+
return queryObject;
|
|
7917
|
+
}
|
|
7918
|
+
|
|
7919
|
+
/** circle cast and return all the objects
|
|
7920
|
+
* @param {Vector2} pos
|
|
7921
|
+
* @param {number} diameter */
|
|
7922
|
+
circleCastAll(pos, diameter)
|
|
7923
|
+
{
|
|
7924
|
+
const radius2 = (diameter/2)**2;
|
|
7925
|
+
const results = box2d.boxCastAll(pos, vec2(diameter));
|
|
7926
|
+
return results.filter(o=>o.pos.distanceSquared(pos) < radius2);
|
|
7927
|
+
}
|
|
7928
|
+
|
|
7929
|
+
/** circle cast and return the first object
|
|
7930
|
+
* @param {Vector2} pos
|
|
7931
|
+
* @param {number} diameter */
|
|
7932
|
+
circleCast(pos, diameter)
|
|
7933
|
+
{
|
|
7934
|
+
const radius2 = (diameter/2)**2;
|
|
7935
|
+
let results = box2d.boxCastAll(pos, vec2(diameter));
|
|
7936
|
+
|
|
7937
|
+
let bestResult, bestDistance2;
|
|
7938
|
+
for (const result of results)
|
|
7939
|
+
{
|
|
7940
|
+
const distance2 = result.pos.distanceSquared(pos);
|
|
7941
|
+
if (distance2 < radius2 && (!bestResult || distance2 < bestDistance2))
|
|
7942
|
+
{
|
|
7943
|
+
bestResult = result;
|
|
7944
|
+
bestDistance2 = distance2;
|
|
7945
|
+
}
|
|
7946
|
+
}
|
|
7947
|
+
return bestResult;
|
|
7948
|
+
}
|
|
7949
|
+
|
|
7950
|
+
/** point cast and return the first object
|
|
7951
|
+
* @param {Vector2} pos
|
|
7952
|
+
* @param {boolean} dynamicOnly */
|
|
7953
|
+
pointCast(pos, dynamicOnly=true)
|
|
7954
|
+
{
|
|
7955
|
+
const queryCallback = new box2d.instance.JSQueryCallback();
|
|
7956
|
+
queryCallback.ReportFixture = function(fixturePointer)
|
|
7957
|
+
{
|
|
7958
|
+
const fixture = box2d.instance.wrapPointer(fixturePointer, box2d.instance.b2Fixture);
|
|
7959
|
+
if (dynamicOnly && fixture.GetBody().GetType() != box2d.instance.b2_dynamicBody)
|
|
7960
|
+
return true; // continue getting results
|
|
7961
|
+
if (!fixture.TestPoint(box2d.vec2dTo(pos)))
|
|
7962
|
+
return true; // continue getting results
|
|
7963
|
+
queryObject = fixture.GetBody().object;
|
|
7964
|
+
return false; // stop getting results
|
|
7965
|
+
};
|
|
7966
|
+
|
|
7967
|
+
const aabb = new box2d.instance.b2AABB();
|
|
7968
|
+
aabb.set_lowerBound(box2d.vec2dTo(pos));
|
|
7969
|
+
aabb.set_upperBound(box2d.vec2dTo(pos));
|
|
7970
|
+
|
|
7971
|
+
let queryObject;
|
|
7972
|
+
box2d.world.QueryAABB(queryCallback, aabb);
|
|
7973
|
+
debugRaycast && debugRect(pos, vec2(), queryObject ? '#f00' : '#00f', .02);
|
|
7974
|
+
return queryObject;
|
|
7975
|
+
}
|
|
7976
|
+
|
|
7977
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
7978
|
+
// drawing
|
|
7979
|
+
|
|
7980
|
+
/** draws a fixture
|
|
7981
|
+
* @param {Object} fixture
|
|
7982
|
+
* @param {Vector2} pos
|
|
7983
|
+
* @param {number} angle
|
|
7984
|
+
* @param {Color} [color]
|
|
7985
|
+
* @param {Color} [outlineColor]
|
|
7986
|
+
* @param {number} [lineWidth]
|
|
7987
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
7988
|
+
drawFixture(fixture, pos, angle, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
|
|
7989
|
+
{
|
|
7990
|
+
const shape = box2d.castObjectType(fixture.GetShape());
|
|
7991
|
+
switch (shape.GetType())
|
|
7992
|
+
{
|
|
7993
|
+
case box2d.instance.b2Shape.e_polygon:
|
|
7994
|
+
{
|
|
7995
|
+
let points = [];
|
|
7996
|
+
for (let i=shape.GetVertexCount(); i--;)
|
|
7997
|
+
points.push(box2d.vec2From(shape.GetVertex(i)));
|
|
7998
|
+
box2d.drawPoly(pos, angle, points, color, outlineColor, lineWidth, context);
|
|
7999
|
+
break;
|
|
8000
|
+
}
|
|
8001
|
+
case box2d.instance.b2Shape.e_circle:
|
|
8002
|
+
{
|
|
8003
|
+
const radius = shape.get_m_radius();
|
|
8004
|
+
box2d.drawCircle(pos, radius, color, outlineColor, lineWidth, context);
|
|
8005
|
+
break;
|
|
8006
|
+
}
|
|
8007
|
+
case box2d.instance.b2Shape.e_edge:
|
|
8008
|
+
{
|
|
8009
|
+
const v1 = box2d.vec2From(shape.get_m_vertex1());
|
|
8010
|
+
const v2 = box2d.vec2From(shape.get_m_vertex2());
|
|
8011
|
+
box2d.drawLine(pos, angle, v1, v2, color, lineWidth, context);
|
|
8012
|
+
break;
|
|
8013
|
+
}
|
|
8014
|
+
}
|
|
8015
|
+
}
|
|
8016
|
+
|
|
8017
|
+
/** draws a circle
|
|
8018
|
+
* @param {Vector2} pos
|
|
8019
|
+
* @param {number} radius
|
|
8020
|
+
* @param {Color} [color]
|
|
8021
|
+
* @param {Color} [outlineColor]
|
|
8022
|
+
* @param {number} [lineWidth]
|
|
8023
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
8024
|
+
drawCircle(pos, radius, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
|
|
8025
|
+
{
|
|
8026
|
+
drawCanvas2D(pos, vec2(1), 0, 0, context=>
|
|
8027
|
+
{
|
|
8028
|
+
context.beginPath();
|
|
8029
|
+
context.arc(0, 0, radius, 0, 9);
|
|
8030
|
+
box2d.drawFillStroke(color, outlineColor, lineWidth, context);
|
|
8031
|
+
}, 0, context);
|
|
8032
|
+
}
|
|
8033
|
+
|
|
8034
|
+
/** draws a polygon
|
|
8035
|
+
* @param {Vector2} pos
|
|
8036
|
+
* @param {number} angle
|
|
8037
|
+
* @param {Array<Vector2>} points
|
|
8038
|
+
* @param {Color} [color]
|
|
8039
|
+
* @param {Color} [outlineColor]
|
|
8040
|
+
* @param {number} [lineWidth]
|
|
8041
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
8042
|
+
drawPoly(pos, angle, points, color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
|
|
8043
|
+
{
|
|
8044
|
+
drawCanvas2D(pos, vec2(1), angle, 0, context=>
|
|
8045
|
+
{
|
|
8046
|
+
context.beginPath();
|
|
8047
|
+
points.forEach(p=>context.lineTo(p.x, p.y));
|
|
8048
|
+
context.closePath();
|
|
8049
|
+
box2d.drawFillStroke(color, outlineColor, lineWidth, context);
|
|
8050
|
+
}, 0, context);
|
|
8051
|
+
}
|
|
8052
|
+
|
|
8053
|
+
/** draws a line
|
|
8054
|
+
* @param {Vector2} pos
|
|
8055
|
+
* @param {number} angle
|
|
8056
|
+
* @param {Vector2} posA
|
|
8057
|
+
* @param {Vector2} posB
|
|
8058
|
+
* @param {Color} [color]
|
|
8059
|
+
* @param {number} [lineWidth]
|
|
8060
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
8061
|
+
drawLine(pos, angle, posA, posB, color=WHITE, lineWidth=.1, context=mainContext)
|
|
8062
|
+
{
|
|
8063
|
+
drawCanvas2D(pos, vec2(1), angle, 0, context=>
|
|
8064
|
+
{
|
|
8065
|
+
context.beginPath();
|
|
8066
|
+
context.lineTo(posA.x, posA.y);
|
|
8067
|
+
context.lineTo(posB.x, posB.y);
|
|
8068
|
+
box2d.drawFillStroke(0, color, lineWidth, context);
|
|
8069
|
+
}, 0, context);
|
|
8070
|
+
}
|
|
8071
|
+
|
|
8072
|
+
/** performs a fill or stroke as a helper to the other draw functions
|
|
8073
|
+
* @param {Color} [color]
|
|
8074
|
+
* @param {Color} [outlineColor]
|
|
8075
|
+
* @param {number} [lineWidth]
|
|
8076
|
+
* @param {CanvasRenderingContext2D} [context] */
|
|
8077
|
+
drawFillStroke(color=WHITE, outlineColor=BLACK, lineWidth=.1, context=mainContext)
|
|
8078
|
+
{
|
|
8079
|
+
if (color)
|
|
8080
|
+
{
|
|
8081
|
+
context.fillStyle = color.toString();
|
|
8082
|
+
context.fill();
|
|
8083
|
+
}
|
|
8084
|
+
if (outlineColor && lineWidth)
|
|
8085
|
+
{
|
|
8086
|
+
context.lineWidth = lineWidth;
|
|
8087
|
+
context.lineJoin = context.lineCap = 'round';
|
|
8088
|
+
context.strokeStyle = outlineColor.toString();
|
|
8089
|
+
context.stroke();
|
|
8090
|
+
}
|
|
8091
|
+
}
|
|
8092
|
+
|
|
8093
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
8094
|
+
// helper functions
|
|
8095
|
+
|
|
8096
|
+
/** converts a box2d vec2 to a Vector2
|
|
8097
|
+
* @param {Object} v */
|
|
8098
|
+
vec2From(v)
|
|
8099
|
+
{
|
|
8100
|
+
ASSERT(v instanceof box2d.instance.b2Vec2);
|
|
8101
|
+
return new Vector2(v.get_x(), v.get_y());
|
|
8102
|
+
}
|
|
8103
|
+
|
|
8104
|
+
/** converts a box2d vec2 pointer to a Vector2
|
|
8105
|
+
* @param {Object} v */
|
|
8106
|
+
vec2FromPointer(v)
|
|
8107
|
+
{
|
|
8108
|
+
return box2d.vec2From(box2d.instance.wrapPointer(v, box2d.instance.b2Vec2));
|
|
8109
|
+
}
|
|
8110
|
+
|
|
8111
|
+
/** converts a Vector2 to a box2 vec2
|
|
8112
|
+
* @param {Vector2} v */
|
|
8113
|
+
vec2dTo(v)
|
|
8114
|
+
{
|
|
8115
|
+
ASSERT(v instanceof Vector2);
|
|
8116
|
+
return new box2d.instance.b2Vec2(v.x, v.y);
|
|
8117
|
+
}
|
|
8118
|
+
|
|
8119
|
+
/** checks if a box2d object is null
|
|
8120
|
+
* @param {Object} o */
|
|
8121
|
+
isNull(o) { return !box2d.instance.getPointer(o); }
|
|
8122
|
+
|
|
8123
|
+
/** casts a box2d object to its correct type
|
|
8124
|
+
* @param {Object} o */
|
|
8125
|
+
castObjectType(o)
|
|
8126
|
+
{
|
|
8127
|
+
switch (o.GetType())
|
|
8128
|
+
{
|
|
8129
|
+
case box2d.instance.b2Shape.e_circle:
|
|
8130
|
+
return box2d.instance.castObject(o, box2d.instance.b2CircleShape);
|
|
8131
|
+
case box2d.instance.b2Shape.e_edge:
|
|
8132
|
+
return box2d.instance.castObject(o, box2d.instance.b2EdgeShape);
|
|
8133
|
+
case box2d.instance.b2Shape.e_polygon:
|
|
8134
|
+
return box2d.instance.castObject(o, box2d.instance.b2PolygonShape);
|
|
8135
|
+
case box2d.instance.b2Shape.e_chain:
|
|
8136
|
+
return box2d.instance.castObject(o, box2d.instance.b2ChainShape);
|
|
8137
|
+
case box2d.instance.e_revoluteJoint:
|
|
8138
|
+
return box2d.instance.castObject(o, box2d.instance.b2RevoluteJoint);
|
|
8139
|
+
case box2d.instance.e_prismaticJoint:
|
|
8140
|
+
return box2d.instance.castObject(o, box2d.instance.b2PrismaticJoint);
|
|
8141
|
+
case box2d.instance.e_distanceJoint:
|
|
8142
|
+
return box2d.instance.castObject(o, box2d.instance.b2DistanceJoint);
|
|
8143
|
+
case box2d.instance.e_pulleyJoint:
|
|
8144
|
+
return box2d.instance.castObject(o, box2d.instance.b2PulleyJoint);
|
|
8145
|
+
case box2d.instance.e_mouseJoint:
|
|
8146
|
+
return box2d.instance.castObject(o, box2d.instance.b2MouseJoint);
|
|
8147
|
+
case box2d.instance.e_gearJoint:
|
|
8148
|
+
return box2d.instance.castObject(o, box2d.instance.b2GearJoint);
|
|
8149
|
+
case box2d.instance.e_wheelJoint:
|
|
8150
|
+
return box2d.instance.castObject(o, box2d.instance.b2WheelJoint);
|
|
8151
|
+
case box2d.instance.e_weldJoint:
|
|
8152
|
+
return box2d.instance.castObject(o, box2d.instance.b2WeldJoint);
|
|
8153
|
+
case box2d.instance.e_frictionJoint:
|
|
8154
|
+
return box2d.instance.castObject(o, box2d.instance.b2FrictionJoint);
|
|
8155
|
+
case box2d.instance.e_ropeJoint:
|
|
8156
|
+
return box2d.instance.castObject(o, box2d.instance.b2RopeJoint);
|
|
8157
|
+
case box2d.instance.e_motorJoint:
|
|
8158
|
+
return box2d.instance.castObject(o, box2d.instance.b2MotorJoint);
|
|
8159
|
+
}
|
|
8160
|
+
|
|
8161
|
+
ASSERT(false, 'Unknown box2d object type');
|
|
8162
|
+
}
|
|
8163
|
+
}
|
|
8164
|
+
|
|
8165
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
8166
|
+
/** Box2d Init - Startup LittleJS engine with your callback functions
|
|
8167
|
+
* @param {Function|function():Promise} gameInit - Called once after the engine starts up
|
|
8168
|
+
* @param {Function} gameUpdate - Called every frame before objects are updated
|
|
8169
|
+
* @param {Function} gameUpdatePost - Called after physics and objects are updated, even when paused
|
|
8170
|
+
* @param {Function} gameRender - Called before objects are rendered, for drawing the background
|
|
8171
|
+
* @param {Function} gameRenderPost - Called after objects are rendered, useful for drawing UI
|
|
8172
|
+
* @param {Array<string>} [imageSources=[]] - List of images to load
|
|
8173
|
+
* @param {HTMLElement} [rootElement] - Root element to attach to, the document body by default */
|
|
8174
|
+
function box2dEngineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement)
|
|
8175
|
+
{
|
|
8176
|
+
Box2D().then(box2dInstance=>
|
|
8177
|
+
{
|
|
8178
|
+
// create box2d object
|
|
8179
|
+
new Box2dPlugin(box2dInstance);
|
|
8180
|
+
setupDebugDraw();
|
|
8181
|
+
|
|
8182
|
+
// start littlejs
|
|
8183
|
+
engineAddPlugin(box2dUpdate, box2dRender);
|
|
8184
|
+
engineInit(gameInit, gameUpdate, gameUpdatePost, gameRender, gameRenderPost, imageSources, rootElement);
|
|
8185
|
+
});
|
|
8186
|
+
|
|
8187
|
+
// hook up box2d plugin to update and render
|
|
8188
|
+
function box2dUpdate()
|
|
8189
|
+
{
|
|
8190
|
+
if (!paused)
|
|
8191
|
+
box2d.step();
|
|
8192
|
+
}
|
|
8193
|
+
function box2dRender()
|
|
8194
|
+
{
|
|
8195
|
+
if (box2dDebug || debugPhysics && debugOverlay)
|
|
8196
|
+
box2d.world.DrawDebugData();
|
|
8197
|
+
}
|
|
8198
|
+
|
|
8199
|
+
// box2d debug drawing
|
|
8200
|
+
function setupDebugDraw()
|
|
8201
|
+
{
|
|
8202
|
+
// setup debug draw
|
|
8203
|
+
const debugDraw = new box2d.instance.JSDraw();
|
|
8204
|
+
const box2dColor = (c)=> new Color(c.get_r(), c.get_g(), c.get_b());
|
|
8205
|
+
const box2dColorPointer = (c)=>
|
|
8206
|
+
box2dColor(box2d.instance.wrapPointer(c, box2d.instance.b2Color));
|
|
8207
|
+
const getDebugColor = (color)=>box2dColorPointer(color).scale(1,.8);
|
|
8208
|
+
const getPointsList = (vertices, vertexCount) =>
|
|
8209
|
+
{
|
|
8210
|
+
const points = [];
|
|
8211
|
+
for (let i=vertexCount; i--;)
|
|
8212
|
+
points.push(box2d.vec2FromPointer(vertices+i*8));
|
|
8213
|
+
return points;
|
|
8214
|
+
}
|
|
8215
|
+
debugDraw.DrawSegment = function(point1, point2, color)
|
|
8216
|
+
{
|
|
8217
|
+
color = getDebugColor(color);
|
|
8218
|
+
point1 = box2d.vec2FromPointer(point1);
|
|
8219
|
+
point2 = box2d.vec2FromPointer(point2);
|
|
8220
|
+
box2d.drawLine(vec2(), 0, point1, point2, color, undefined, overlayContext);
|
|
8221
|
+
};
|
|
8222
|
+
debugDraw.DrawPolygon = function(vertices, vertexCount, color)
|
|
8223
|
+
{
|
|
8224
|
+
color = getDebugColor(color);
|
|
8225
|
+
const points = getPointsList(vertices, vertexCount);
|
|
8226
|
+
box2d.drawPoly(vec2(), 0, points, undefined, color, undefined, overlayContext);
|
|
8227
|
+
};
|
|
8228
|
+
debugDraw.DrawSolidPolygon = function(vertices, vertexCount, color)
|
|
8229
|
+
{
|
|
8230
|
+
color = getDebugColor(color);
|
|
8231
|
+
const points = getPointsList(vertices, vertexCount);
|
|
8232
|
+
box2d.drawPoly(vec2(), 0, points, color, color, undefined, overlayContext);
|
|
8233
|
+
};
|
|
8234
|
+
debugDraw.DrawCircle = function(center, radius, color)
|
|
8235
|
+
{
|
|
8236
|
+
color = getDebugColor(color);
|
|
8237
|
+
center = box2d.vec2FromPointer(center);
|
|
8238
|
+
box2d.drawCircle(center, radius, undefined, color, undefined, overlayContext);
|
|
8239
|
+
};
|
|
8240
|
+
debugDraw.DrawSolidCircle = function(center, radius, axis, color)
|
|
8241
|
+
{
|
|
8242
|
+
color = getDebugColor(color);
|
|
8243
|
+
center = box2d.vec2FromPointer(center);
|
|
8244
|
+
axis = box2d.vec2FromPointer(axis).scale(radius);
|
|
8245
|
+
box2d.drawCircle(center, radius, color, color, undefined, overlayContext);
|
|
8246
|
+
box2d.drawLine(center, 0, vec2(), axis, color, undefined, overlayContext);
|
|
8247
|
+
};
|
|
8248
|
+
debugDraw.DrawTransform = function(transform)
|
|
8249
|
+
{
|
|
8250
|
+
transform = box2d.instance.wrapPointer(transform, box2d.instance.b2Transform);
|
|
8251
|
+
const pos = vec2(transform.get_p());
|
|
8252
|
+
const angle = -transform.get_q().GetAngle();
|
|
8253
|
+
const p1 = vec2(1,0), c1 = rgb(.75,0,0,.8);
|
|
8254
|
+
const p2 = vec2(0,1), c2 = rgb(0,.75,0,.8);
|
|
8255
|
+
box2d.drawLine(pos, angle, vec2(), p1, c1, undefined, overlayContext);
|
|
8256
|
+
box2d.drawLine(pos, angle, vec2(), p2, c2, undefined, overlayContext);
|
|
8257
|
+
}
|
|
8258
|
+
|
|
8259
|
+
debugDraw.AppendFlags(box2d.instance.b2Draw.e_shapeBit);
|
|
8260
|
+
debugDraw.AppendFlags(box2d.instance.b2Draw.e_jointBit);
|
|
8261
|
+
//debugDraw.AppendFlags(box2d.instance.b2Draw.e_aabbBit);
|
|
8262
|
+
//debugDraw.AppendFlags(box2d.instance.b2Draw.e_pairBit);
|
|
8263
|
+
//debugDraw.AppendFlags(box2d.instance.b2Draw.e_centerOfMassBit);
|
|
8264
|
+
box2d.world.SetDebugDraw(debugDraw);
|
|
8265
|
+
}
|
|
8266
|
+
}
|
|
8267
|
+
|