Skip to content

Commit b4a3453

Browse files
committed
refactored plugins
1 parent 4b5837e commit b4a3453

5 files changed

Lines changed: 132 additions & 112 deletions

File tree

examples/attractors.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,9 @@
1111

1212
Matter.use(
1313
'matter-gravity',
14-
'matter-world-wrap'
14+
'matter-wrap'
1515
);
1616

17-
world.bounds = {
18-
min: { x: 0, y: 0 },
19-
max: { x: 800, y: 600 }
20-
};
21-
2217
world.bodies = [];
2318
world.gravity.scale = 0;
2419

@@ -32,7 +27,11 @@
3227
{
3328
mass: Common.random(10, 20),
3429
gravity: G,
35-
frictionAir: 0
30+
frictionAir: 0,
31+
wrap: {
32+
min: { x: 0, y: 0 },
33+
max: { x: 800, y: 600 }
34+
}
3635
}
3736
);
3837

@@ -46,7 +45,6 @@
4645

4746
var renderOptions = demo.render.options;
4847
renderOptions.showAngleIndicator = false;
49-
renderOptions.showPositions = true;
5048
};
5149

5250
})();

examples/attractorsPlugin.js

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,49 @@
1414
install: function(base) {
1515
base.Body.create = Common.chain(
1616
Matter.Body.create,
17-
MatterAttractors.init
17+
function() {
18+
MatterAttractors.Body.init(this);
19+
}
1820
);
1921

2022
base.Engine.update = Common.chain(
2123
Matter.Engine.update,
22-
MatterAttractors.update
24+
function() {
25+
MatterAttractors.Engine.update(this);
26+
}
2327
);
2428
},
2529

26-
init: function(body) {
27-
body = this || body;
28-
body.attractors = body.attractors || [];
30+
Body: {
31+
init: function(body) {
32+
body.attractors = body.attractors || [];
33+
}
2934
},
3035

31-
update: function(engine) {
32-
engine = this || engine;
33-
34-
var world = engine.world,
35-
bodies = Composite.allBodies(world);
36-
37-
for (var i = 0; i < bodies.length; i += 1) {
38-
var bodyA = bodies[i],
39-
attractors = bodyA.attractors;
40-
41-
if (attractors && attractors.length > 0) {
42-
for (var j = i + 1; j < bodies.length; j += 1) {
43-
var bodyB = bodies[j];
44-
45-
for (var k = 0; k < attractors.length; k += 1) {
46-
var attractor = attractors[k],
47-
forceVector = attractor;
48-
49-
if (Common.isFunction(attractor)) {
50-
forceVector = attractor(bodyA, bodyB);
51-
}
52-
53-
if (forceVector) {
54-
Body.applyForce(bodyB, bodyB.position, forceVector);
36+
Engine: {
37+
update: function(engine) {
38+
var world = engine.world,
39+
bodies = Composite.allBodies(world);
40+
41+
for (var i = 0; i < bodies.length; i += 1) {
42+
var bodyA = bodies[i],
43+
attractors = bodyA.attractors;
44+
45+
if (attractors && attractors.length > 0) {
46+
for (var j = i + 1; j < bodies.length; j += 1) {
47+
var bodyB = bodies[j];
48+
49+
for (var k = 0; k < attractors.length; k += 1) {
50+
var attractor = attractors[k],
51+
forceVector = attractor;
52+
53+
if (Common.isFunction(attractor)) {
54+
forceVector = attractor(bodyA, bodyB);
55+
}
56+
57+
if (forceVector) {
58+
Body.applyForce(bodyB, bodyB.position, forceVector);
59+
}
5560
}
5661
}
5762
}

examples/gravityPlugin.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,29 @@
1818
install: function(base) {
1919
base.Body.create = Common.chain(
2020
Matter.Body.create,
21-
MatterGravity.addAttractor
21+
function() {
22+
MatterGravity.Body.init(this);
23+
}
2224
);
2325
},
2426

25-
addAttractor: function(body) {
26-
body = this || body;
27-
28-
if (body.gravity) {
29-
body.attractors.push(MatterGravity.applyForce);
27+
Body: {
28+
init: function(body) {
29+
if (body.gravity) {
30+
body.attractors.push(MatterGravity.Body.applyGravity);
31+
}
32+
},
33+
34+
applyGravity: function(bodyA, bodyB) {
35+
var bToA = Vector.sub(bodyB.position, bodyA.position),
36+
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
37+
normal = Vector.normalise(bToA),
38+
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
39+
force = Vector.mult(normal, magnitude);
40+
41+
Body.applyForce(bodyA, bodyA.position, Vector.neg(force));
42+
Body.applyForce(bodyB, bodyB.position, force);
3043
}
31-
},
32-
33-
applyForce: function(bodyA, bodyB) {
34-
var bToA = Vector.sub(bodyB.position, bodyA.position),
35-
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
36-
normal = Vector.normalise(bToA),
37-
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
38-
force = Vector.mult(normal, magnitude);
39-
40-
Body.applyForce(bodyA, bodyA.position, Vector.neg(force));
41-
Body.applyForce(bodyB, bodyB.position, force);
4244
}
4345
};
4446

examples/worldWrapPlugin.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/wrapPlugin.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
(function() {
2+
3+
var Body = Matter.Body,
4+
Common = Matter.Common,
5+
Composite = Matter.Composite;
6+
7+
var MatterWrap = {
8+
name: 'matter-wrap',
9+
10+
version: '0.1.0',
11+
12+
for: 'matter-js@^0.10.0',
13+
14+
install: function(base) {
15+
base.Engine.update = Common.chain(
16+
Matter.Engine.update,
17+
function() {
18+
MatterWrap.Engine.update(this);
19+
}
20+
);
21+
},
22+
23+
Engine: {
24+
update: function(engine) {
25+
var world = engine.world,
26+
bodies = Composite.allBodies(world);
27+
28+
for (var i = 0; i < bodies.length; i += 1) {
29+
var body = bodies[i];
30+
31+
if (body.wrap) {
32+
MatterWrap.Body.wrap(body, body.wrap);
33+
}
34+
}
35+
}
36+
},
37+
38+
Body: {
39+
wrap: function(body, bounds) {
40+
var x = null,
41+
y = null;
42+
43+
if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
44+
if (body.bounds.min.x > bounds.max.x) {
45+
x = bounds.min.x - (body.bounds.max.x - body.position.x);
46+
} else if (body.bounds.max.x < bounds.min.x) {
47+
x = bounds.max.x - (body.bounds.min.x - body.position.x);
48+
}
49+
}
50+
51+
if (typeof bounds.min.y !== 'undefined' && typeof bounds.max.y !== 'undefined') {
52+
if (body.bounds.min.y > bounds.max.y) {
53+
y = bounds.min.y - (body.bounds.max.y - body.position.y);
54+
} else if (body.bounds.max.y < bounds.min.y) {
55+
y = bounds.max.y - (body.bounds.min.y - body.position.y);
56+
}
57+
}
58+
59+
if (x !== null || y !== null) {
60+
Body.setPosition(body, {
61+
x: x || body.position.x,
62+
y: y || body.position.y
63+
});
64+
}
65+
}
66+
}
67+
};
68+
69+
Matter.Plugin.register(MatterWrap);
70+
71+
window.MatterWrap = MatterWrap;
72+
73+
})();

0 commit comments

Comments
 (0)