From 749ed5066e009dc2b4668820394884a7a1dc0578 Mon Sep 17 00:00:00 2001 From: liabru Date: Thu, 4 May 2017 00:31:56 +0100 Subject: [PATCH] added rotation point parameter to Body.rotate, closes #410 --- src/body/Body.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/body/Body.js b/src/body/Body.js index fb0e0dd..cc1857c 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -492,9 +492,24 @@ var Axes = require('../geometry/Axes'); * @method rotate * @param {body} body * @param {number} rotation + * @param {vector} [point] */ - Body.rotate = function(body, rotation) { - Body.setAngle(body, body.angle + rotation); + Body.rotate = function(body, rotation, point) { + if (!point) { + Body.setAngle(body, body.angle + rotation); + } else { + var cos = Math.cos(rotation), + sin = Math.sin(rotation), + 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.setAngle(body, body.angle + rotation); + } }; /**