From 6dd0c05f41ade9a6ccddbb6fcf9b60c1a494a186 Mon Sep 17 00:00:00 2001 From: liabru Date: Sat, 21 Jun 2014 19:24:53 +0100 Subject: [PATCH] removed engine.broadphase.current, removed engine.broadphase.instance, changed engine.broadphase to refer to the broadphase instance, updated matter-tools --- demo/js/lib/matter-tools/matter-tools-dev.js | 22 ++++++++---- src/core/Engine.js | 38 ++++++++++---------- src/render/Render.js | 4 +-- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/demo/js/lib/matter-tools/matter-tools-dev.js b/demo/js/lib/matter-tools/matter-tools-dev.js index 65438a39..d0fc044d 100644 --- a/demo/js/lib/matter-tools/matter-tools-dev.js +++ b/demo/js/lib/matter-tools/matter-tools-dev.js @@ -1,12 +1,12 @@ /** -* matter-tools-dev.min.js 0.5.0-dev 2014-05-04 +* matter-tools-dev.min.js 0.5.0-dev 2014-06-21 * https://github.com/liabru/matter-tools * License: MIT */ (function() { var MatterTools = {}; - var Engine = Matter.Engine, World = Matter.World, Bodies = Matter.Bodies, Body = Matter.Body, Composite = Matter.Composite, Composites = Matter.Composites, Common = Matter.Common, Constraint = Matter.Constraint, Events = Matter.Events, Bounds = Matter.Bounds, Vector = Matter.Vector, Vertices = Matter.Vertices, MouseConstraint = Matter.MouseConstraint, Render = Matter.Render, RenderPixi = Matter.RenderPixi, Mouse = Matter.Mouse, Query = Matter.Query; + var Engine = Matter.Engine, World = Matter.World, Bodies = Matter.Bodies, Body = Matter.Body, Composite = Matter.Composite, Composites = Matter.Composites, Common = Matter.Common, Constraint = Matter.Constraint, Events = Matter.Events, Bounds = Matter.Bounds, Vector = Matter.Vector, Vertices = Matter.Vertices, MouseConstraint = Matter.MouseConstraint, Render = Matter.Render, RenderPixi = Matter.RenderPixi, Mouse = Matter.Mouse, Query = Matter.Query, Grid = Matter.Grid, Detector = Matter.Detector; var Gui = {}; (function() { Gui.create = function(engine, options) { @@ -19,6 +19,13 @@ var gui = { engine:engine, datGui:datGui, + broadphase:"grid", + broadphaseCache:{ + grid:engine.broadphase.controller === Grid ? engine.broadphase :Grid.create(), + bruteForce:{ + detector:Detector.bruteForce + } + }, amount:1, size:40, sides:4, @@ -146,7 +153,8 @@ gravity.open(); var physics = datGui.addFolder("Engine"); physics.add(engine, "enableSleeping"); - physics.add(engine.broadphase, "current", [ "grid", "bruteForce" ]).onFinishChange(function(value) { + physics.add(gui, "broadphase", [ "grid", "bruteForce" ]).onFinishChange(function(value) { + engine.broadphase = gui.broadphaseCache[value]; Composite.setModified(engine.world, true, false, false); }); physics.add(engine.timing, "timeScale", 0, 1.2).step(.05).listen(); @@ -490,10 +498,10 @@ } }; var _getMousePosition = function(inspector) { - return Vector.add(inspector.engine.input.mouse.position, inspector.offset); + return Vector.add(inspector.mouse.position, inspector.offset); }; var _initEngineEvents = function(inspector) { - var engine = inspector.engine, mouse = engine.input.mouse, mousePosition = _getMousePosition(inspector), controls = inspector.controls; + var engine = inspector.engine, mouse = inspector.mouse, mousePosition = _getMousePosition(inspector), controls = inspector.controls; Events.on(engine, "tick", function() { mousePosition = _getMousePosition(inspector); var mouseDelta = mousePosition.x - inspector.mousePrevPosition.x, keyDelta = _key.isPressed("up") + _key.isPressed("right") - _key.isPressed("down") - _key.isPressed("left"), delta = mouseDelta + keyDelta; @@ -654,7 +662,7 @@ }, 200); }; var _updateSelectedMouseDownOffset = function(inspector) { - var selected = inspector.selected, mouse = inspector.engine.input.mouse, mousePosition = _getMousePosition(inspector), item, data; + var selected = inspector.selected, mouse = inspector.mouse, mousePosition = _getMousePosition(inspector), item, data; for (var i = 0; i < selected.length; i++) { item = selected[i]; data = item.data; @@ -677,7 +685,7 @@ } }; var _moveSelectedObjects = function(inspector, x, y) { - var selected = inspector.selected, mouse = inspector.engine.input.mouse, mousePosition = _getMousePosition(inspector), item, data; + var selected = inspector.selected, mouse = inspector.mouse, mousePosition = _getMousePosition(inspector), item, data; for (var i = 0; i < selected.length; i++) { item = selected[i]; data = item.data; diff --git a/src/core/Engine.js b/src/core/Engine.js index 84bbca52..1b764421 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -55,6 +55,9 @@ var Engine = {}; render: { element: element, controller: Render + }, + broadphase: { + controller: Grid } }; @@ -64,18 +67,7 @@ var Engine = {}; engine.world = World.create(engine.world); engine.pairs = Pairs.create(); engine.metrics = engine.metrics || Metrics.create(); - - engine.broadphase = engine.broadphase || { - current: 'grid', - grid: { - controller: Grid, - instance: Grid.create(), - detector: Detector.collisions - }, - bruteForce: { - detector: Detector.bruteForce - } - }; + engine.broadphase = engine.broadphase.controller.create(engine.broadphase); return engine; }; @@ -184,7 +176,7 @@ var Engine = {}; var world = engine.world, timing = engine.timing, - broadphase = engine.broadphase[engine.broadphase.current], + broadphase = engine.broadphase, broadphasePairs = [], i; @@ -227,11 +219,11 @@ var Engine = {}; // if world is dirty, we must flush the whole grid if (world.isModified) - broadphase.controller.clear(broadphase.instance); + broadphase.controller.clear(broadphase); // update the grid buckets based on current bodies - broadphase.controller.update(broadphase.instance, allBodies, engine, world.isModified); - broadphasePairs = broadphase.instance.pairsList; + broadphase.controller.update(broadphase, allBodies, engine, world.isModified); + broadphasePairs = broadphase.pairsList; } else { // if no broadphase set, we just pass all bodies @@ -329,11 +321,11 @@ var Engine = {}; Pairs.clear(engine.pairs); - var broadphase = engine.broadphase[engine.broadphase.current]; + var broadphase = engine.broadphase; if (broadphase.controller) { var bodies = Composite.allBodies(world); - broadphase.controller.clear(broadphase.instance); - broadphase.controller.update(broadphase.instance, bodies, engine, true); + broadphase.controller.clear(broadphase); + broadphase.controller.update(broadphase, bodies, engine, true); } }; @@ -587,6 +579,14 @@ var Engine = {}; * @default a Matter.Render instance */ + /** + * An instance of a broadphase controller. The default value is a `Matter.Grid` instance created by `Engine.create`. + * + * @property broadphase + * @type grid + * @default a Matter.Grid instance + */ + /** * A `World` composite object that will contain all simulated bodies and constraints. * diff --git a/src/render/Render.js b/src/render/Render.js index adb80ec0..18a3a4c7 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -204,8 +204,8 @@ var Render = {}; Render.constraints(constraints, context); - if (options.showBroadphase && engine.broadphase.current === 'grid') - Render.grid(engine, engine.broadphase[engine.broadphase.current].instance, context); + if (options.showBroadphase && engine.broadphase.controller === Grid) + Render.grid(engine, engine.broadphase, context); if (options.showDebug) Render.debug(engine, context);