From 1f11af9edb280155483b911dbacf97c7623cb6f7 Mon Sep 17 00:00:00 2001 From: liabru Date: Wed, 23 Apr 2014 16:40:48 +0100 Subject: [PATCH] added Body.scale --- src/body/Body.js | 28 ++++++++++++++++++++++++++++ src/geometry/Vertices.js | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/body/Body.js b/src/body/Body.js index 6d9d623..f8846f9 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -264,4 +264,32 @@ var Body = {}; 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); + }; + })(); \ No newline at end of file diff --git a/src/geometry/Vertices.js b/src/geometry/Vertices.js index 5afe0cc..ea3a899 100644 --- a/src/geometry/Vertices.js +++ b/src/geometry/Vertices.js @@ -167,4 +167,31 @@ var Vertices = {}; 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; + }; + })(); \ No newline at end of file