Skip to content

Commit

Permalink
added Body.setCentre, closes #684, closes #461, closes #679
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 18, 2018
1 parent d577477 commit 2ec247b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ var Axes = require('../geometry/Axes');
case 'parts':
Body.setParts(body, value);
break;
case 'centre':
Body.setCentre(body, value);
break;
default:
body[property] = value;

Expand Down Expand Up @@ -426,6 +429,31 @@ var Axes = require('../geometry/Axes');
Body.setPosition(body, total.centre);
};

/**
* Set the centre of mass of the body.
* The `centre` is a vector in world-space unless `relative` is set, in which case it is a translation.
* The centre of mass is the point the body rotates about and can be used to simulate non-uniform density.
* This is equal to moving `body.position` but not the `body.vertices`.
* Invalid if the `centre` falls outside the body's convex hull.
* @method setCentre
* @param {body} body
* @param {vector} centre
* @param {bool} relative
*/
Body.setCentre = function(body, centre, relative) {
if (!relative) {
body.positionPrev.x = centre.x - (body.position.x - body.positionPrev.x);
body.positionPrev.y = centre.y - (body.position.y - body.positionPrev.y);
body.position.x = centre.x;
body.position.y = centre.y;
} else {
body.positionPrev.x += centre.x;
body.positionPrev.y += centre.y;
body.position.x += centre.x;
body.position.y += centre.y;
}
};

/**
* Sets the position of the body instantly. Velocity, angle, force etc. are unchanged.
* @method setPosition
Expand Down

0 comments on commit 2ec247b

Please sign in to comment.