From 50a89d0217f951f221801a3ed1a0f79d250402e9 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 25 Nov 2017 14:16:38 +0000 Subject: [PATCH] fix Body.scale for compound bodies --- src/body/Body.js | 44 ++++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/src/body/Body.js b/src/body/Body.js index 59a4f3d8..fcb18c1c 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -525,35 +525,50 @@ var Axes = require('../geometry/Axes'); * @param {vector} [point] */ Body.scale = function(body, scaleX, scaleY, point) { + var totalArea = 0, + totalInertia = 0; + point = point || body.position; for (var i = 0; i < body.parts.length; i++) { var part = body.parts[i]; - // scale position - part.position.x = point.x + (part.position.x - point.x) * scaleX; - part.position.y = point.y + (part.position.y - point.y) * scaleY; - // scale vertices Vertices.scale(part.vertices, scaleX, scaleY, point); // update properties part.axes = Axes.fromVertices(part.vertices); + part.area = Vertices.area(part.vertices); + Body.setMass(part, body.density * part.area); - if (!body.isStatic) { - part.area = Vertices.area(part.vertices); - Body.setMass(part, body.density * part.area); + // update inertia (requires vertices to be at origin) + Vertices.translate(part.vertices, { x: -part.position.x, y: -part.position.y }); + Body.setInertia(part, Body._inertiaScale * Vertices.inertia(part.vertices, part.mass)); + Vertices.translate(part.vertices, { x: part.position.x, y: part.position.y }); - // update inertia (requires vertices to be at origin) - Vertices.translate(part.vertices, { x: -part.position.x, y: -part.position.y }); - Body.setInertia(part, Vertices.inertia(part.vertices, part.mass)); - Vertices.translate(part.vertices, { x: part.position.x, y: part.position.y }); + if (i > 0) { + totalArea += part.area; + totalInertia += part.inertia; } + // scale position + part.position.x = point.x + (part.position.x - point.x) * scaleX; + part.position.y = point.y + (part.position.y - point.y) * scaleY; + // update bounds Bounds.update(part.bounds, part.vertices, body.velocity); } + // handle parent body + if (body.parts.length > 1) { + body.area = totalArea; + + if (!body.isStatic) { + Body.setMass(body, body.density * totalArea); + Body.setInertia(body, totalInertia); + } + } + // handle circles if (body.circleRadius) { if (scaleX === scaleY) { @@ -563,13 +578,6 @@ var Axes = require('../geometry/Axes'); body.circleRadius = null; } } - - if (!body.isStatic) { - var total = _totalProperties(body); - body.area = total.area; - Body.setMass(body, total.mass); - Body.setInertia(body, total.inertia); - } }; /**