-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added plugins matter-attractors, matter-gravity, matter-world-wrap, r…
…emoved test plugins
- Loading branch information
Showing
5 changed files
with
174 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
(function() { | ||
|
||
var Body = Matter.Body, | ||
Common = Matter.Common, | ||
Composite = Matter.Composite; | ||
|
||
var MatterAttractors = { | ||
name: 'matter-attractors', | ||
|
||
version: '0.1.0', | ||
|
||
for: 'matter-js@^0.10.0', | ||
|
||
install: function(base) { | ||
base.Body.create = Common.chain( | ||
Matter.Body.create, | ||
MatterAttractors.init | ||
); | ||
|
||
base.Engine.update = Common.chain( | ||
Matter.Engine.update, | ||
MatterAttractors.update | ||
); | ||
}, | ||
|
||
init: function(body) { | ||
body = this || body; | ||
body.attractors = body.attractors || []; | ||
}, | ||
|
||
update: function(engine) { | ||
engine = this || engine; | ||
|
||
var world = engine.world, | ||
bodies = Composite.allBodies(world); | ||
|
||
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]; | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
Matter.Plugin.register(MatterAttractors); | ||
|
||
window.MatterAttractors = MatterAttractors; | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
(function() { | ||
|
||
var Body = Matter.Body, | ||
Common = Matter.Common, | ||
Vector = Matter.Vector; | ||
|
||
var MatterGravity = { | ||
name: 'matter-gravity', | ||
|
||
version: '0.1.0', | ||
|
||
for: 'matter-js@^0.10.0', | ||
|
||
uses: [ | ||
'matter-attractors@^0.1.0' | ||
], | ||
|
||
install: function(base) { | ||
base.Body.create = Common.chain( | ||
Matter.Body.create, | ||
MatterGravity.addAttractor | ||
); | ||
}, | ||
|
||
addAttractor: function(body) { | ||
body = this || body; | ||
|
||
if (body.gravity) { | ||
body.attractors.push(MatterGravity.applyForce); | ||
} | ||
}, | ||
|
||
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); | ||
} | ||
}; | ||
|
||
Matter.Plugin.register(MatterGravity); | ||
|
||
window.MatterGravity = MatterGravity; | ||
|
||
})(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
(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; | ||
|
||
})(); |