Skip to content

Commit d6b0341

Browse files
authored
Merge pull request phaserjs#4978 from samme/fix/arcade-body-coordinates
Arcade Physics fixes
2 parents 2a7cfb6 + 7d5673a commit d6b0341

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var Body = new Class({
3333

3434
function Body (world, gameObject)
3535
{
36-
var width = (gameObject.width) ? gameObject.width : 64;
37-
var height = (gameObject.height) ? gameObject.height : 64;
36+
var width = (gameObject.displayWidth) ? gameObject.displayWidth : 64;
37+
var height = (gameObject.displayHeight) ? gameObject.displayHeight : 64;
3838

3939
/**
4040
* The Arcade Physics simulation this Body belongs to.
@@ -148,7 +148,10 @@ var Body = new Class({
148148
* @type {Phaser.Math.Vector2}
149149
* @since 3.0.0
150150
*/
151-
this.position = new Vector2(gameObject.x, gameObject.y);
151+
this.position = new Vector2(
152+
gameObject.x - gameObject.scaleX * gameObject.displayOriginX,
153+
gameObject.y - gameObject.scaleY * gameObject.displayOriginY
154+
);
152155

153156
/**
154157
* The position of this Body during the previous step.
@@ -278,7 +281,7 @@ var Body = new Class({
278281
* @type {Phaser.Math.Vector2}
279282
* @since 3.0.0
280283
*/
281-
this.center = new Vector2(gameObject.x + this.halfWidth, gameObject.y + this.halfHeight);
284+
this.center = new Vector2(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
282285

283286
/**
284287
* The Body's velocity, in pixels per second.

src/physics/arcade/StaticBody.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var Vector2 = require('../../math/Vector2');
1515
* A Static Arcade Physics Body.
1616
*
1717
* A Static Body never moves, and isn't automatically synchronized with its parent Game Object.
18-
* That means if you make any change to the parent's origin, position, or scale after creating or adding the body, you'll need to update the Body manually.
18+
* That means if you make any change to the parent's origin, position, or scale after creating or adding the body, you'll need to update the Static Body manually.
1919
*
2020
* A Static Body can collide with other Bodies, but is never moved by collisions.
2121
*
@@ -35,8 +35,8 @@ var StaticBody = new Class({
3535

3636
function StaticBody (world, gameObject)
3737
{
38-
var width = (gameObject.width) ? gameObject.width : 64;
39-
var height = (gameObject.height) ? gameObject.height : 64;
38+
var width = (gameObject.displayWidth) ? gameObject.displayWidth : 64;
39+
var height = (gameObject.displayHeight) ? gameObject.displayHeight : 64;
4040

4141
/**
4242
* The Arcade Physics simulation this Static Body belongs to.
@@ -95,8 +95,8 @@ var StaticBody = new Class({
9595
this.isCircle = false;
9696

9797
/**
98-
* If this Static Body is circular, this is the unscaled radius of the Static Body's boundary, as set by {@link #setCircle}, in source pixels.
99-
* The true radius is equal to `halfWidth`.
98+
* If this Static Body is circular, this is the radius of the boundary, as set by {@link Phaser.Physics.Arcade.StaticBody#setCircle}, in pixels.
99+
* Equal to `halfWidth`.
100100
*
101101
* @name Phaser.Physics.Arcade.StaticBody#radius
102102
* @type {number}
@@ -106,12 +106,13 @@ var StaticBody = new Class({
106106
this.radius = 0;
107107

108108
/**
109-
* The offset of this Static Body's actual position from any updated position.
109+
* The offset set by {@link Phaser.Physics.Arcade.StaticBody#setCircle} or {@link Phaser.Physics.Arcade.StaticBody#setSize}.
110110
*
111-
* Unlike a dynamic Body, a Static Body does not follow its Game Object. As such, this offset is only applied when resizing the Static Body.
111+
* This doesn't affect the Static Body's position, because a Static Body does not follow its Game Object.
112112
*
113113
* @name Phaser.Physics.Arcade.StaticBody#offset
114114
* @type {Phaser.Math.Vector2}
115+
* @readonly
115116
* @since 3.0.0
116117
*/
117118
this.offset = new Vector2();
@@ -173,7 +174,7 @@ var StaticBody = new Class({
173174
* @type {Phaser.Math.Vector2}
174175
* @since 3.0.0
175176
*/
176-
this.center = new Vector2(gameObject.x + this.halfWidth, gameObject.y + this.halfHeight);
177+
this.center = new Vector2(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
177178

178179
/**
179180
* A constant zero velocity used by the Arcade Physics simulation for calculations.
@@ -389,7 +390,7 @@ var StaticBody = new Class({
389390
this.physicsType = CONST.STATIC_BODY;
390391

391392
/**
392-
* The calculated change in the Body's horizontal position during the current step.
393+
* The calculated change in the Static Body's horizontal position during the current step.
393394
* For a static body this is always zero.
394395
*
395396
* @name Phaser.Physics.Arcade.StaticBody#_dx
@@ -401,7 +402,7 @@ var StaticBody = new Class({
401402
this._dx = 0;
402403

403404
/**
404-
* The calculated change in the Body's vertical position during the current step.
405+
* The calculated change in the Static Body's vertical position during the current step.
405406
* For a static body this is always zero.
406407
*
407408
* @name Phaser.Physics.Arcade.StaticBody#_dy
@@ -450,7 +451,7 @@ var StaticBody = new Class({
450451
},
451452

452453
/**
453-
* Syncs the Body's position and size with its parent Game Object.
454+
* Syncs the Static Body's position and size with its parent Game Object.
454455
*
455456
* @method Phaser.Physics.Arcade.StaticBody#updateFromGameObject
456457
* @since 3.1.0
@@ -479,13 +480,13 @@ var StaticBody = new Class({
479480
},
480481

481482
/**
482-
* Sets the offset of the body.
483+
* Positions the Static Body at an offset from its Game Object.
483484
*
484485
* @method Phaser.Physics.Arcade.StaticBody#setOffset
485486
* @since 3.4.0
486487
*
487-
* @param {number} x - The horizontal offset of the Body from the Game Object's center.
488-
* @param {number} y - The vertical offset of the Body from the Game Object's center.
488+
* @param {number} x - The horizontal offset of the Static Body from the Game Object's `x`.
489+
* @param {number} y - The vertical offset of the Static Body from the Game Object's `y`.
489490
*
490491
* @return {Phaser.Physics.Arcade.StaticBody} This Static Body object.
491492
*/
@@ -511,15 +512,16 @@ var StaticBody = new Class({
511512
},
512513

513514
/**
514-
* Sets the size of the body.
515+
* Sets the size of the Static Body.
516+
* When `center` is true, also repositions it.
515517
* Resets the width and height to match current frame, if no width and height provided and a frame is found.
516518
*
517519
* @method Phaser.Physics.Arcade.StaticBody#setSize
518520
* @since 3.0.0
519521
*
520-
* @param {integer} [width] - The width of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame width.
521-
* @param {integer} [height] - The height of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame height.
522-
* @param {boolean} [center=true] - Modify the Body's `offset`, placing the Body's center on its Game Object's center. Only works if the Game Object has the `getCenter` method.
522+
* @param {integer} [width] - The width of the Static Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame width.
523+
* @param {integer} [height] - The height of the Static Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame height.
524+
* @param {boolean} [center=true] - Place the Static Body's center on its Game Object's center. Only works if the Game Object has the `getCenter` method.
523525
*
524526
* @return {Phaser.Physics.Arcade.StaticBody} This Static Body object.
525527
*/
@@ -572,7 +574,7 @@ var StaticBody = new Class({
572574
},
573575

574576
/**
575-
* Sets this Static Body to have a circular body and sets its sizes and position.
577+
* Sets this Static Body to have a circular body and sets its size and position.
576578
*
577579
* @method Phaser.Physics.Arcade.StaticBody#setCircle
578580
* @since 3.0.0

0 commit comments

Comments
 (0)