From 9f012ebf86310e7e74895928da778f036126ba6d Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Mon, 9 Dec 2019 12:48:51 +0000 Subject: [PATCH] `Matter.Transform.centerOffsetX` is a new read-only property available on all Matter Game Objects that returns the horizontal offset between the center of the frame and the center of mass. This can be used to allow for accurately mapping texture centers to the body center. --- src/physics/matter-js/components/Transform.js | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/physics/matter-js/components/Transform.js b/src/physics/matter-js/components/Transform.js index 31eb3c4d1..767a1f1fd 100644 --- a/src/physics/matter-js/components/Transform.js +++ b/src/physics/matter-js/components/Transform.js @@ -187,6 +187,56 @@ var Transform = { } }, + /** + * Returns the center x offset of the Body this Game Object is using. + * + * This is calculated by taking the difference between the center of the frame and the center of + * the physics body. If set, the `body.render.sprite.xOffset` value is then added to it. + * + * Use this when setting the Origin of a Physics Game Object post-creation, i.e.: + * + * `setOrigin(originX + centerOffsetX, originY + centerOffsetY)` + * + * @name Phaser.Physics.Matter.Components.Transform#centerOffsetX + * @type {number} + * @readonly + * @since 3.22.0 + */ + centerOffsetX: { + + get: function () + { + var body = this.body; + + return body.render.sprite.xOffset + ((body.centerOfMass.x - (this.width / 2)) / this.width); + } + }, + + /** + * Returns the center y offset of the Body this Game Object is using. + * + * This is calculated by taking the difference between the center of the frame and the center of + * the physics body. If set, the `body.render.sprite.yOffset` value is then added to it. + * + * Use this when setting the Origin of a Physics Game Object post-creation, i.e.: + * + * `setOrigin(originX + centerOffsetX, originY + centerOffsetY)` + * + * @name Phaser.Physics.Matter.Components.Transform#centerOffsetY + * @type {number} + * @readonly + * @since 3.22.0 + */ + centerOffsetY: { + + get: function () + { + var body = this.body; + + return body.render.sprite.yOffset + ((body.centerOfMass.y - (this.height / 2)) / this.height); + } + }, + /** * Sets the position of the physics body along x and y axes. Both the parameters to this function are optional and if not passed any they default to 0. *