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

added Body.scale

This commit is contained in:
liabru 2014-04-23 16:40:48 +01:00
parent 537f0912c3
commit 1f11af9edb
2 changed files with 55 additions and 0 deletions

View file

@ -264,4 +264,32 @@ var Body = {};
Bounds.update(body.bounds, body.vertices, body.velocity); Bounds.update(body.bounds, body.vertices, body.velocity);
}; };
/**
* Scales the body, including updating physical properties (mass, area, axes, inertia), from a point (default is centre)
* @method translate
* @param {body} body
* @param {number} scaleX
* @param {number} scaleY
* @param {vector} point
*/
Body.scale = function(body, scaleX, scaleY, point) {
// scale vertices
Vertices.scale(body.vertices, scaleX, scaleY, point);
// 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;
// update inertia (requires vertices to be at origin)
Vertices.translate(body.vertices, { x: -body.position.x, y: -body.position.y });
body.inertia = Vertices.inertia(body.vertices, body.mass);
body.inverseInertia = 1 / body.inertia;
Vertices.translate(body.vertices, { x: body.position.x, y: body.position.y });
// update bounds
Bounds.update(body.bounds, body.vertices, body.velocity);
};
})(); })();

View file

@ -167,4 +167,31 @@ var Vertices = {};
return true; return true;
}; };
/**
* Scales the vertices from a point (default is centre)
* @method scale
* @param {vertices} vertices
* @param {number} scaleX
* @param {number} scaleY
* @param {vector} point
*/
Vertices.scale = function(vertices, scaleX, scaleY, point) {
if (scaleX === 1 && scaleY === 1)
return vertices;
point = point || Vertices.centre(vertices);
var vertex,
delta;
for (var i = 0; i < vertices.length; i++) {
vertex = vertices[i];
delta = Vector.sub(vertex, point);
vertices[i].x = point.x + delta.x * scaleX;
vertices[i].y = point.y + delta.y * scaleY;
}
return vertices;
};
})(); })();