Skip to content

Commit

Permalink
added Body.setVertices and Vector.clone
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jun 3, 2014
1 parent e3e462e commit b6dbb25
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* @method resetForcesAll
Expand Down
10 changes: 10 additions & 0 deletions src/geometry/Vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ var Vector = {};

(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.
* @method magnitude
Expand Down

0 comments on commit b6dbb25

Please sign in to comment.