Skip to content

Commit 887227a

Browse files
committed
Added centerOffset back and fixed calculations
1 parent 6c947c9 commit 887227a

1 file changed

Lines changed: 20 additions & 26 deletions

File tree

  • src/physics/matter-js/lib/body

src/physics/matter-js/lib/body/Body.js

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* The `Matter.Body` module contains methods for creating and manipulating body models.
3-
* A `Matter.Body` is a rigid body that can be simulated by a `Matter.Engine`.
4-
* Factories for commonly used body configurations (such as rectangles, circles and other polygons) can be found in the module `Matter.Bodies`.
5-
*
6-
* See the included usage [examples](https://github.com/liabru/matter-js/tree/master/examples).
7-
8-
* @class Body
2+
* The `Matter.Body` module contains methods for creating and manipulating body models.
3+
* A `Matter.Body` is a rigid body that can be simulated by a `Matter.Engine`.
4+
* Factories for commonly used body configurations (such as rectangles, circles and other polygons) can be found in the module `Matter.Bodies`.
5+
*
6+
* See the included usage [examples](https://github.com/liabru/matter-js/tree/master/examples).
7+
* @class Body
98
*/
109

1110
var Body = {};
@@ -100,8 +99,8 @@ var Axes = require('../geometry/Axes');
10099
},
101100
gameObject: null, // custom Phaser property
102101
scale: { x: 1, y: 1 }, // custom Phaser property
103-
centerOfMass: { x: 0, y: 0 }, // custom Phaser property
104-
centerOffset: { x: 0, y: 0 }, // custom Phaser property
102+
centerOfMass: { x: 0, y: 0 }, // custom Phaser property (float, 0 - 1)
103+
centerOffset: { x: 0, y: 0 }, // custom Phaser property (pixel values)
105104
gravityScale: { x: 1, y: 1 }, // custom Phaser property
106105
ignoreGravity: false, // custom Phaser property
107106
ignorePointer: false, // custom Phaser property
@@ -192,17 +191,18 @@ var Axes = require('../geometry/Axes');
192191

193192
var bounds = body.bounds;
194193
var centerOfMass = body.centerOfMass;
194+
var centerOffset = body.centerOffset;
195195

196196
Bounds.update(bounds, body.vertices, body.velocity);
197197

198-
var boundsWidth = bounds.max.x - bounds.min.x;
199-
var boundsHeight = bounds.max.y - bounds.min.y;
198+
var bodyWidth = bounds.max.x - bounds.min.x;
199+
var bodyHeight = bounds.max.y - bounds.min.y;
200200

201-
centerOfMass.x = -(bounds.min.x - body.position.x) / boundsWidth;
202-
centerOfMass.y = -(bounds.min.y - body.position.y) / boundsHeight;
201+
centerOfMass.x = -(bounds.min.x - body.position.x) / bodyWidth;
202+
centerOfMass.y = -(bounds.min.y - body.position.y) / bodyHeight;
203203

204-
body.centerOffset.x = (boundsWidth * centerOfMass.x) - (boundsWidth / 2);
205-
body.centerOffset.y = (boundsHeight * centerOfMass.y) - (boundsHeight / 2);
204+
centerOffset.x = body.position.x;
205+
centerOffset.y = body.position.y;
206206

207207
Vertices.rotate(body.vertices, body.angle, body.position);
208208
Axes.rotate(body.axes, body.angle);
@@ -465,21 +465,15 @@ var Axes = require('../geometry/Axes');
465465

466466
var bounds = body.bounds;
467467
var centerOfMass = body.centerOfMass;
468+
var centerOffset = body.centerOffset;
468469

469470
Bounds.update(bounds, body.vertices, body.velocity);
470471

471-
var boundsWidth = bounds.max.x - bounds.min.x;
472-
var boundsHeight = bounds.max.y - bounds.min.y;
473-
474-
centerOfMass.x = -(bounds.min.x - cx) / boundsWidth;
475-
centerOfMass.y = -(bounds.min.y - cy) / boundsHeight;
476-
477-
body.centerOffset.x = (boundsWidth * centerOfMass.x) - (boundsWidth / 2);
478-
body.centerOffset.y = (boundsHeight * centerOfMass.y) - (boundsHeight / 2);
472+
centerOfMass.x = -(bounds.min.x - cx) / (bounds.max.x - bounds.min.x);
473+
centerOfMass.y = -(bounds.min.y - cy) / (bounds.max.y - bounds.min.y);
479474

480-
console.log('setParts1-com', body.centerOfMass.x, body.centerOfMass.y);
481-
console.log('setParts2-off', body.centerOffset.x, body.centerOffset.y);
482-
console.log('setParts3-bwh', boundsWidth, boundsHeight);
475+
centerOffset.x = cx;
476+
centerOffset.y = cy;
483477

484478
body.area = total.area;
485479
body.parent = body;

0 commit comments

Comments
 (0)