Skip to content

Commit 1d3616f

Browse files
committed
Do not invert (physics) shape size data for circle, rectangle, capsuel & line phaserjs#445
1 parent 812de7d commit 1d3616f

4 files changed

Lines changed: 78 additions & 26 deletions

File tree

examples/wip/cannon.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function fire() {
7575
bullet.position.set(cannon.x, cannon.y);
7676
bullet.physicsEnabled = true;
7777
bullet.body.collideWorldBounds = false;
78-
bullet.body.rotation = cannon.rotation + game.math.degToRad(90);
78+
bullet.body.rotation = cannon.rotation - game.math.degToRad(90);
7979

8080
var magnitude = game.math.px2p(-500);
8181
var angle = bullet.body.rotation + Math.PI / 2;

src/math/Math.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,9 @@ Phaser.Math = {
12991299
* @return {number} The scaled value.
13001300
*/
13011301
p2px: function (v) {
1302-
return v *= -20;
1302+
1303+
return v *= 20;
1304+
13031305
},
13041306

13051307
/**
@@ -1310,7 +1312,35 @@ Phaser.Math = {
13101312
* @return {number} The scaled value.
13111313
*/
13121314
px2p: function (v) {
1315+
1316+
return v * 0.05;
1317+
1318+
},
1319+
1320+
/**
1321+
* Convert p2 physics value (meters) to pixel scale and inverses it.
1322+
*
1323+
* @method Phaser.Math#p2pxi
1324+
* @param {number} v - The value to convert.
1325+
* @return {number} The scaled value.
1326+
*/
1327+
p2pxi: function (v) {
1328+
1329+
return v *= -20;
1330+
1331+
},
1332+
1333+
/**
1334+
* Convert pixel value to p2 physics scale (meters) and inverses it.
1335+
*
1336+
* @method Phaser.Math#px2pi
1337+
* @param {number} v - The value to convert.
1338+
* @return {number} The scaled value.
1339+
*/
1340+
px2pi: function (v) {
1341+
13131342
return v * -0.05;
1343+
13141344
},
13151345

13161346
/**

src/physics/Body.js

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
4545
* @property {p2.Body} data - The p2 Body data.
4646
* @protected
4747
*/
48-
this.data = new p2.Body({ position:[this.px2p(x), this.px2p(y)], mass: mass });
48+
this.data = new p2.Body({ position:[this.px2pi(x), this.px2pi(y)], mass: mass });
4949
this.data.parent = this;
5050

5151
/**
@@ -419,7 +419,7 @@ Phaser.Physics.Body.prototype = {
419419
*/
420420
rotateLeft: function (speed) {
421421

422-
this.data.angularVelocity = this.px2p(speed);
422+
this.data.angularVelocity = this.px2p(-speed);
423423

424424
},
425425

@@ -431,7 +431,7 @@ Phaser.Physics.Body.prototype = {
431431
*/
432432
rotateRight: function (speed) {
433433

434-
this.data.angularVelocity = this.px2p(-speed);
434+
this.data.angularVelocity = this.px2p(speed);
435435

436436
},
437437

@@ -444,7 +444,7 @@ Phaser.Physics.Body.prototype = {
444444
*/
445445
moveForward: function (speed) {
446446

447-
var magnitude = this.px2p(-speed);
447+
var magnitude = this.px2pi(-speed);
448448
var angle = this.data.angle + Math.PI / 2;
449449

450450
this.data.velocity[0] = magnitude * Math.cos(angle);
@@ -461,7 +461,7 @@ Phaser.Physics.Body.prototype = {
461461
*/
462462
moveBackward: function (speed) {
463463

464-
var magnitude = this.px2p(-speed);
464+
var magnitude = this.px2pi(-speed);
465465
var angle = this.data.angle + Math.PI / 2;
466466

467467
this.data.velocity[0] = -(magnitude * Math.cos(angle));
@@ -478,7 +478,7 @@ Phaser.Physics.Body.prototype = {
478478
*/
479479
thrust: function (speed) {
480480

481-
var magnitude = this.px2p(-speed);
481+
var magnitude = this.px2pi(-speed);
482482
var angle = this.data.angle + Math.PI / 2;
483483

484484
this.data.force[0] += magnitude * Math.cos(angle);
@@ -495,7 +495,7 @@ Phaser.Physics.Body.prototype = {
495495
*/
496496
reverse: function (speed) {
497497

498-
var magnitude = this.px2p(-speed);
498+
var magnitude = this.px2pi(-speed);
499499
var angle = this.data.angle + Math.PI / 2;
500500

501501
this.data.force[0] -= magnitude * Math.cos(angle);
@@ -512,7 +512,7 @@ Phaser.Physics.Body.prototype = {
512512
*/
513513
moveLeft: function (speed) {
514514

515-
this.data.velocity[0] = this.px2p(-speed);
515+
this.data.velocity[0] = this.px2pi(-speed);
516516

517517
},
518518

@@ -525,7 +525,7 @@ Phaser.Physics.Body.prototype = {
525525
*/
526526
moveRight: function (speed) {
527527

528-
this.data.velocity[0] = this.px2p(speed);
528+
this.data.velocity[0] = this.px2pi(speed);
529529

530530
},
531531

@@ -538,7 +538,7 @@ Phaser.Physics.Body.prototype = {
538538
*/
539539
moveUp: function (speed) {
540540

541-
this.data.velocity[1] = this.px2p(-speed);
541+
this.data.velocity[1] = this.px2pi(-speed);
542542

543543
},
544544

@@ -551,7 +551,7 @@ Phaser.Physics.Body.prototype = {
551551
*/
552552
moveDown: function (speed) {
553553

554-
this.data.velocity[1] = this.px2p(speed);
554+
this.data.velocity[1] = this.px2pi(speed);
555555

556556
},
557557

@@ -572,8 +572,8 @@ Phaser.Physics.Body.prototype = {
572572
*/
573573
postUpdate: function () {
574574

575-
this.sprite.x = this.p2px(this.data.position[0]);
576-
this.sprite.y = this.p2px(this.data.position[1]);
575+
this.sprite.x = this.p2pxi(this.data.position[0]);
576+
this.sprite.y = this.p2pxi(this.data.position[1]);
577577

578578
if (!this.fixedRotation)
579579
{
@@ -695,7 +695,7 @@ Phaser.Physics.Body.prototype = {
695695
if (typeof offsetY === 'undefined') { offsetY = 0; }
696696
if (typeof rotation === 'undefined') { rotation = 0; }
697697

698-
this.data.addShape(shape, [this.px2p(offsetX), this.px2p(offsetY)], rotation);
698+
this.data.addShape(shape, [this.px2pi(offsetX), this.px2pi(offsetY)], rotation);
699699

700700
return shape;
701701

@@ -950,10 +950,6 @@ Phaser.Physics.Body.prototype = {
950950

951951
if (typeof sprite === 'undefined') { sprite = this.sprite; }
952952

953-
// because Sprite.phyicsEnabled = true now sets anchor to 0.5
954-
// var px = (sprite.width / 2) + (-sprite.width * sprite.anchor.x);
955-
// var py = (sprite.height / 2) + (-sprite.height * sprite.anchor.y);
956-
957953
this.clearShapes();
958954

959955
return this.addRectangle(sprite.width, sprite.height, 0, 0, sprite.rotation);
@@ -1046,25 +1042,51 @@ Phaser.Physics.Body.prototype = {
10461042
/**
10471043
* Convert p2 physics value (meters) to pixel scale.
10481044
*
1049-
* @method Phaser.Math#p2px
1045+
* @method Phaser.Physics.Body#p2px
10501046
* @param {number} v - The value to convert.
10511047
* @return {number} The scaled value.
10521048
*/
10531049
p2px: function (v) {
10541050

1055-
return v *= -20;
1051+
return v *= 20;
10561052

10571053
},
10581054

10591055
/**
10601056
* Convert pixel value to p2 physics scale (meters).
10611057
*
1062-
* @method Phaser.Math#px2p
1058+
* @method Phaser.Physics.Body#px2p
10631059
* @param {number} v - The value to convert.
10641060
* @return {number} The scaled value.
10651061
*/
10661062
px2p: function (v) {
10671063

1064+
return v * 0.05;
1065+
1066+
},
1067+
1068+
/**
1069+
* Convert p2 physics value (meters) to pixel scale and inverses it.
1070+
*
1071+
* @method Phaser.Physics.Body#p2pxi
1072+
* @param {number} v - The value to convert.
1073+
* @return {number} The scaled value.
1074+
*/
1075+
p2pxi: function (v) {
1076+
1077+
return v *= -20;
1078+
1079+
},
1080+
1081+
/**
1082+
* Convert pixel value to p2 physics scale (meters) and inverses it.
1083+
*
1084+
* @method Phaser.Physics.Body#px2pi
1085+
* @param {number} v - The value to convert.
1086+
* @return {number} The scaled value.
1087+
*/
1088+
px2pi: function (v) {
1089+
10681090
return v * -0.05;
10691091

10701092
}

src/physics/World.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,12 @@ Phaser.Physics.World.prototype = {
373373
this.bounds.removeShape(shape);
374374
}
375375

376-
this.bounds.position[0] = this.game.math.px2p(cx);
377-
this.bounds.position[1] = this.game.math.px2p(cy);
376+
this.bounds.position[0] = this.game.math.px2pi(cx);
377+
this.bounds.position[1] = this.game.math.px2pi(cy);
378378
}
379379
else
380380
{
381-
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2p(cx), this.game.math.px2p(cy)] });
381+
this.bounds = new p2.Body({ mass: 0, position:[this.game.math.px2pi(cx), this.game.math.px2pi(cy)] });
382382
}
383383

384384
if (left)

0 commit comments

Comments
 (0)