0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

added Body.setCentre, closes #684, closes #461, closes #679

This commit is contained in:
liabru 2018-11-18 09:24:20 +00:00
parent d57747785f
commit 2ec247b7af

View file

@ -232,6 +232,9 @@ var Axes = require('../geometry/Axes');
case 'parts': case 'parts':
Body.setParts(body, value); Body.setParts(body, value);
break; break;
case 'centre':
Body.setCentre(body, value);
break;
default: default:
body[property] = value; body[property] = value;
@ -426,6 +429,31 @@ var Axes = require('../geometry/Axes');
Body.setPosition(body, total.centre); 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. * Sets the position of the body instantly. Velocity, angle, force etc. are unchanged.
* @method setPosition * @method setPosition