Skip to content

Commit

Permalink
change gravity scale to variable rather than a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Dec 29, 2015
1 parent 5ff2182 commit a38b227
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
51 changes: 46 additions & 5 deletions src/body/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -44,34 +48,71 @@ 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
* @return {world} The original world with the objects from composite added
*/

/**
* An alias for Composite.addBody since World is also a Composite
* An alias for Composite.addBody
* @method addBody
* @param {world} world
* @param {body} body
* @return {world} The original world with the body added
*/

/**
* An alias for Composite.addConstraint since World is also a Composite
* An alias for Composite.addConstraint
* @method addConstraint
* @param {world} world
* @param {constraint} constraint
Expand Down
8 changes: 5 additions & 3 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
};

Expand Down

0 comments on commit a38b227

Please sign in to comment.