1+ var Matter = require ( '../../CustomMain' ) ;
2+
3+ /**
4+ * A coordinate wrapping plugin for matter.js.
5+ * See the readme for usage and examples.
6+ * @module MatterWrap
7+ */
8+ var MatterWrap = {
9+ // plugin meta
10+ name : 'matter-wrap' , // PLUGIN_NAME
11+ version : '0.1.4' , // PLUGIN_VERSION
12+ for : 'matter-js@^0.13.1' ,
13+ silent : true , // no console log please
14+
15+ // installs the plugin where `base` is `Matter`
16+ // you should not need to call this directly.
17+ install : function ( base ) {
18+ base . after ( 'Engine.update' , function ( ) {
19+ MatterWrap . Engine . update ( this ) ;
20+ } ) ;
21+ } ,
22+
23+ Engine : {
24+ /**
25+ * Updates the engine by wrapping bodies and composites inside `engine.world`.
26+ * This is called automatically by the plugin.
27+ * @function MatterWrap.Engine.update
28+ * @param {Matter.Engine } engine The engine to update.
29+ * @returns {void } No return value.
30+ */
31+ update : function ( engine ) {
32+ var world = engine . world ,
33+ bodies = Matter . Composite . allBodies ( world ) ,
34+ composites = Matter . Composite . allComposites ( world ) ;
35+
36+ for ( var i = 0 ; i < bodies . length ; i += 1 ) {
37+ var body = bodies [ i ] ;
38+
39+ if ( body . plugin . wrap ) {
40+ MatterWrap . Body . wrap ( body , body . plugin . wrap ) ;
41+ }
42+ }
43+
44+ for ( i = 0 ; i < composites . length ; i += 1 ) {
45+ var composite = composites [ i ] ;
46+
47+ if ( composite . plugin . wrap ) {
48+ MatterWrap . Composite . wrap ( composite , composite . plugin . wrap ) ;
49+ }
50+ }
51+ }
52+ } ,
53+
54+ Bounds : {
55+ /**
56+ * Returns a translation vector that wraps the `objectBounds` inside the `bounds`.
57+ * @function MatterWrap.Bounds.wrap
58+ * @param {Matter.Bounds } objectBounds The bounds of the object to wrap inside the bounds.
59+ * @param {Matter.Bounds } bounds The bounds to wrap the body inside.
60+ * @returns {?Matter.Vector } A translation vector (only if wrapping is required).
61+ */
62+ wrap : function ( objectBounds , bounds ) {
63+ var x = null ,
64+ y = null ;
65+
66+ if ( typeof bounds . min . x !== 'undefined' && typeof bounds . max . x !== 'undefined' ) {
67+ if ( objectBounds . min . x > bounds . max . x ) {
68+ x = bounds . min . x - objectBounds . max . x ;
69+ } else if ( objectBounds . max . x < bounds . min . x ) {
70+ x = bounds . max . x - objectBounds . min . x ;
71+ }
72+ }
73+
74+ if ( typeof bounds . min . y !== 'undefined' && typeof bounds . max . y !== 'undefined' ) {
75+ if ( objectBounds . min . y > bounds . max . y ) {
76+ y = bounds . min . y - objectBounds . max . y ;
77+ } else if ( objectBounds . max . y < bounds . min . y ) {
78+ y = bounds . max . y - objectBounds . min . y ;
79+ }
80+ }
81+
82+ if ( x !== null || y !== null ) {
83+ return {
84+ x : x || 0 ,
85+ y : y || 0
86+ } ;
87+ }
88+ }
89+ } ,
90+
91+ Body : {
92+ /**
93+ * Wraps the `body` position such that it always stays within the given bounds.
94+ * Upon crossing a boundary the body will appear on the opposite side of the bounds,
95+ * while maintaining its velocity.
96+ * This is called automatically by the plugin.
97+ * @function MatterWrap.Body.wrap
98+ * @param {Matter.Body } body The body to wrap.
99+ * @param {Matter.Bounds } bounds The bounds to wrap the body inside.
100+ * @returns {?Matter.Vector } The translation vector that was applied (only if wrapping was required).
101+ */
102+ wrap : function ( body , bounds ) {
103+ var translation = MatterWrap . Bounds . wrap ( body . bounds , bounds ) ;
104+
105+ if ( translation ) {
106+ Matter . Body . translate ( body , translation ) ;
107+ }
108+
109+ return translation ;
110+ }
111+ } ,
112+
113+ Composite : {
114+ /**
115+ * Returns the union of the bounds of all of the composite's bodies
116+ * (not accounting for constraints).
117+ * @function MatterWrap.Composite.bounds
118+ * @param {Matter.Composite } composite The composite.
119+ * @returns {Matter.Bounds } The composite bounds.
120+ */
121+ bounds : function ( composite ) {
122+ var bodies = Matter . Composite . allBodies ( composite ) ,
123+ vertices = [ ] ;
124+
125+ for ( var i = 0 ; i < bodies . length ; i += 1 ) {
126+ var body = bodies [ i ] ;
127+ vertices . push ( body . bounds . min , body . bounds . max ) ;
128+ }
129+
130+ return Matter . Bounds . create ( vertices ) ;
131+ } ,
132+
133+ /**
134+ * Wraps the `composite` position such that it always stays within the given bounds.
135+ * Upon crossing a boundary the composite will appear on the opposite side of the bounds,
136+ * while maintaining its velocity.
137+ * This is called automatically by the plugin.
138+ * @function MatterWrap.Composite.wrap
139+ * @param {Matter.Composite } composite The composite to wrap.
140+ * @param {Matter.Bounds } bounds The bounds to wrap the composite inside.
141+ * @returns {?Matter.Vector } The translation vector that was applied (only if wrapping was required).
142+ */
143+ wrap : function ( composite , bounds ) {
144+ var translation = MatterWrap . Bounds . wrap (
145+ MatterWrap . Composite . bounds ( composite ) ,
146+ bounds
147+ ) ;
148+
149+ if ( translation ) {
150+ Matter . Composite . translate ( composite , translation ) ;
151+ }
152+
153+ return translation ;
154+ }
155+ }
156+ } ;
157+
158+ module . exports = MatterWrap ;
159+
160+ /**
161+ * @namespace Matter.Body
162+ * @see http://brm.io/matter-js/docs/classes/Body.html
163+ */
164+
165+ /**
166+ * This plugin adds a new property `body.plugin.wrap` to instances of `Matter.Body`.
167+ * This is a `Matter.Bounds` instance that specifies the wrapping region.
168+ * @property {Matter.Bounds } body.plugin.wrap
169+ * @memberof Matter.Body
170+ */
171+
172+ /**
173+ * This plugin adds a new property `composite.plugin.wrap` to instances of `Matter.Composite`.
174+ * This is a `Matter.Bounds` instance that specifies the wrapping region.
175+ * @property {Matter.Bounds } composite.plugin.wrap
176+ * @memberof Matter.Composite
177+ */
0 commit comments