From a38b22721c1ae6010b351ce4ae3108b3edcbf861 Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 29 Dec 2015 22:50:24 +0000 Subject: [PATCH] change gravity scale to variable rather than a constant --- src/body/World.js | 51 +++++++++++++++++++++++++++++++++++++++++----- src/core/Engine.js | 8 +++++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/body/World.js b/src/body/World.js index f01b0d9..c1c9dbd 100644 --- a/src/body/World.js +++ b/src/body/World.js @@ -34,7 +34,11 @@ var Common = require('../core/Common'); var defaults = { label: 'World', - gravity: { x: 0, y: 1 }, + gravity: { + x: 0, + y: 1, + scale: 0.001 + }, bounds: { min: { x: -Infinity, y: -Infinity }, max: { x: Infinity, y: Infinity } @@ -44,18 +48,55 @@ var Common = require('../core/Common'); return Common.extend(composite, defaults, options); }; + /* + * + * Properties Documentation + * + */ + + /** + * The gravity to apply on the world. + * + * @property gravity + * @type object + */ + + /** + * The gravity x component. + * + * @property gravity.x + * @type object + * @default 0 + */ + + /** + * The gravity y component. + * + * @property gravity.y + * @type object + * @default 1 + */ + + /** + * The gravity scale factor. + * + * @property gravity.scale + * @type object + * @default 0.001 + */ + // World is a Composite body // see src/module/Outro.js for these aliases: /** - * An alias for Composite.clear since World is also a Composite + * An alias for Composite.clear * @method clear * @param {world} world * @param {boolean} keepStatic */ /** - * An alias for Composite.add since World is also a Composite + * An alias for Composite.add * @method addComposite * @param {world} world * @param {composite} composite @@ -63,7 +104,7 @@ var Common = require('../core/Common'); */ /** - * An alias for Composite.addBody since World is also a Composite + * An alias for Composite.addBody * @method addBody * @param {world} world * @param {body} body @@ -71,7 +112,7 @@ var Common = require('../core/Common'); */ /** - * An alias for Composite.addConstraint since World is also a Composite + * An alias for Composite.addConstraint * @method addConstraint * @param {world} world * @param {constraint} constraint diff --git a/src/core/Engine.js b/src/core/Engine.js index 3315d4a..5040317 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -280,7 +280,9 @@ var Body = require('../body/Body'); * @param {vector} gravity */ var _bodiesApplyGravity = function(bodies, gravity) { - if (gravity.x === 0 && gravity.y === 0) { + var gravityScale = typeof gravity.scale !== 'undefined' ? gravity.scale : 0.001; + + if ((gravity.x === 0 && gravity.y === 0) || gravityScale === 0) { return; } @@ -291,8 +293,8 @@ var Body = require('../body/Body'); continue; // apply gravity - body.force.y += body.mass * gravity.y * 0.001; - body.force.x += body.mass * gravity.x * 0.001; + body.force.y += body.mass * gravity.y * gravityScale; + body.force.x += body.mass * gravity.x * gravityScale; } };