|
4 | 4 | * @license {@link https://opensource.org/licenses/MIT|MIT License} |
5 | 5 | */ |
6 | 6 |
|
| 7 | +var ALIGN_CONST = require('../../display/align/const'); |
7 | 8 | var Axes = require('./lib/geometry/Axes'); |
8 | 9 | var Bodies = require('./lib/factory/Bodies'); |
9 | 10 | var Body = require('./lib/body/Body'); |
@@ -1052,7 +1053,6 @@ var MatterPhysics = new Class({ |
1052 | 1053 | var body = (bodies[i].hasOwnProperty('body')) ? bodies[i].body : bodies[i]; |
1053 | 1054 |
|
1054 | 1055 | output.push(body); |
1055 | | - |
1056 | 1056 | } |
1057 | 1057 |
|
1058 | 1058 | return output; |
@@ -1304,10 +1304,99 @@ var MatterPhysics = new Class({ |
1304 | 1304 | return DistanceBetween(aX, aY, bX, bY); |
1305 | 1305 | }, |
1306 | 1306 |
|
1307 | | - // alignBody: function (body, x, y, align) |
1308 | | - // { |
| 1307 | + /** |
| 1308 | + * Aligns a Body, or Matter Game Object, against the given coordinates. |
| 1309 | + * |
| 1310 | + * The alignment takes place using the body bounds, which take into consideration things |
| 1311 | + * like body scale and rotation. |
| 1312 | + * |
| 1313 | + * For example, if you wanted to align a body so it sat in the bottom-left of the |
| 1314 | + * Scene, and the world was 800 x 600 in size: |
| 1315 | + * |
| 1316 | + * ```javascript |
| 1317 | + * this.matter.alignBody(body, 0, 600, Phaser.Display.Align.BOTTOM_LEFT); |
| 1318 | + * ``` |
| 1319 | + * |
| 1320 | + * @method Phaser.Physics.Matter.MatterPhysics#alignBody |
| 1321 | + * @since 3.22.0 |
| 1322 | + * |
| 1323 | + * @param {MatterJS.Body} body - The Body to align. |
| 1324 | + * @param {number} x - The horizontal position to align the body to. |
| 1325 | + * @param {number} y - The vertical position to align the body to. |
| 1326 | + * @param {integer} align - One of the `Phaser.Display.Align` constants, such as `Phaser.Display.Align.TOP_LEFT`. |
| 1327 | + * |
| 1328 | + * @return {number} The length of the constraint. |
| 1329 | + */ |
| 1330 | + alignBody: function (body, x, y, align) |
| 1331 | + { |
| 1332 | + body = (body.hasOwnProperty('body')) ? body.body : body; |
1309 | 1333 |
|
1310 | | - // }, |
| 1334 | + var boundsWidth = body.bounds.max.x - body.bounds.min.x; |
| 1335 | + var boundsHeight = body.bounds.max.y - body.bounds.min.y; |
| 1336 | + |
| 1337 | + var boundsCenterX = boundsWidth / 2; |
| 1338 | + var boundsCenterY = boundsHeight / 2; |
| 1339 | + |
| 1340 | + var bodyCenterX = boundsWidth * body.centerOfMass.x; |
| 1341 | + var bodyCenterY = boundsHeight * body.centerOfMass.y; |
| 1342 | + |
| 1343 | + var diffX = bodyCenterX - boundsCenterX; |
| 1344 | + var diffY = bodyCenterY - boundsCenterY; |
| 1345 | + |
| 1346 | + var posX; |
| 1347 | + var posY; |
| 1348 | + |
| 1349 | + switch (align) |
| 1350 | + { |
| 1351 | + case ALIGN_CONST.TOP_LEFT: |
| 1352 | + case ALIGN_CONST.LEFT_TOP: |
| 1353 | + posX = x + boundsCenterX + diffX; |
| 1354 | + posY = y + boundsCenterY + diffY; |
| 1355 | + break; |
| 1356 | + |
| 1357 | + case ALIGN_CONST.TOP_CENTER: |
| 1358 | + posX = x + diffX; |
| 1359 | + posY = y + boundsCenterY + diffY; |
| 1360 | + break; |
| 1361 | + |
| 1362 | + case ALIGN_CONST.TOP_RIGHT: |
| 1363 | + case ALIGN_CONST.RIGHT_TOP: |
| 1364 | + posX = x - (boundsCenterX - diffX); |
| 1365 | + posY = y + boundsCenterY + diffY; |
| 1366 | + break; |
| 1367 | + |
| 1368 | + case ALIGN_CONST.LEFT_CENTER: |
| 1369 | + posX = x + boundsCenterX + diffX; |
| 1370 | + posY = y + diffY; |
| 1371 | + break; |
| 1372 | + |
| 1373 | + case ALIGN_CONST.LEFT_BOTTOM: |
| 1374 | + case ALIGN_CONST.BOTTOM_LEFT: |
| 1375 | + posX = x + boundsCenterX + diffX; |
| 1376 | + posY = y - (boundsCenterY - diffY); |
| 1377 | + break; |
| 1378 | + |
| 1379 | + case ALIGN_CONST.BOTTOM_CENTER: |
| 1380 | + posX = x + diffX; |
| 1381 | + posY = y - (boundsCenterY - diffY); |
| 1382 | + break; |
| 1383 | + |
| 1384 | + case ALIGN_CONST.BOTTOM_RIGHT: |
| 1385 | + case ALIGN_CONST.RIGHT_BOTTOM: |
| 1386 | + posX = x - (boundsCenterX - diffX); |
| 1387 | + posY = y - (boundsCenterY - diffY); |
| 1388 | + break; |
| 1389 | + |
| 1390 | + case ALIGN_CONST.RIGHT_CENTER: |
| 1391 | + posX = x - (boundsCenterX - diffX); |
| 1392 | + posY = y + diffY; |
| 1393 | + break; |
| 1394 | + } |
| 1395 | + |
| 1396 | + Body.setPosition(body, { x: posX, y: posY }); |
| 1397 | + |
| 1398 | + return this; |
| 1399 | + }, |
1311 | 1400 |
|
1312 | 1401 | /** |
1313 | 1402 | * The Scene that owns this plugin is shutting down. |
|
0 commit comments