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.

This commit is contained in:
Richard Davey 2020-01-08 14:41:24 +00:00
parent b8797de87a
commit d6e0ba9b29

View file

@ -13,6 +13,7 @@ var Composite = require('./lib/body/Composite');
var Composites = require('./lib/factory/Composites');
var Constraint = require('./lib/constraint/Constraint');
var Detector = require('./lib/collision/Detector');
var DistanceBetween = require('../../math/distance/DistanceBetween');
var Factory = require('./Factory');
var GetFastValue = require('../../utils/object/GetFastValue');
var GetValue = require('../../utils/object/GetValue');
@ -1271,6 +1272,38 @@ var MatterPhysics = new Class({
return this;
},
/**
* Returns the length of the given constraint, which is the distance between the two points.
*
* @method Phaser.Physics.Matter.MatterPhysics#getConstraintLength
* @since 3.22.0
*
* @param {MatterJS.Constraint} constraint - The constraint to get the length from.
*
* @return {number} The length of the constraint.
*/
getConstraintLength: function (constraint)
{
var aX = constraint.pointA.x;
var aY = constraint.pointA.y;
var bX = constraint.pointB.x;
var bY = constraint.pointB.y;
if (constraint.bodyA)
{
aX += constraint.bodyA.position.x;
aY += constraint.bodyA.position.y;
}
if (constraint.bodyB)
{
bX += constraint.bodyB.position.x;
bY += constraint.bodyB.position.y;
}
return DistanceBetween(aX, aY, bX, bY);
},
/**
* The Scene that owns this plugin is shutting down.
* We need to kill and reset all internal properties as well as stop listening to Scene events.