mirror of
https://github.com/liabru/matter-js.git
synced 2025-02-12 19:34:57 -05:00
allows gravity to be a point rather than a direction
This commit is contained in:
parent
2ab20b0656
commit
02489ea7de
1 changed files with 35 additions and 11 deletions
|
@ -294,12 +294,36 @@ var Engine = {};
|
|||
if (body.isStatic || body.isSleeping)
|
||||
continue;
|
||||
|
||||
if (gravity.isPoint === true) {
|
||||
gravity = _calculateGravity(gravity, body.position);
|
||||
}
|
||||
|
||||
// apply gravity
|
||||
body.force.y += body.mass * gravity.y * 0.001;
|
||||
body.force.x += body.mass * gravity.x * 0.001;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Caluclates gravity based on point and body position
|
||||
* @method calculateGravity
|
||||
* @private
|
||||
* @param {vector} point
|
||||
* @param {vector} body position
|
||||
* @return {vector} normalized gravity vector
|
||||
*/
|
||||
var _calculateGravity = function(point, position) {
|
||||
var x = point.x - position.x;
|
||||
var y = point.y - position.y;
|
||||
|
||||
var length = Math.sqrt( x * x + y * y );
|
||||
|
||||
return {
|
||||
x: x / length,
|
||||
y: y / length
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Applys `Body.update` to all given `bodies`.
|
||||
* @method updateAll
|
||||
|
|
Loading…
Add table
Reference in a new issue