Skip to content

Commit

Permalink
fix Body.scale for compound bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 25, 2017
1 parent f7d1877 commit 50a89d0
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
};

/**
Expand Down

0 comments on commit 50a89d0

Please sign in to comment.