0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-21 17:14:38 -05:00

refactored plugins

This commit is contained in:
liabru 2016-10-15 23:23:35 +01:00
parent 4b5837ec3f
commit b4a3453659
5 changed files with 127 additions and 107 deletions

View file

@ -11,14 +11,9 @@
Matter.use(
'matter-gravity',
'matter-world-wrap'
'matter-wrap'
);
world.bounds = {
min: { x: 0, y: 0 },
max: { x: 800, y: 600 }
};
world.bodies = [];
world.gravity.scale = 0;
@ -32,7 +27,11 @@
{
mass: Common.random(10, 20),
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;
renderOptions.showAngleIndicator = false;
renderOptions.showPositions = true;
};
})();

View file

@ -14,44 +14,49 @@
install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterAttractors.init
function() {
MatterAttractors.Body.init(this);
}
);
base.Engine.update = Common.chain(
Matter.Engine.update,
MatterAttractors.update
function() {
MatterAttractors.Engine.update(this);
}
);
},
init: function(body) {
body = this || body;
body.attractors = body.attractors || [];
Body: {
init: function(body) {
body.attractors = body.attractors || [];
}
},
update: function(engine) {
engine = this || engine;
Engine: {
update: function(engine) {
var world = engine.world,
bodies = Composite.allBodies(world);
var world = engine.world,
bodies = Composite.allBodies(world);
for (var i = 0; i < bodies.length; i += 1) {
var bodyA = bodies[i],
attractors = bodyA.attractors;
for (var i = 0; i < bodies.length; i += 1) {
var bodyA = bodies[i],
attractors = bodyA.attractors;
if (attractors && attractors.length > 0) {
for (var j = i + 1; j < bodies.length; j += 1) {
var bodyB = bodies[j];
if (attractors && attractors.length > 0) {
for (var j = i + 1; j < bodies.length; j += 1) {
var bodyB = bodies[j];
for (var k = 0; k < attractors.length; k += 1) {
var attractor = attractors[k],
forceVector = attractor;
for (var k = 0; k < attractors.length; k += 1) {
var attractor = attractors[k],
forceVector = attractor;
if (Common.isFunction(attractor)) {
forceVector = attractor(bodyA, bodyB);
}
if (forceVector) {
Body.applyForce(bodyB, bodyB.position, forceVector);
if (Common.isFunction(attractor)) {
forceVector = attractor(bodyA, bodyB);
}
if (forceVector) {
Body.applyForce(bodyB, bodyB.position, forceVector);
}
}
}
}

View file

@ -18,27 +18,29 @@
install: function(base) {
base.Body.create = Common.chain(
Matter.Body.create,
MatterGravity.addAttractor
function() {
MatterGravity.Body.init(this);
}
);
},
addAttractor: function(body) {
body = this || body;
Body: {
init: function(body) {
if (body.gravity) {
body.attractors.push(MatterGravity.Body.applyGravity);
}
},
if (body.gravity) {
body.attractors.push(MatterGravity.applyForce);
applyGravity: 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);
}
},
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);
}
};

View file

@ -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
View 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;
})();