Skip to content

Commit 4c4962f

Browse files
committed
added Composite.translate, Composite.rotate, Composite.scale
1 parent 2fa1570 commit 4c4962f

1 file changed

Lines changed: 81 additions & 2 deletions

File tree

src/body/Composite.js

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* @class Composite
1111
*/
1212

13-
// TODO: composite translate, rotate
14-
1513
var Composite = {};
1614

1715
(function() {
@@ -441,6 +439,87 @@ var Composite = {};
441439
return composite;
442440
};
443441

442+
/**
443+
* Translates all children in the composite by a given vector relative to their current positions,
444+
* without imparting any velocity.
445+
* @method translate
446+
* @param {composite} composite
447+
* @param {vector} translation
448+
* @param {bool} [recursive=true]
449+
*/
450+
Composite.translate = function(composite, translation, recursive) {
451+
var bodies = recursive ? Composite.allBodies(composite) : composite.bodies;
452+
453+
for (var i = 0; i < bodies.length; i++) {
454+
Body.translate(bodies[i], translation);
455+
}
456+
457+
Composite.setModified(composite, true, true, false);
458+
459+
return composite;
460+
};
461+
462+
/**
463+
* Rotates all children in the composite by a given angle about the given point, without imparting any angular velocity.
464+
* @method rotate
465+
* @param {composite} composite
466+
* @param {number} rotation
467+
* @param {vector} point
468+
* @param {bool} [recursive=true]
469+
*/
470+
Composite.rotate = function(composite, rotation, point, recursive) {
471+
var cos = Math.cos(rotation),
472+
sin = Math.sin(rotation),
473+
bodies = recursive ? Composite.allBodies(composite) : composite.bodies;
474+
475+
for (var i = 0; i < bodies.length; i++) {
476+
var body = bodies[i],
477+
dx = body.position.x - point.x,
478+
dy = body.position.y - point.y;
479+
480+
Body.setPosition(body, {
481+
x: point.x + (dx * cos - dy * sin),
482+
y: point.y + (dx * sin + dy * cos)
483+
});
484+
485+
Body.rotate(body, rotation);
486+
}
487+
488+
Composite.setModified(composite, true, true, false);
489+
490+
return composite;
491+
};
492+
493+
/**
494+
* Scales all children in the composite, including updating physical properties (mass, area, axes, inertia), from a world-space point.
495+
* @method scale
496+
* @param {composite} composite
497+
* @param {number} scaleX
498+
* @param {number} scaleY
499+
* @param {vector} point
500+
* @param {bool} [recursive=true]
501+
*/
502+
Composite.scale = function(composite, scaleX, scaleY, point, recursive) {
503+
var bodies = recursive ? Composite.allBodies(composite) : composite.bodies;
504+
505+
for (var i = 0; i < bodies.length; i++) {
506+
var body = bodies[i],
507+
dx = body.position.x - point.x,
508+
dy = body.position.y - point.y;
509+
510+
Body.setPosition(body, {
511+
x: point.x + dx * scaleX,
512+
y: point.y + dy * scaleY
513+
});
514+
515+
Body.scale(body, scaleX, scaleY);
516+
}
517+
518+
Composite.setModified(composite, true, true, false);
519+
520+
return composite;
521+
};
522+
444523
/*
445524
*
446525
* Events Documentation

0 commit comments

Comments
 (0)