Skip to content

Commit

Permalink
added Composite.translate, Composite.rotate, Composite.scale
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jul 30, 2014
1 parent 2fa1570 commit 4c4962f
Showing 1 changed file with 81 additions and 2 deletions.
83 changes: 81 additions & 2 deletions src/body/Composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* @class Composite
*/

// TODO: composite translate, rotate

var Composite = {};

(function() {
Expand Down Expand Up @@ -441,6 +439,87 @@ var Composite = {};
return composite;
};

/**
* Translates all children in the composite by a given vector relative to their current positions,
* without imparting any velocity.
* @method translate
* @param {composite} composite
* @param {vector} translation
* @param {bool} [recursive=true]
*/
Composite.translate = function(composite, translation, recursive) {
var bodies = recursive ? Composite.allBodies(composite) : composite.bodies;

for (var i = 0; i < bodies.length; i++) {
Body.translate(bodies[i], translation);
}

Composite.setModified(composite, true, true, false);

return composite;
};

/**
* Rotates all children in the composite by a given angle about the given point, without imparting any angular velocity.
* @method rotate
* @param {composite} composite
* @param {number} rotation
* @param {vector} point
* @param {bool} [recursive=true]
*/
Composite.rotate = function(composite, rotation, point, recursive) {
var cos = Math.cos(rotation),
sin = Math.sin(rotation),
bodies = recursive ? Composite.allBodies(composite) : composite.bodies;

for (var i = 0; i < bodies.length; i++) {
var body = bodies[i],
dx = body.position.x - point.x,
dy = body.position.y - point.y;

Body.setPosition(body, {
x: point.x + (dx * cos - dy * sin),
y: point.y + (dx * sin + dy * cos)
});

Body.rotate(body, rotation);
}

Composite.setModified(composite, true, true, false);

return composite;
};

/**
* Scales all children in the composite, including updating physical properties (mass, area, axes, inertia), from a world-space point.
* @method scale
* @param {composite} composite
* @param {number} scaleX
* @param {number} scaleY
* @param {vector} point
* @param {bool} [recursive=true]
*/
Composite.scale = function(composite, scaleX, scaleY, point, recursive) {
var bodies = recursive ? Composite.allBodies(composite) : composite.bodies;

for (var i = 0; i < bodies.length; i++) {
var body = bodies[i],
dx = body.position.x - point.x,
dy = body.position.y - point.y;

Body.setPosition(body, {
x: point.x + dx * scaleX,
y: point.y + dy * scaleY
});

Body.scale(body, scaleX, scaleY);
}

Composite.setModified(composite, true, true, false);

return composite;
};

/*
*
* Events Documentation
Expand Down

0 comments on commit 4c4962f

Please sign in to comment.