|
1 | 1 | /** |
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 |
9 | 8 | */ |
10 | 9 |
|
11 | 10 | var Body = {}; |
@@ -100,8 +99,8 @@ var Axes = require('../geometry/Axes'); |
100 | 99 | }, |
101 | 100 | gameObject: null, // custom Phaser property |
102 | 101 | 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) |
105 | 104 | gravityScale: { x: 1, y: 1 }, // custom Phaser property |
106 | 105 | ignoreGravity: false, // custom Phaser property |
107 | 106 | ignorePointer: false, // custom Phaser property |
@@ -192,17 +191,18 @@ var Axes = require('../geometry/Axes'); |
192 | 191 |
|
193 | 192 | var bounds = body.bounds; |
194 | 193 | var centerOfMass = body.centerOfMass; |
| 194 | + var centerOffset = body.centerOffset; |
195 | 195 |
|
196 | 196 | Bounds.update(bounds, body.vertices, body.velocity); |
197 | 197 |
|
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; |
200 | 200 |
|
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; |
203 | 203 |
|
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; |
206 | 206 |
|
207 | 207 | Vertices.rotate(body.vertices, body.angle, body.position); |
208 | 208 | Axes.rotate(body.axes, body.angle); |
@@ -465,21 +465,15 @@ var Axes = require('../geometry/Axes'); |
465 | 465 |
|
466 | 466 | var bounds = body.bounds; |
467 | 467 | var centerOfMass = body.centerOfMass; |
| 468 | + var centerOffset = body.centerOffset; |
468 | 469 |
|
469 | 470 | Bounds.update(bounds, body.vertices, body.velocity); |
470 | 471 |
|
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); |
479 | 474 |
|
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; |
483 | 477 |
|
484 | 478 | body.area = total.area; |
485 | 479 | body.parent = body; |
|
0 commit comments