Transform.getLocalPoint is a new method, available on all Game Objects, that takes an x / y pair and translates them into the local space of the Game Object, factoring in parent transforms and display origins.

This commit is contained in:
Richard Davey 2020-07-31 10:28:09 +01:00
parent 214b383fcd
commit 0385d108a8

View file

@ -6,8 +6,10 @@
var MATH_CONST = require('../../math/const'); var MATH_CONST = require('../../math/const');
var TransformMatrix = require('./TransformMatrix'); var TransformMatrix = require('./TransformMatrix');
var TransformXY = require('../../math/TransformXY');
var WrapAngle = require('../../math/angle/Wrap'); var WrapAngle = require('../../math/angle/Wrap');
var WrapAngleDegrees = require('../../math/angle/WrapDegrees'); var WrapAngleDegrees = require('../../math/angle/WrapDegrees');
var Vector2 = require('../../math/Vector2');
// global bitmask flag for GameObject.renderMask (used by Scale) // global bitmask flag for GameObject.renderMask (used by Scale)
var _FLAG = 4; // 0100 var _FLAG = 4; // 0100
@ -502,6 +504,56 @@ var Transform = {
return tempMatrix; return tempMatrix;
}, },
/**
* Takes the given `x` and `y` coordinates and converts them into local space for this
* Game Object, taking into account parent and local transforms, and the Display Origin.
*
* The returned Vector2 contains the translated point in its properties.
*
* A Camera needs to be provided in order to handle modified scroll factors. If no
* camera is specified, it will use the `main` camera from the Scene to which this
* Game Object belongs.
*
* @method Phaser.GameObjects.Components.Transform#getLocalPoint
* @since 3.50.0
*
* @param {number} x - The x position to translate.
* @param {number} y - The y position to translate.
* @param {Phaser.Math.Vector2} [point] - A Vector2, or point-like object, to store the results in.
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera which is being tested against. If not given will use the Scene default camera.
*
* @return {Phaser.Math.Vector2} The translated point.
*/
getLocalPoint: function (x, y, point, camera)
{
if (!point) { point = new Vector2(); }
if (!camera) { camera = this.scene.sys.cameras.main; }
var csx = camera.scrollX;
var csy = camera.scrollY;
var px = x + (csx * this.scrollFactorX) - csx;
var py = y + (csy * this.scrollFactorY) - csy;
if (this.parentContainer)
{
this.getWorldTransformMatrix().applyInverse(px, py, point);
}
else
{
TransformXY(px, py, this.x, this.y, this.rotation, this.scaleX, this.scaleY, point);
}
// Normalize origin
if (this._originComponent)
{
point.x += this._displayOriginX;
point.y += this._displayOriginY;
}
return point;
},
/** /**
* Gets the sum total rotation of all of this Game Objects parent Containers. * Gets the sum total rotation of all of this Game Objects parent Containers.
* *