0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-26 13:49:01 -05:00

added Body.setVertices and Vector.clone

This commit is contained in:
liabru 2014-06-03 15:47:00 +01:00
parent e3e462e64c
commit b6dbb259ab
2 changed files with 43 additions and 0 deletions

View file

@ -134,6 +134,39 @@ var Body = {};
} }
}; };
/**
* Sets the body's vertices and updates body properties accordingly, including inertia, area and mass (with respect to `body.density`).
* Vertices will be automatically transformed to be orientated around their centre of mass as the origin.
* They are then automatically translated to world space based on `body.position`.
*
* The `vertices` argument should be passed as an array of `Matter.Vector` points (or a `Matter.Vertices` array).
* Vertices must form a convex hull, concave hulls are not supported.
*
* @method setVertices
* @param {body} body
* @param {vector[]} vertices
*/
Body.setVertices = function(body, vertices) {
// change vertices
body.vertices = Vertices.create(vertices, body);
// update properties
body.axes = Axes.fromVertices(body.vertices);
body.area = Vertices.area(body.vertices);
body.mass = body.density * body.area;
body.inverseMass = 1 / body.mass;
body.inertia = Vertices.inertia(body.vertices, body.mass);
body.inverseInertia = 1 / body.inertia;
// update geometry
var centre = Vertices.centre(body.vertices);
Vertices.translate(body.vertices, centre, -1);
Vertices.translate(body.vertices, body.position);
Vertices.rotate(body.vertices, body.angle, body.position);
Axes.rotate(body.axes, body.angle);
Bounds.update(body.bounds, body.vertices, body.velocity);
};
/** /**
* Zeroes the `body.force` and `body.torque` force buffers. * Zeroes the `body.force` and `body.torque` force buffers.
* @method resetForcesAll * @method resetForcesAll

View file

@ -15,6 +15,16 @@ var Vector = {};
(function() { (function() {
/**
* Returns a new vector with `x` and `y` copied from the given `vector`.
* @method clone
* @param {vector} vector
* @return {vector} A new cloned vector
*/
Vector.clone = function(vector) {
return { x: vector.x, y: vector.y };
};
/** /**
* Returns the magnitude (length) of a vector. * Returns the magnitude (length) of a vector.
* @method magnitude * @method magnitude