mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-17 12:42:17 -05:00
changed engine.broadphase to engine.grid
This commit is contained in:
parent
1aa8ed0972
commit
b74e40024b
3 changed files with 27 additions and 41 deletions
|
@ -9,7 +9,6 @@ var Grid = {};
|
||||||
module.exports = Grid;
|
module.exports = Grid;
|
||||||
|
|
||||||
var Pair = require('./Pair');
|
var Pair = require('./Pair');
|
||||||
var Detector = require('./Detector');
|
|
||||||
var Common = require('../core/Common');
|
var Common = require('../core/Common');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
@ -22,8 +21,6 @@ var Common = require('../core/Common');
|
||||||
*/
|
*/
|
||||||
Grid.create = function(options) {
|
Grid.create = function(options) {
|
||||||
var defaults = {
|
var defaults = {
|
||||||
controller: Grid,
|
|
||||||
detector: Detector.collisions,
|
|
||||||
buckets: {},
|
buckets: {},
|
||||||
pairs: {},
|
pairs: {},
|
||||||
pairsList: [],
|
pairsList: [],
|
||||||
|
|
|
@ -15,6 +15,7 @@ module.exports = Engine;
|
||||||
var World = require('../body/World');
|
var World = require('../body/World');
|
||||||
var Sleeping = require('./Sleeping');
|
var Sleeping = require('./Sleeping');
|
||||||
var Resolver = require('../collision/Resolver');
|
var Resolver = require('../collision/Resolver');
|
||||||
|
var Detector = require('../collision/Detector');
|
||||||
var Pairs = require('../collision/Pairs');
|
var Pairs = require('../collision/Pairs');
|
||||||
var Grid = require('../collision/Grid');
|
var Grid = require('../collision/Grid');
|
||||||
var Events = require('./Events');
|
var Events = require('./Events');
|
||||||
|
@ -43,21 +44,22 @@ var Body = require('../body/Body');
|
||||||
enableSleeping: false,
|
enableSleeping: false,
|
||||||
events: [],
|
events: [],
|
||||||
plugin: {},
|
plugin: {},
|
||||||
|
grid: null,
|
||||||
timing: {
|
timing: {
|
||||||
timestamp: 0,
|
timestamp: 0,
|
||||||
timeScale: 1
|
timeScale: 1
|
||||||
},
|
|
||||||
broadphase: {
|
|
||||||
controller: Grid
|
|
||||||
}
|
}
|
||||||
metrics: {}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
var engine = Common.extend(defaults, options);
|
var engine = Common.extend(defaults, options);
|
||||||
|
|
||||||
engine.world = options.world || World.create(engine.world);
|
engine.world = options.world || World.create(engine.world);
|
||||||
engine.pairs = Pairs.create();
|
engine.pairs = Pairs.create();
|
||||||
engine.broadphase = engine.broadphase.controller.create(engine.broadphase);
|
engine.grid = Grid.create(engine.grid || engine.broadphase);
|
||||||
|
|
||||||
|
// temporary back compatibility
|
||||||
|
engine.broadphase = engine.grid;
|
||||||
|
engine.metrics = {};
|
||||||
|
|
||||||
return engine;
|
return engine;
|
||||||
};
|
};
|
||||||
|
@ -83,8 +85,8 @@ var Body = require('../body/Body');
|
||||||
|
|
||||||
var world = engine.world,
|
var world = engine.world,
|
||||||
timing = engine.timing,
|
timing = engine.timing,
|
||||||
broadphase = engine.broadphase,
|
grid = engine.grid,
|
||||||
broadphasePairs = [],
|
gridPairs = [],
|
||||||
i;
|
i;
|
||||||
|
|
||||||
// increment timestamp
|
// increment timestamp
|
||||||
|
@ -119,18 +121,14 @@ var Body = require('../body/Body');
|
||||||
Constraint.postSolveAll(allBodies);
|
Constraint.postSolveAll(allBodies);
|
||||||
|
|
||||||
// broadphase pass: find potential collision pairs
|
// broadphase pass: find potential collision pairs
|
||||||
if (broadphase.controller) {
|
|
||||||
// if world is dirty, we must flush the whole grid
|
// if world is dirty, we must flush the whole grid
|
||||||
if (world.isModified)
|
if (world.isModified)
|
||||||
broadphase.controller.clear(broadphase);
|
Grid.clear(grid);
|
||||||
|
|
||||||
// update the grid buckets based on current bodies
|
// update the grid buckets based on current bodies
|
||||||
broadphase.controller.update(broadphase, allBodies, engine, world.isModified);
|
Grid.update(grid, allBodies, engine, world.isModified);
|
||||||
broadphasePairs = broadphase.pairsList;
|
gridPairs = grid.pairsList;
|
||||||
} else {
|
|
||||||
// if no broadphase set, we just pass all bodies
|
|
||||||
broadphasePairs = allBodies;
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear all composite modified flags
|
// clear all composite modified flags
|
||||||
if (world.isModified) {
|
if (world.isModified) {
|
||||||
|
@ -138,7 +136,7 @@ var Body = require('../body/Body');
|
||||||
}
|
}
|
||||||
|
|
||||||
// narrowphase pass: find actual collisions, then create or update collision pairs
|
// narrowphase pass: find actual collisions, then create or update collision pairs
|
||||||
var collisions = broadphase.detector(broadphasePairs, engine);
|
var collisions = Detector.collisions(gridPairs, engine);
|
||||||
|
|
||||||
// update collision pairs
|
// update collision pairs
|
||||||
var pairs = engine.pairs,
|
var pairs = engine.pairs,
|
||||||
|
@ -219,16 +217,12 @@ var Body = require('../body/Body');
|
||||||
* @param {engine} engine
|
* @param {engine} engine
|
||||||
*/
|
*/
|
||||||
Engine.clear = function(engine) {
|
Engine.clear = function(engine) {
|
||||||
var world = engine.world;
|
var world = engine.world,
|
||||||
|
bodies = Composite.allBodies(world);
|
||||||
|
|
||||||
Pairs.clear(engine.pairs);
|
Pairs.clear(engine.pairs);
|
||||||
|
Grid.clear(engine.grid);
|
||||||
var broadphase = engine.broadphase;
|
Grid.update(engine.grid, bodies, engine, true);
|
||||||
if (broadphase.controller) {
|
|
||||||
var bodies = Composite.allBodies(world);
|
|
||||||
broadphase.controller.clear(broadphase);
|
|
||||||
broadphase.controller.update(broadphase, bodies, engine, true);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -427,21 +421,17 @@ var Body = require('../body/Body');
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An instance of a `Render` controller. The default value is a `Matter.Render` instance created by `Engine.create`.
|
* A `Matter.Grid` instance.
|
||||||
* One may also develop a custom renderer module based on `Matter.Render` and pass an instance of it to `Engine.create` via `options.render`.
|
|
||||||
*
|
*
|
||||||
* A minimal custom renderer object must define at least three functions: `create`, `clear` and `world` (see `Matter.Render`).
|
* @property grid
|
||||||
* It is also possible to instead pass the _module_ reference via `options.render.controller` and `Engine.create` will instantiate one for you.
|
* @type grid
|
||||||
*
|
* @default a Matter.Grid instance
|
||||||
* @property render
|
|
||||||
* @type render
|
|
||||||
* @deprecated see Demo.js for an example of creating a renderer
|
|
||||||
* @default a Matter.Render instance
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An instance of a broadphase controller. The default value is a `Matter.Grid` instance created by `Engine.create`.
|
* Replaced by and now alias for `engine.grid`.
|
||||||
*
|
*
|
||||||
|
* @deprecated use `engine.grid`
|
||||||
* @property broadphase
|
* @property broadphase
|
||||||
* @type grid
|
* @type grid
|
||||||
* @default a Matter.Grid instance
|
* @default a Matter.Grid instance
|
||||||
|
|
|
@ -14,7 +14,6 @@ var Common = require('../core/Common');
|
||||||
var Composite = require('../body/Composite');
|
var Composite = require('../body/Composite');
|
||||||
var Bounds = require('../geometry/Bounds');
|
var Bounds = require('../geometry/Bounds');
|
||||||
var Events = require('../core/Events');
|
var Events = require('../core/Events');
|
||||||
var Grid = require('../collision/Grid');
|
|
||||||
var Vector = require('../geometry/Vector');
|
var Vector = require('../geometry/Vector');
|
||||||
var Mouse = require('../core/Mouse');
|
var Mouse = require('../core/Mouse');
|
||||||
|
|
||||||
|
@ -406,8 +405,8 @@ var Mouse = require('../core/Mouse');
|
||||||
|
|
||||||
Render.constraints(constraints, context);
|
Render.constraints(constraints, context);
|
||||||
|
|
||||||
if (options.showBroadphase && engine.broadphase.controller === Grid)
|
if (options.showBroadphase)
|
||||||
Render.grid(render, engine.broadphase, context);
|
Render.grid(render, engine.grid, context);
|
||||||
|
|
||||||
if (options.showDebug)
|
if (options.showDebug)
|
||||||
Render.debug(render, context);
|
Render.debug(render, context);
|
||||||
|
|
Loading…
Reference in a new issue