Skip to content

Commit

Permalink
Transform.getWorldPoint is a new method that will return the World …
Browse files Browse the repository at this point in the history
…point as a Vector2 of a Game Object, factoring in parents if applicable

The `Transform.getWorldTransformMatrix` method will now destroy the parent matrix at the end, if it was created within the method.
  • Loading branch information
photonstorm committed Jan 3, 2025
1 parent 08f7310 commit 8df3803
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/gameobjects/components/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,13 @@ var Transform = {
return this.getLocalTransformMatrix(tempMatrix);
}

var destroyParentMatrix = false;

if (!parentMatrix)
{
parentMatrix = new TransformMatrix();

destroyParentMatrix = true;
}

tempMatrix.applyITRS(this.x, this.y, this._rotation, this._scaleX, this._scaleY);
Expand All @@ -536,6 +540,11 @@ var Transform = {
parent = parent.parentContainer;
}

if (destroyParentMatrix)
{
parentMatrix.destroy();
}

return tempMatrix;
},

Expand Down Expand Up @@ -589,6 +598,40 @@ var Transform = {
return point;
},

/**
* Gets the world position of this Game Object, factoring in any parent Containers.
*
* @method Phaser.GameObjects.Components.Transform#getWorldPoint
* @since 3.88.0
*
* @param {Phaser.Math.Vector2} [point] - A Vector2, or point-like object, to store the result in.
* @param {Phaser.GameObjects.Components.TransformMatrix} [tempMatrix] - A temporary matrix to hold the Game Object's values.
* @param {Phaser.GameObjects.Components.TransformMatrix} [parentMatrix] - A temporary matrix to hold parent values.
*
* @return {Phaser.Math.Vector2} The world position of this Game Object.
*/
getWorldPoint: function (point, tempMatrix, parentMatrix)
{
if (point === undefined) { point = new Vector2(); }

var parent = this.parentContainer;

if (!parent)
{
point.x = this.x;
point.y = this.y;

return point;
}

var worldTransform = this.getWorldTransformMatrix(tempMatrix, parentMatrix);

point.x = worldTransform.tx;
point.y = worldTransform.ty;

return point;
},

/**
* Gets the sum total rotation of all of this Game Objects parent Containers.
*
Expand Down

0 comments on commit 8df3803

Please sign in to comment.