-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
139 lines (120 loc) · 4.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
const Matter = require('matter-js');
/**
* An attractors plugin for matter.js.
* See the readme for usage and examples.
* @module MatterAttractors
*/
const MatterAttractors = {
// plugin meta
name: 'matter-attractors', // PLUGIN_NAME
version: '0.1.6', // PLUGIN_VERSION
for: 'matter-js@^0.12.0',
// installs the plugin where `base` is `Matter`
// you should not need to call this directly.
install: function(base) {
base.after('Body.create', function() {
MatterAttractors.Body.init(this);
});
base.before('Engine.update', function(engine) {
MatterAttractors.Engine.update(engine);
});
},
Body: {
/**
* Initialises the `body` to support attractors.
* This is called automatically by the plugin.
* @function MatterAttractors.Body.init
* @param {Matter.Body} body The body to init.
* @returns {void} No return value.
*/
init: function(body) {
body.plugin.attractors = body.plugin.attractors || [];
}
},
Engine: {
/**
* Applies all attractors for all bodies in the `engine`.
* This is called automatically by the plugin.
* @function MatterAttractors.Engine.update
* @param {Matter.Engine} engine The engine to update.
* @returns {void} No return value.
*/
update: function(engine) {
let world = engine.world,
bodies = Matter.Composite.allBodies(world);
for (let i = 0; i < bodies.length; i += 1) {
let bodyA = bodies[i],
attractors = bodyA.plugin.attractors;
if (attractors && attractors.length > 0) {
for (let j = i + 1; j < bodies.length; j += 1) {
let bodyB = bodies[j];
for (let k = 0; k < attractors.length; k += 1) {
let attractor = attractors[k],
forceVector = attractor;
if (Matter.Common.isFunction(attractor)) {
forceVector = attractor(bodyA, bodyB);
}
if (forceVector) {
Matter.Body.applyForce(bodyB, bodyB.position, forceVector);
}
}
}
}
}
}
},
/**
* Defines some useful common attractor functions that can be used
* by pushing them to your body's `body.plugin.attractors` array.
* @namespace MatterAttractors.Attractors
* @property {number} gravityConstant The gravitational constant used by the gravity attractor.
*/
Attractors: {
gravityConstant: 0.001,
/**
* An attractor function that applies Newton's law of gravitation.
* Use this by pushing `MatterAttractors.Attractors.gravity` to your body's `body.plugin.attractors` array.
* The gravitational constant defaults to `0.001` which you can change
* at `MatterAttractors.Attractors.gravityConstant`.
* @function MatterAttractors.Attractors.gravity
* @param {Matter.Body} bodyA The first body.
* @param {Matter.Body} bodyB The second body.
* @returns {void} No return value.
*/
gravity: function(bodyA, bodyB) {
// use Newton's law of gravitation
var bToA = Matter.Vector.sub(bodyB.position, bodyA.position),
distanceSq = Matter.Vector.magnitudeSquared(bToA) || 0.0001,
normal = Matter.Vector.normalise(bToA),
magnitude = -MatterAttractors.Attractors.gravityConstant * (bodyA.mass * bodyB.mass / distanceSq),
force = Matter.Vector.mult(normal, magnitude);
// to apply forces to both bodies
Matter.Body.applyForce(bodyA, bodyA.position, Matter.Vector.neg(force));
Matter.Body.applyForce(bodyB, bodyB.position, force);
}
}
};
Matter.Plugin.register(MatterAttractors);
module.exports = MatterAttractors;
/**
* @namespace Matter.Body
* @see http://brm.io/matter-js/docs/classes/Body.html
*/
/**
* This plugin adds a new property `body.plugin.attractors` to instances of `Matter.Body`.
* This is an array of callback functions that will be called automatically
* for every pair of bodies, on every engine update.
* @property {Function[]} body.plugin.attractors
* @memberof Matter.Body
*/
/**
* An attractor function calculates the force to be applied
* to `bodyB`, it should either:
* - return the force vector to be applied to `bodyB`
* - or apply the force to the body(s) itself
* @callback AttractorFunction
* @param {Matter.Body} bodyA
* @param {Matter.Body} bodyB
* @returns {Vector|undefined} a force vector (optional)
*/