mirror of
https://github.com/liabru/matter-js.git
synced 2025-01-12 16:08:50 -05:00
changed Body._initProperties to use Body.setVertices
This commit is contained in:
parent
b6dbb259ab
commit
2d04389c84
1 changed files with 23 additions and 22 deletions
|
@ -62,7 +62,7 @@ var Body = {};
|
||||||
|
|
||||||
var body = Common.extend(defaults, options);
|
var body = Common.extend(defaults, options);
|
||||||
|
|
||||||
_initProperties(body);
|
_initProperties(body, options);
|
||||||
|
|
||||||
return body;
|
return body;
|
||||||
};
|
};
|
||||||
|
@ -81,37 +81,38 @@ var Body = {};
|
||||||
* @method _initProperties
|
* @method _initProperties
|
||||||
* @private
|
* @private
|
||||||
* @param {body} body
|
* @param {body} body
|
||||||
|
* @param {} options
|
||||||
*/
|
*/
|
||||||
var _initProperties = function(body) {
|
var _initProperties = function(body, options) {
|
||||||
// calculated properties
|
// init required properties
|
||||||
body.axes = body.axes || Axes.fromVertices(body.vertices);
|
body.bounds = body.bounds || Bounds.create(body.vertices);
|
||||||
body.area = Vertices.area(body.vertices);
|
body.positionPrev = body.positionPrev || Vector.clone(body.position);
|
||||||
body.bounds = Bounds.create(body.vertices);
|
|
||||||
body.mass = body.mass || (body.density * body.area);
|
|
||||||
body.inverseMass = 1 / body.mass;
|
|
||||||
body.inertia = body.inertia || Vertices.inertia(body.vertices, body.mass);
|
|
||||||
body.inverseInertia = 1 / body.inertia;
|
|
||||||
body.positionPrev = body.positionPrev || { x: body.position.x, y: body.position.y };
|
|
||||||
body.anglePrev = body.anglePrev || body.angle;
|
body.anglePrev = body.anglePrev || body.angle;
|
||||||
body.render.fillStyle = body.render.fillStyle || (body.isStatic ? '#eeeeee' : Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58']));
|
|
||||||
body.render.strokeStyle = body.render.strokeStyle || Common.shadeColor(body.render.fillStyle, -20);
|
|
||||||
|
|
||||||
// update geometry
|
|
||||||
body.vertices = Vertices.create(body.vertices, body);
|
|
||||||
var centre = Vertices.centre(body.vertices);
|
|
||||||
Vertices.translate(body.vertices, body.position);
|
|
||||||
Vertices.translate(body.vertices, centre, -1);
|
|
||||||
Vertices.rotate(body.vertices, body.angle, body.position);
|
|
||||||
Axes.rotate(body.axes, body.angle);
|
|
||||||
Bounds.update(body.bounds, body.vertices, body.velocity);
|
|
||||||
|
|
||||||
|
// must use setters for the more complicated properties
|
||||||
|
Body.setVertices(body, body.vertices);
|
||||||
Body.setStatic(body, body.isStatic);
|
Body.setStatic(body, body.isStatic);
|
||||||
Sleeping.set(body, body.isSleeping);
|
Sleeping.set(body, body.isSleeping);
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
// render properties
|
||||||
|
var defaultFillStyle = (body.isStatic ? '#eeeeee' : Common.choose(['#556270', '#4ECDC4', '#C7F464', '#FF6B6B', '#C44D58'])),
|
||||||
|
defaultStrokeStyle = Common.shadeColor(defaultFillStyle, -20);
|
||||||
|
body.render.fillStyle = body.render.fillStyle || defaultFillStyle;
|
||||||
|
body.render.strokeStyle = body.render.strokeStyle || defaultStrokeStyle;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the body as static, including isStatic flag and setting mass and inertia to Infinity.
|
* Sets the body as static, including isStatic flag and setting mass and inertia to Infinity.
|
||||||
* @method setStatic
|
* @method setStatic
|
||||||
|
* @param {body} body
|
||||||
* @param {bool} isStatic
|
* @param {bool} isStatic
|
||||||
*/
|
*/
|
||||||
Body.setStatic = function(body, isStatic) {
|
Body.setStatic = function(body, isStatic) {
|
||||||
|
|
Loading…
Reference in a new issue