mirror of
https://github.com/liabru/matter-js.git
synced 2024-12-23 13:19:01 -05:00
migrate Matter.World to the equivalent Matter.Composite
This commit is contained in:
parent
164456b5c1
commit
5dbec9bdc6
7 changed files with 70 additions and 196 deletions
|
@ -54,7 +54,7 @@ Example.views = function() {
|
|||
render.mouse = mouse;
|
||||
|
||||
// add bodies
|
||||
var stack = Composites.stack(20, 20, 15, 4, 0, 0, function(x, y) {
|
||||
var stack = Composites.stack(20, 20, 10, 4, 0, 0, function(x, y) {
|
||||
switch (Math.round(Common.random(0, 1))) {
|
||||
|
||||
case 0:
|
||||
|
@ -85,11 +85,11 @@ Example.views = function() {
|
|||
y: render.options.height * 0.5
|
||||
};
|
||||
|
||||
// make the world bounds a little bigger than the render bounds
|
||||
world.bounds.min.x = -300;
|
||||
world.bounds.min.y = -300;
|
||||
world.bounds.max.x = 1100;
|
||||
world.bounds.max.y = 900;
|
||||
// create limits for the viewport
|
||||
var extents = {
|
||||
min: { x: -300, y: -300 },
|
||||
max: { x: 1100, y: 900 }
|
||||
};
|
||||
|
||||
// keep track of current bounds scale (view zoom)
|
||||
var boundsScaleTarget = 1,
|
||||
|
@ -148,18 +148,18 @@ Example.views = function() {
|
|||
|
||||
translate = Vector.mult(direction, speed);
|
||||
|
||||
// prevent the view moving outside the world bounds
|
||||
if (render.bounds.min.x + translate.x < world.bounds.min.x)
|
||||
translate.x = world.bounds.min.x - render.bounds.min.x;
|
||||
// prevent the view moving outside the extents
|
||||
if (render.bounds.min.x + translate.x < extents.min.x)
|
||||
translate.x = extents.min.x - render.bounds.min.x;
|
||||
|
||||
if (render.bounds.max.x + translate.x > world.bounds.max.x)
|
||||
translate.x = world.bounds.max.x - render.bounds.max.x;
|
||||
if (render.bounds.max.x + translate.x > extents.max.x)
|
||||
translate.x = extents.max.x - render.bounds.max.x;
|
||||
|
||||
if (render.bounds.min.y + translate.y < world.bounds.min.y)
|
||||
translate.y = world.bounds.min.y - render.bounds.min.y;
|
||||
if (render.bounds.min.y + translate.y < extents.min.y)
|
||||
translate.y = extents.min.y - render.bounds.min.y;
|
||||
|
||||
if (render.bounds.max.y + translate.y > world.bounds.max.y)
|
||||
translate.y = world.bounds.max.y - render.bounds.max.y;
|
||||
if (render.bounds.max.y + translate.y > extents.max.y)
|
||||
translate.y = extents.max.y - render.bounds.max.y;
|
||||
|
||||
// move the view
|
||||
Bounds.translate(render.bounds, translate);
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
/**
|
||||
* The `Matter.Composite` module contains methods for creating and manipulating composite bodies.
|
||||
* A composite body is a collection of `Matter.Body`, `Matter.Constraint` and other `Matter.Composite`, therefore composites form a tree structure.
|
||||
* It is important to use the functions in this module to modify composites, rather than directly modifying their properties.
|
||||
* Note that the `Matter.World` object is also a type of `Matter.Composite` and as such all composite methods here can also operate on a `Matter.World`.
|
||||
* A composite is a collection of `Matter.Body`, `Matter.Constraint` and other `Matter.Composite` objects.
|
||||
*
|
||||
* They are a container that can represent complex objects made of multiple parts, even if they are not physically connected.
|
||||
* A composite could contain anything from a single body all the way up to a whole world.
|
||||
*
|
||||
* When making any changes to composites, use the included functions rather than changing their properties directly.
|
||||
*
|
||||
* See the included usage [examples](https://github.com/liabru/matter-js/tree/master/examples).
|
||||
*
|
||||
|
@ -67,11 +69,11 @@ var Body = require('./Body');
|
|||
};
|
||||
|
||||
/**
|
||||
* Generic add function. Adds one or many body(s), constraint(s) or a composite(s) to the given composite.
|
||||
* Generic single or multi-add function. Adds a single or an array of body(s), constraint(s) or composite(s) to the given composite.
|
||||
* Triggers `beforeAdd` and `afterAdd` events on the `composite`.
|
||||
* @method add
|
||||
* @param {composite} composite
|
||||
* @param {} object
|
||||
* @param {object|array} object A single or an array of body(s), constraint(s) or composite(s)
|
||||
* @return {composite} The original composite with the objects added
|
||||
*/
|
||||
Composite.add = function(composite, object) {
|
||||
|
@ -117,7 +119,7 @@ var Body = require('./Body');
|
|||
* Triggers `beforeRemove` and `afterRemove` events on the `composite`.
|
||||
* @method remove
|
||||
* @param {composite} composite
|
||||
* @param {} object
|
||||
* @param {object|array} object
|
||||
* @param {boolean} [deep=false]
|
||||
* @return {composite} The original composite with the objects removed
|
||||
*/
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
/**
|
||||
* The `Matter.World` module contains methods for creating and manipulating the world composite.
|
||||
* A `Matter.World` is a `Matter.Composite` body, which is a collection of `Matter.Body`, `Matter.Constraint` and other `Matter.Composite`.
|
||||
* A `Matter.World` has a few additional properties including `gravity` and `bounds`.
|
||||
* It is important to use the functions in the `Matter.Composite` module to modify the world composite, rather than directly modifying its properties.
|
||||
* There are also a few methods here that alias those in `Matter.Composite` for easier readability.
|
||||
* This module has now been replaced by `Matter.Composite`.
|
||||
*
|
||||
* See the included usage [examples](https://github.com/liabru/matter-js/tree/master/examples).
|
||||
* All usage should be migrated to the equivalent functions found on `Matter.Composite`.
|
||||
* For example `World.add(world, body)` now becomes `Composite.add(world, body)`.
|
||||
*
|
||||
* The property `world.gravity` has been moved to `engine.gravity`.
|
||||
*
|
||||
* For back-compatibility purposes this module will remain as a direct alias to `Matter.Composite` in the short term during migration.
|
||||
* Eventually this alias module will be marked as deprecated and then later removed in a future release.
|
||||
*
|
||||
* @class World
|
||||
* @extends Composite
|
||||
*/
|
||||
|
||||
var World = {};
|
||||
|
@ -16,132 +17,19 @@ var World = {};
|
|||
module.exports = World;
|
||||
|
||||
var Composite = require('./Composite');
|
||||
var Constraint = require('../constraint/Constraint');
|
||||
var Common = require('../core/Common');
|
||||
|
||||
(function() {
|
||||
|
||||
/**
|
||||
* Creates a new world composite. The options parameter is an object that specifies any properties you wish to override the defaults.
|
||||
* See the properties section below for detailed information on what you can pass via the `options` object.
|
||||
* @method create
|
||||
* @constructor
|
||||
* @param {} options
|
||||
* @return {world} A new world
|
||||
* See above, aliases for back compatibility only
|
||||
*/
|
||||
World.create = function(options) {
|
||||
var composite = Composite.create();
|
||||
|
||||
var defaults = {
|
||||
label: 'World',
|
||||
gravity: {
|
||||
x: 0,
|
||||
y: 1,
|
||||
scale: 0.001
|
||||
},
|
||||
bounds: {
|
||||
min: { x: -Infinity, y: -Infinity },
|
||||
max: { x: Infinity, y: Infinity }
|
||||
}
|
||||
};
|
||||
|
||||
return Common.extend(composite, defaults, options);
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
* Properties Documentation
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* The gravity to apply on the world.
|
||||
*
|
||||
* @property gravity
|
||||
* @type object
|
||||
*/
|
||||
|
||||
/**
|
||||
* The gravity x component.
|
||||
*
|
||||
* @property gravity.x
|
||||
* @type object
|
||||
* @default 0
|
||||
*/
|
||||
|
||||
/**
|
||||
* The gravity y component.
|
||||
*
|
||||
* @property gravity.y
|
||||
* @type object
|
||||
* @default 1
|
||||
*/
|
||||
|
||||
/**
|
||||
* The gravity scale factor.
|
||||
*
|
||||
* @property gravity.scale
|
||||
* @type object
|
||||
* @default 0.001
|
||||
*/
|
||||
|
||||
/**
|
||||
* A `Bounds` object that defines the world bounds for collision detection.
|
||||
*
|
||||
* @property bounds
|
||||
* @type bounds
|
||||
* @default { min: { x: -Infinity, y: -Infinity }, max: { x: Infinity, y: Infinity } }
|
||||
*/
|
||||
|
||||
// World is a Composite body
|
||||
// see src/module/Outro.js for these aliases:
|
||||
|
||||
/**
|
||||
* An alias for Composite.add
|
||||
* @method add
|
||||
* @param {world} world
|
||||
* @param {} object
|
||||
* @return {composite} The original world with the objects added
|
||||
*/
|
||||
|
||||
/**
|
||||
* An alias for Composite.remove
|
||||
* @method remove
|
||||
* @param {world} world
|
||||
* @param {} object
|
||||
* @param {boolean} [deep=false]
|
||||
* @return {composite} The original world with the objects removed
|
||||
*/
|
||||
|
||||
/**
|
||||
* An alias for Composite.clear
|
||||
* @method clear
|
||||
* @param {world} world
|
||||
* @param {boolean} keepStatic
|
||||
*/
|
||||
|
||||
/**
|
||||
* An alias for Composite.addComposite
|
||||
* @method addComposite
|
||||
* @param {world} world
|
||||
* @param {composite} composite
|
||||
* @return {world} The original world with the objects from composite added
|
||||
*/
|
||||
|
||||
/**
|
||||
* An alias for Composite.addBody
|
||||
* @method addBody
|
||||
* @param {world} world
|
||||
* @param {body} body
|
||||
* @return {world} The original world with the body added
|
||||
*/
|
||||
|
||||
/**
|
||||
* An alias for Composite.addConstraint
|
||||
* @method addConstraint
|
||||
* @param {world} world
|
||||
* @param {constraint} constraint
|
||||
* @return {world} The original world with the constraint added
|
||||
*/
|
||||
World.create = Composite.create;
|
||||
World.add = Composite.add;
|
||||
World.remove = Composite.remove;
|
||||
World.clear = Composite.clear;
|
||||
World.addComposite = Composite.addComposite;
|
||||
World.addBody = Composite.addBody;
|
||||
World.addConstraint = Composite.addConstraint;
|
||||
|
||||
})();
|
||||
|
|
|
@ -69,9 +69,9 @@ var Common = require('../core/Common');
|
|||
if (body.isSleeping && !forceUpdate)
|
||||
continue;
|
||||
|
||||
// don't update out of world bodies
|
||||
if (body.bounds.max.x < world.bounds.min.x || body.bounds.min.x > world.bounds.max.x
|
||||
|| body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y)
|
||||
// temporary back compatibility bounds check
|
||||
if (world.bounds && (body.bounds.max.x < world.bounds.min.x || body.bounds.min.x > world.bounds.max.x
|
||||
|| body.bounds.max.y < world.bounds.min.y || body.bounds.min.y > world.bounds.max.y))
|
||||
continue;
|
||||
|
||||
var newRegion = Grid._getRegion(grid, body);
|
||||
|
|
|
@ -12,7 +12,6 @@ var Engine = {};
|
|||
|
||||
module.exports = Engine;
|
||||
|
||||
var World = require('../body/World');
|
||||
var Sleeping = require('./Sleeping');
|
||||
var Resolver = require('../collision/Resolver');
|
||||
var Detector = require('../collision/Detector');
|
||||
|
@ -53,14 +52,14 @@ var Body = require('../body/Body');
|
|||
|
||||
var engine = Common.extend(defaults, options);
|
||||
|
||||
engine.world = options.world || World.create(engine.world);
|
||||
engine.world = options.world || Composite.create({ label: 'World' });
|
||||
engine.grid = Grid.create(options.grid || options.broadphase);
|
||||
engine.pairs = Pairs.create();
|
||||
engine.grid = Grid.create(engine.grid || engine.broadphase);
|
||||
|
||||
// temporary back compatibility
|
||||
engine.broadphase = engine.grid;
|
||||
engine.metrics = {};
|
||||
|
||||
|
||||
return engine;
|
||||
};
|
||||
|
||||
|
@ -438,11 +437,11 @@ var Body = require('../body/Body');
|
|||
*/
|
||||
|
||||
/**
|
||||
* A `World` composite object that will contain all simulated bodies and constraints.
|
||||
* The root `Matter.Composite` instance that will contain all bodies, constraints and other composites to be simulated by this engine.
|
||||
*
|
||||
* @property world
|
||||
* @type world
|
||||
* @default a Matter.World instance
|
||||
* @type composite
|
||||
* @default a Matter.Composite instance
|
||||
*/
|
||||
|
||||
/**
|
||||
|
|
|
@ -203,7 +203,7 @@ var deprecated = Common.deprecated;
|
|||
};
|
||||
|
||||
/**
|
||||
* Creates a composite with a Newton's Cradle setup of bodies and constraints.
|
||||
* This has now moved to the [newtonsCradle example](https://github.com/liabru/matter-js/blob/master/examples/newtonsCradle.js), follow that instead as this function is deprecated here.
|
||||
* @deprecated moved to newtonsCradle example
|
||||
* @method newtonsCradle
|
||||
* @param {number} xx
|
||||
|
@ -232,7 +232,7 @@ var deprecated = Common.deprecated;
|
|||
deprecated(Composites, 'newtonsCradle', 'Composites.newtonsCradle ➤ moved to newtonsCradle example');
|
||||
|
||||
/**
|
||||
* Creates a composite with simple car setup of bodies and constraints.
|
||||
* This has now moved to the [car example](https://github.com/liabru/matter-js/blob/master/examples/car.js), follow that instead as this function is deprecated here.
|
||||
* @deprecated moved to car example
|
||||
* @method car
|
||||
* @param {number} xx
|
||||
|
@ -302,7 +302,8 @@ var deprecated = Common.deprecated;
|
|||
deprecated(Composites, 'car', 'Composites.car ➤ moved to car example');
|
||||
|
||||
/**
|
||||
* Creates a simple soft body like object.
|
||||
* This has now moved to the [softBody example](https://github.com/liabru/matter-js/blob/master/examples/softBody.js)
|
||||
* and the [cloth example](https://github.com/liabru/matter-js/blob/master/examples/cloth.js), follow those instead as this function is deprecated here.
|
||||
* @deprecated moved to softBody and cloth examples
|
||||
* @method softBody
|
||||
* @param {number} xx
|
||||
|
|
|
@ -1,46 +1,30 @@
|
|||
var Matter = module.exports = require('../core/Matter');
|
||||
|
||||
Matter.Axes = require('../geometry/Axes');
|
||||
Matter.Bodies = require('../factory/Bodies');
|
||||
Matter.Body = require('../body/Body');
|
||||
Matter.Bounds = require('../geometry/Bounds');
|
||||
Matter.Common = require('../core/Common');
|
||||
Matter.Composite = require('../body/Composite');
|
||||
Matter.World = require('../body/World');
|
||||
|
||||
Matter.Composites = require('../factory/Composites');
|
||||
Matter.Constraint = require('../constraint/Constraint');
|
||||
Matter.Contact = require('../collision/Contact');
|
||||
Matter.Detector = require('../collision/Detector');
|
||||
Matter.Grid = require('../collision/Grid');
|
||||
Matter.Pairs = require('../collision/Pairs');
|
||||
Matter.Pair = require('../collision/Pair');
|
||||
Matter.Query = require('../collision/Query');
|
||||
Matter.Resolver = require('../collision/Resolver');
|
||||
Matter.SAT = require('../collision/SAT');
|
||||
|
||||
Matter.Constraint = require('../constraint/Constraint');
|
||||
Matter.MouseConstraint = require('../constraint/MouseConstraint');
|
||||
|
||||
Matter.Common = require('../core/Common');
|
||||
Matter.Engine = require('../core/Engine');
|
||||
Matter.Events = require('../core/Events');
|
||||
Matter.Grid = require('../collision/Grid');
|
||||
Matter.Mouse = require('../core/Mouse');
|
||||
Matter.Runner = require('../core/Runner');
|
||||
Matter.Sleeping = require('../core/Sleeping');
|
||||
Matter.MouseConstraint = require('../constraint/MouseConstraint');
|
||||
Matter.Pair = require('../collision/Pair');
|
||||
Matter.Pairs = require('../collision/Pairs');
|
||||
Matter.Plugin = require('../core/Plugin');
|
||||
|
||||
Matter.Bodies = require('../factory/Bodies');
|
||||
Matter.Composites = require('../factory/Composites');
|
||||
|
||||
Matter.Axes = require('../geometry/Axes');
|
||||
Matter.Bounds = require('../geometry/Bounds');
|
||||
Matter.Query = require('../collision/Query');
|
||||
Matter.Render = require('../render/Render');
|
||||
Matter.Resolver = require('../collision/Resolver');
|
||||
Matter.Runner = require('../core/Runner');
|
||||
Matter.SAT = require('../collision/SAT');
|
||||
Matter.Sleeping = require('../core/Sleeping');
|
||||
Matter.Svg = require('../geometry/Svg');
|
||||
Matter.Vector = require('../geometry/Vector');
|
||||
Matter.Vertices = require('../geometry/Vertices');
|
||||
|
||||
Matter.Render = require('../render/Render');
|
||||
|
||||
// aliases
|
||||
|
||||
Matter.World.add = Matter.Composite.add;
|
||||
Matter.World.remove = Matter.Composite.remove;
|
||||
Matter.World.addComposite = Matter.Composite.addComposite;
|
||||
Matter.World.addBody = Matter.Composite.addBody;
|
||||
Matter.World.addConstraint = Matter.Composite.addConstraint;
|
||||
Matter.World.clear = Matter.Composite.clear;
|
||||
Matter.Engine.run = Matter.Runner.run;
|
||||
Matter.World = require('../body/World');
|
||||
|
|
Loading…
Reference in a new issue