@@ -117,6 +117,7 @@ var Body = {};
117117 vertices : body . vertices ,
118118 isStatic : body . isStatic ,
119119 isSleeping : body . isSleeping ,
120+ parent : body . parent || body ,
120121 parts : body . parts || [ body ]
121122 } ) ;
122123
@@ -194,6 +195,9 @@ var Body = {};
194195 case 'angularVelocity' :
195196 Body . setAngularVelocity ( body , value ) ;
196197 break ;
198+ case 'parts' :
199+ Body . setParts ( body , value ) ;
200+ break ;
197201 default :
198202 body [ property ] = value ;
199203
@@ -298,6 +302,78 @@ var Body = {};
298302 Bounds . update ( body . bounds , body . vertices , body . velocity ) ;
299303 } ;
300304
305+ /**
306+ * Sets the parts of the `body` and updates mass, inertia and centroid.
307+ * Each part will have its parent set to `body`.
308+ * By default the convex hull will be automatically computed and set on `body`, unless `autoHull` is set to `false.`
309+ * Note that this method will ensure that the first part in `body.parts` will always be the `body`.
310+ * @method setParts
311+ * @param {body } body
312+ * @param [body] parts
313+ * @param {bool } [autoHull=true]
314+ */
315+ Body . setParts = function ( body , parts , autoHull ) {
316+ autoHull = typeof autoHull !== 'undefined' ? autoHull : true ;
317+
318+ // ensure the body is always at index 0
319+ var index = Common . indexOf ( parts , body ) ;
320+ if ( index > - 1 ) {
321+ parts . splice ( index , 1 ) ;
322+ }
323+
324+ parts . unshift ( body ) ;
325+ body . parts = parts ;
326+
327+ if ( parts . length === 1 )
328+ return ;
329+
330+ var i ;
331+
332+ // find the convex hull of all parts to set on the parent body
333+ if ( autoHull ) {
334+ var vertices = [ ] ;
335+ for ( i = 1 ; i < parts . length ; i ++ ) {
336+ vertices = vertices . concat ( parts [ i ] . vertices ) ;
337+ }
338+
339+ Vertices . clockwiseSort ( vertices ) ;
340+
341+ var hull = Vertices . hull ( vertices ) ,
342+ hullCentre = Vertices . centre ( hull ) ;
343+
344+ Body . setVertices ( body , hull ) ;
345+ Body . setPosition ( body , hullCentre ) ;
346+ }
347+
348+ // find the combined properties of all parts to set on the parent body
349+ var mass = 0 ,
350+ area = 0 ,
351+ inertia = 0 ,
352+ centroid = { x : 0 , y : 0 } ;
353+
354+ for ( i = 1 ; i < parts . length ; i ++ ) {
355+ var part = parts [ i ] ;
356+ part . parent = body ;
357+ mass += part . mass ;
358+ area += part . area ;
359+ inertia += part . inertia ;
360+ Vector . add ( centroid , part . position , centroid ) ;
361+ }
362+
363+ centroid = Vector . div ( centroid , parts . length - 1 ) ;
364+
365+ body . area = area ;
366+ body . parent = body ;
367+ body . position . x = centroid . x ;
368+ body . position . y = centroid . y ;
369+ body . positionPrev . x = centroid . x ;
370+ body . positionPrev . y = centroid . y ;
371+
372+ Body . setMass ( body , mass ) ;
373+ Body . setInertia ( body , inertia ) ;
374+ Body . setPosition ( body , centroid ) ;
375+ } ;
376+
301377 /**
302378 * Sets the position of the body instantly. Velocity, angle, force etc. are unchanged.
303379 * @method setPosition
@@ -314,8 +390,6 @@ var Body = {};
314390
315391 Vertices . translate ( body . vertices , delta ) ;
316392 Bounds . update ( body . bounds , body . vertices , body . velocity ) ;
317-
318- //Common.each(body.children, Body.setPosition, position);
319393 } ;
320394
321395 /**
@@ -333,8 +407,6 @@ var Body = {};
333407 Vertices . rotate ( body . vertices , delta , body . position ) ;
334408 Axes . rotate ( body . axes , delta ) ;
335409 Bounds . update ( body . bounds , body . vertices , body . velocity ) ;
336-
337- //Common.each(body.children, Body.setAngle, angle);
338410 } ;
339411
340412 /**
@@ -452,6 +524,10 @@ var Body = {};
452524 Axes . rotate ( part . axes , body . angularVelocity ) ;
453525 }
454526 Bounds . update ( part . bounds , body . vertices , body . velocity ) ;
527+ if ( i > 0 ) {
528+ part . position . x += body . velocity . x ;
529+ part . position . y += body . velocity . y ;
530+ }
455531 }
456532 } ;
457533
0 commit comments