Skip to content

Commit

Permalink
changed Vertices.create and Vertices.fromPath to create and return a …
Browse files Browse the repository at this point in the history
…new array rather than apply in-place
  • Loading branch information
liabru committed Jun 3, 2014
1 parent 3a16f90 commit e3e462e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var Body = {};
body.render.strokeStyle = body.render.strokeStyle || Common.shadeColor(body.render.fillStyle, -20);

// update geometry
Vertices.create(body.vertices, body);
body.vertices = Vertices.create(body.vertices, body);
var centre = Vertices.centre(body.vertices);
Vertices.translate(body.vertices, body.position);
Vertices.translate(body.vertices, centre, -1);
Expand Down
39 changes: 27 additions & 12 deletions src/geometry/Vertices.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,53 @@ var Vertices = {};

/**
* Creates a new set of `Matter.Body` compatible vertices.
* The `vertices` argument accepts an array of `Matter.Vector` orientated around the origin `(0, 0)`, for example:
* The `points` argument accepts an array of `Matter.Vector` points orientated around the origin `(0, 0)`, for example:
*
* [{ x: 0, y: 0 }, { x: 25, y: 50 }, { x: 50, y: 0 }]
*
* The `Vertices.create` method then inserts additional indexing properties required for efficient collision detection routines.
* The `Vertices.create` method returns a new array of vertices, which are similar to Matter.Vector objects,
* but with some additional references required for efficient collision detection routines.
*
* Note that the `body` argument is not optional, a `Matter.Body` reference must be provided.
*
* @method create
* @param {vertices} vertices
* @param {vector[]} points
* @param {body} body
*/
Vertices.create = function(vertices, body) {
for (var i = 0; i < vertices.length; i++) {
vertices[i].index = i;
vertices[i].body = body;
Vertices.create = function(points, body) {
var vertices = [];

for (var i = 0; i < points.length; i++) {
var point = points[i],
vertex = {};

vertex.x = point.x;
vertex.y = point.y;
vertex.index = i;
vertex.body = body;

vertices.push(vertex);
}

return vertices;
};

/**
* Parses a _simple_ SVG-style path into a set of `Matter.Vector` points.
* Parses a _simple_ SVG-style path into a `Matter.Vertices` object for the given `Matter.Body`.
* @method fromPath
* @param {string} path
* @param {body} body
* @return {vertices} vertices
*/
Vertices.fromPath = function(path) {
Vertices.fromPath = function(path, body) {
var pathPattern = /L\s*([\-\d\.]*)\s*([\-\d\.]*)/ig,
vertices = [];
points = [];

path.replace(pathPattern, function(match, x, y) {
vertices.push({ x: parseFloat(x, 10), y: parseFloat(y, 10) });
points.push({ x: parseFloat(x, 10), y: parseFloat(y, 10) });
});

return vertices;
return Vertices.create(points, body);
};

/**
Expand Down

0 comments on commit e3e462e

Please sign in to comment.