Skip to content

Commit

Permalink
added Body.setMass, Body.setDensity, Body.setInertia
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jun 21, 2014
1 parent c6a9414 commit ca978fa
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ var Body = {};
// allow options to override the automatically calculated properties
body.axes = options.axes || body.axes;
body.area = options.area || body.area;
body.mass = options.mass || body.mass;
body.inertia = options.inertia || body.inertia;
body.inverseMass = 1 / body.mass;
body.inverseInertia = 1 / body.inertia;
Body.setMass(body, options.mass || body.mass);
Body.setInertia(body, options.inertia || body.inertia);

// render properties
var defaultFillStyle = (body.isStatic ? '#eeeeee' : Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58'])),
Expand Down Expand Up @@ -139,6 +137,41 @@ var Body = {};
}
};

/**
* Sets the mass of the body. Inverse mass and density are automatically updated to reflect the change.
* @method setMass
* @param {body} body
* @param {number} mass
*/
Body.setMass = function(body, mass) {
body.mass = mass;
body.inverseMass = 1 / body.mass;
body.density = body.mass / body.area;
};

/**
* Sets the density of the body. Mass is automatically updated to reflect the change.
* @method setDensity
* @param {body} body
* @param {number} density
*/
Body.setDensity = function(body, density) {
Body.setMass(body, density * body.area);
body.density = density;
};

/**
* Sets the moment of inertia (i.e. second moment of area) of the body of the body.
* Inverse inertia is automatically updated to reflect the change. Mass is not changed.
* @method setInertia
* @param {body} body
* @param {number} inertia
*/
Body.setInertia = function(body, inertia) {
body.inertia = inertia;
body.inverseInertia = 1 / body.inertia;
};

/**
* Sets the body's vertices and updates body properties accordingly, including inertia, area and mass (with respect to `body.density`).
* Vertices will be automatically transformed to be orientated around their centre of mass as the origin.
Expand All @@ -162,16 +195,14 @@ var Body = {};
// 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;
Body.setMass(body, body.density * body.area);

// orient vertices around the centre of mass at origin (0, 0)
var centre = Vertices.centre(body.vertices);
Vertices.translate(body.vertices, centre, -1);

// update inertia while vertices are at origin (0, 0)
body.inertia = Body._inertiaScale * Vertices.inertia(body.vertices, body.mass);
body.inverseInertia = 1 / body.inertia;
Body.setInertia(body, Body._inertiaScale * Vertices.inertia(body.vertices, body.mass));

// update geometry
Vertices.translate(body.vertices, body.position);
Expand Down Expand Up @@ -274,13 +305,11 @@ var Body = {};
// 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;
Body.setMass(body, body.density * body.area);

// 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;
Body.setInertia(body, Vertices.inertia(body.vertices, body.mass));
Vertices.translate(body.vertices, { x: body.position.x, y: body.position.y });

// update bounds
Expand Down

0 comments on commit ca978fa

Please sign in to comment.