mirror of
https://github.com/liabru/matter-js.git
synced 2025-02-01 18:24:54 -05:00
refactored plugins
This commit is contained in:
parent
4b5837ec3f
commit
b4a3453659
5 changed files with 127 additions and 107 deletions
|
@ -11,14 +11,9 @@
|
||||||
|
|
||||||
Matter.use(
|
Matter.use(
|
||||||
'matter-gravity',
|
'matter-gravity',
|
||||||
'matter-world-wrap'
|
'matter-wrap'
|
||||||
);
|
);
|
||||||
|
|
||||||
world.bounds = {
|
|
||||||
min: { x: 0, y: 0 },
|
|
||||||
max: { x: 800, y: 600 }
|
|
||||||
};
|
|
||||||
|
|
||||||
world.bodies = [];
|
world.bodies = [];
|
||||||
world.gravity.scale = 0;
|
world.gravity.scale = 0;
|
||||||
|
|
||||||
|
@ -32,7 +27,11 @@
|
||||||
{
|
{
|
||||||
mass: Common.random(10, 20),
|
mass: Common.random(10, 20),
|
||||||
gravity: G,
|
gravity: G,
|
||||||
frictionAir: 0
|
frictionAir: 0,
|
||||||
|
wrap: {
|
||||||
|
min: { x: 0, y: 0 },
|
||||||
|
max: { x: 800, y: 600 }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@
|
||||||
|
|
||||||
var renderOptions = demo.render.options;
|
var renderOptions = demo.render.options;
|
||||||
renderOptions.showAngleIndicator = false;
|
renderOptions.showAngleIndicator = false;
|
||||||
renderOptions.showPositions = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
|
@ -14,44 +14,49 @@
|
||||||
install: function(base) {
|
install: function(base) {
|
||||||
base.Body.create = Common.chain(
|
base.Body.create = Common.chain(
|
||||||
Matter.Body.create,
|
Matter.Body.create,
|
||||||
MatterAttractors.init
|
function() {
|
||||||
|
MatterAttractors.Body.init(this);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
base.Engine.update = Common.chain(
|
base.Engine.update = Common.chain(
|
||||||
Matter.Engine.update,
|
Matter.Engine.update,
|
||||||
MatterAttractors.update
|
function() {
|
||||||
|
MatterAttractors.Engine.update(this);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
init: function(body) {
|
Body: {
|
||||||
body = this || body;
|
init: function(body) {
|
||||||
body.attractors = body.attractors || [];
|
body.attractors = body.attractors || [];
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
update: function(engine) {
|
Engine: {
|
||||||
engine = this || engine;
|
update: function(engine) {
|
||||||
|
var world = engine.world,
|
||||||
|
bodies = Composite.allBodies(world);
|
||||||
|
|
||||||
var world = engine.world,
|
for (var i = 0; i < bodies.length; i += 1) {
|
||||||
bodies = Composite.allBodies(world);
|
var bodyA = bodies[i],
|
||||||
|
attractors = bodyA.attractors;
|
||||||
|
|
||||||
for (var i = 0; i < bodies.length; i += 1) {
|
if (attractors && attractors.length > 0) {
|
||||||
var bodyA = bodies[i],
|
for (var j = i + 1; j < bodies.length; j += 1) {
|
||||||
attractors = bodyA.attractors;
|
var bodyB = bodies[j];
|
||||||
|
|
||||||
if (attractors && attractors.length > 0) {
|
for (var k = 0; k < attractors.length; k += 1) {
|
||||||
for (var j = i + 1; j < bodies.length; j += 1) {
|
var attractor = attractors[k],
|
||||||
var bodyB = bodies[j];
|
forceVector = attractor;
|
||||||
|
|
||||||
for (var k = 0; k < attractors.length; k += 1) {
|
if (Common.isFunction(attractor)) {
|
||||||
var attractor = attractors[k],
|
forceVector = attractor(bodyA, bodyB);
|
||||||
forceVector = attractor;
|
}
|
||||||
|
|
||||||
if (Common.isFunction(attractor)) {
|
if (forceVector) {
|
||||||
forceVector = attractor(bodyA, bodyB);
|
Body.applyForce(bodyB, bodyB.position, forceVector);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (forceVector) {
|
|
||||||
Body.applyForce(bodyB, bodyB.position, forceVector);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,27 +18,29 @@
|
||||||
install: function(base) {
|
install: function(base) {
|
||||||
base.Body.create = Common.chain(
|
base.Body.create = Common.chain(
|
||||||
Matter.Body.create,
|
Matter.Body.create,
|
||||||
MatterGravity.addAttractor
|
function() {
|
||||||
|
MatterGravity.Body.init(this);
|
||||||
|
}
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
addAttractor: function(body) {
|
Body: {
|
||||||
body = this || body;
|
init: function(body) {
|
||||||
|
if (body.gravity) {
|
||||||
|
body.attractors.push(MatterGravity.Body.applyGravity);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
if (body.gravity) {
|
applyGravity: function(bodyA, bodyB) {
|
||||||
body.attractors.push(MatterGravity.applyForce);
|
var bToA = Vector.sub(bodyB.position, bodyA.position),
|
||||||
|
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
|
||||||
|
normal = Vector.normalise(bToA),
|
||||||
|
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
|
||||||
|
force = Vector.mult(normal, magnitude);
|
||||||
|
|
||||||
|
Body.applyForce(bodyA, bodyA.position, Vector.neg(force));
|
||||||
|
Body.applyForce(bodyB, bodyB.position, force);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
applyForce: function(bodyA, bodyB) {
|
|
||||||
var bToA = Vector.sub(bodyB.position, bodyA.position),
|
|
||||||
distanceSq = Vector.magnitudeSquared(bToA) || 0.0001,
|
|
||||||
normal = Vector.normalise(bToA),
|
|
||||||
magnitude = -bodyA.gravity * (bodyA.mass * bodyB.mass / distanceSq),
|
|
||||||
force = Vector.mult(normal, magnitude);
|
|
||||||
|
|
||||||
Body.applyForce(bodyA, bodyA.position, Vector.neg(force));
|
|
||||||
Body.applyForce(bodyB, bodyB.position, force);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,58 +0,0 @@
|
||||||
(function() {
|
|
||||||
|
|
||||||
var Body = Matter.Body,
|
|
||||||
Common = Matter.Common,
|
|
||||||
Composite = Matter.Composite;
|
|
||||||
|
|
||||||
var MatterWorldWrap = {
|
|
||||||
name: 'matter-world-wrap',
|
|
||||||
|
|
||||||
version: '0.1.0',
|
|
||||||
|
|
||||||
for: 'matter-js@^0.10.0',
|
|
||||||
|
|
||||||
install: function(base) {
|
|
||||||
base.Engine.update = Common.chain(
|
|
||||||
Matter.Engine.update,
|
|
||||||
MatterWorldWrap.update
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
update: function(engine) {
|
|
||||||
engine = this || engine;
|
|
||||||
|
|
||||||
var world = engine.world,
|
|
||||||
bodies = Composite.allBodies(world);
|
|
||||||
|
|
||||||
for (var i = 0; i < bodies.length; i += 1) {
|
|
||||||
var body = bodies[i],
|
|
||||||
x = null,
|
|
||||||
y = null;
|
|
||||||
|
|
||||||
if (body.bounds.min.x > world.bounds.max.x) {
|
|
||||||
x = world.bounds.min.x - (body.bounds.max.x - body.position.x);
|
|
||||||
} else if (body.bounds.max.x < world.bounds.min.x) {
|
|
||||||
x = world.bounds.max.x - (body.bounds.min.x - body.position.x);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (body.bounds.min.y > world.bounds.max.y) {
|
|
||||||
y = world.bounds.min.y - (body.bounds.max.y - body.position.y);
|
|
||||||
} else if (body.bounds.max.y < world.bounds.min.y) {
|
|
||||||
y = world.bounds.max.y - (body.bounds.min.y - body.position.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (x !== null || y !== null) {
|
|
||||||
Body.setPosition(body, {
|
|
||||||
x: x || body.position.x,
|
|
||||||
y: y || body.position.y
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Matter.Plugin.register(MatterWorldWrap);
|
|
||||||
|
|
||||||
window.MatterWorldWrap = MatterWorldWrap;
|
|
||||||
|
|
||||||
})();
|
|
73
examples/wrapPlugin.js
Normal file
73
examples/wrapPlugin.js
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
(function() {
|
||||||
|
|
||||||
|
var Body = Matter.Body,
|
||||||
|
Common = Matter.Common,
|
||||||
|
Composite = Matter.Composite;
|
||||||
|
|
||||||
|
var MatterWrap = {
|
||||||
|
name: 'matter-wrap',
|
||||||
|
|
||||||
|
version: '0.1.0',
|
||||||
|
|
||||||
|
for: 'matter-js@^0.10.0',
|
||||||
|
|
||||||
|
install: function(base) {
|
||||||
|
base.Engine.update = Common.chain(
|
||||||
|
Matter.Engine.update,
|
||||||
|
function() {
|
||||||
|
MatterWrap.Engine.update(this);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
Engine: {
|
||||||
|
update: function(engine) {
|
||||||
|
var world = engine.world,
|
||||||
|
bodies = Composite.allBodies(world);
|
||||||
|
|
||||||
|
for (var i = 0; i < bodies.length; i += 1) {
|
||||||
|
var body = bodies[i];
|
||||||
|
|
||||||
|
if (body.wrap) {
|
||||||
|
MatterWrap.Body.wrap(body, body.wrap);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
Body: {
|
||||||
|
wrap: function(body, bounds) {
|
||||||
|
var x = null,
|
||||||
|
y = null;
|
||||||
|
|
||||||
|
if (typeof bounds.min.x !== 'undefined' && typeof bounds.max.x !== 'undefined') {
|
||||||
|
if (body.bounds.min.x > bounds.max.x) {
|
||||||
|
x = bounds.min.x - (body.bounds.max.x - body.position.x);
|
||||||
|
} else if (body.bounds.max.x < bounds.min.x) {
|
||||||
|
x = bounds.max.x - (body.bounds.min.x - body.position.x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof bounds.min.y !== 'undefined' && typeof bounds.max.y !== 'undefined') {
|
||||||
|
if (body.bounds.min.y > bounds.max.y) {
|
||||||
|
y = bounds.min.y - (body.bounds.max.y - body.position.y);
|
||||||
|
} else if (body.bounds.max.y < bounds.min.y) {
|
||||||
|
y = bounds.max.y - (body.bounds.min.y - body.position.y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (x !== null || y !== null) {
|
||||||
|
Body.setPosition(body, {
|
||||||
|
x: x || body.position.x,
|
||||||
|
y: y || body.position.y
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Matter.Plugin.register(MatterWrap);
|
||||||
|
|
||||||
|
window.MatterWrap = MatterWrap;
|
||||||
|
|
||||||
|
})();
|
Loading…
Add table
Reference in a new issue