Skip to content

Commit 0385d10

Browse files
committed
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.
1 parent 214b383 commit 0385d10

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

src/gameobjects/components/Transform.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
var MATH_CONST = require('../../math/const');
88
var TransformMatrix = require('./TransformMatrix');
9+
var TransformXY = require('../../math/TransformXY');
910
var WrapAngle = require('../../math/angle/Wrap');
1011
var WrapAngleDegrees = require('../../math/angle/WrapDegrees');
12+
var Vector2 = require('../../math/Vector2');
1113

1214
// global bitmask flag for GameObject.renderMask (used by Scale)
1315
var _FLAG = 4; // 0100
@@ -502,6 +504,56 @@ var Transform = {
502504
return tempMatrix;
503505
},
504506

507+
/**
508+
* Takes the given `x` and `y` coordinates and converts them into local space for this
509+
* Game Object, taking into account parent and local transforms, and the Display Origin.
510+
*
511+
* The returned Vector2 contains the translated point in its properties.
512+
*
513+
* A Camera needs to be provided in order to handle modified scroll factors. If no
514+
* camera is specified, it will use the `main` camera from the Scene to which this
515+
* Game Object belongs.
516+
*
517+
* @method Phaser.GameObjects.Components.Transform#getLocalPoint
518+
* @since 3.50.0
519+
*
520+
* @param {number} x - The x position to translate.
521+
* @param {number} y - The y position to translate.
522+
* @param {Phaser.Math.Vector2} [point] - A Vector2, or point-like object, to store the results in.
523+
* @param {Phaser.Cameras.Scene2D.Camera} [camera] - The Camera which is being tested against. If not given will use the Scene default camera.
524+
*
525+
* @return {Phaser.Math.Vector2} The translated point.
526+
*/
527+
getLocalPoint: function (x, y, point, camera)
528+
{
529+
if (!point) { point = new Vector2(); }
530+
if (!camera) { camera = this.scene.sys.cameras.main; }
531+
532+
var csx = camera.scrollX;
533+
var csy = camera.scrollY;
534+
535+
var px = x + (csx * this.scrollFactorX) - csx;
536+
var py = y + (csy * this.scrollFactorY) - csy;
537+
538+
if (this.parentContainer)
539+
{
540+
this.getWorldTransformMatrix().applyInverse(px, py, point);
541+
}
542+
else
543+
{
544+
TransformXY(px, py, this.x, this.y, this.rotation, this.scaleX, this.scaleY, point);
545+
}
546+
547+
// Normalize origin
548+
if (this._originComponent)
549+
{
550+
point.x += this._displayOriginX;
551+
point.y += this._displayOriginY;
552+
}
553+
554+
return point;
555+
},
556+
505557
/**
506558
* Gets the sum total rotation of all of this Game Objects parent Containers.
507559
*

0 commit comments

Comments
 (0)