Skip to content

Commit d6e0ba9

Browse files
committed
MatterPhysics.getConstraintLength is a new method that will return the length of the given constraint, as this is something you cannot get from the constraint properties directly.
1 parent b8797de commit d6e0ba9

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/physics/matter-js/MatterPhysics.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Composite = require('./lib/body/Composite');
1313
var Composites = require('./lib/factory/Composites');
1414
var Constraint = require('./lib/constraint/Constraint');
1515
var Detector = require('./lib/collision/Detector');
16+
var DistanceBetween = require('../../math/distance/DistanceBetween');
1617
var Factory = require('./Factory');
1718
var GetFastValue = require('../../utils/object/GetFastValue');
1819
var GetValue = require('../../utils/object/GetValue');
@@ -1271,6 +1272,38 @@ var MatterPhysics = new Class({
12711272
return this;
12721273
},
12731274

1275+
/**
1276+
* Returns the length of the given constraint, which is the distance between the two points.
1277+
*
1278+
* @method Phaser.Physics.Matter.MatterPhysics#getConstraintLength
1279+
* @since 3.22.0
1280+
*
1281+
* @param {MatterJS.Constraint} constraint - The constraint to get the length from.
1282+
*
1283+
* @return {number} The length of the constraint.
1284+
*/
1285+
getConstraintLength: function (constraint)
1286+
{
1287+
var aX = constraint.pointA.x;
1288+
var aY = constraint.pointA.y;
1289+
var bX = constraint.pointB.x;
1290+
var bY = constraint.pointB.y;
1291+
1292+
if (constraint.bodyA)
1293+
{
1294+
aX += constraint.bodyA.position.x;
1295+
aY += constraint.bodyA.position.y;
1296+
}
1297+
1298+
if (constraint.bodyB)
1299+
{
1300+
bX += constraint.bodyB.position.x;
1301+
bY += constraint.bodyB.position.y;
1302+
}
1303+
1304+
return DistanceBetween(aX, aY, bX, bY);
1305+
},
1306+
12741307
/**
12751308
* The Scene that owns this plugin is shutting down.
12761309
* We need to kill and reset all internal properties as well as stop listening to Scene events.

0 commit comments

Comments
 (0)