From d3813fc611aaf54363313c9b39f06140614d705b Mon Sep 17 00:00:00 2001 From: Gustavo Canedo Date: Mon, 1 Nov 2021 12:48:14 -0300 Subject: [PATCH] add relativeForce --- src/body/Body.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/body/Body.js b/src/body/Body.js index 3bdc75c..9407d23 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -694,6 +694,28 @@ var Axes = require('../geometry/Axes'); body.torque += offset.x * force.y - offset.y * force.x; }; + /** + * Applies a relative force to a body from a given relative position, including resulting torque. + * @method applyForce + * @param {body} body + * @param {vector} relativePosition + * @param {vector} relativeForce + */ + Body.applyRelativeForce = function(body, relativePosition, relativeForce) { + var sin = Math.sin(body.angle); + var cos = Math.cos(body.angle); + + Body.applyForce( + body, { + x: body.position.x + cos * relativePosition.x - sin * relativePosition.y, + y: body.position.y + sin * relativePosition.x + cos * relativePosition.y, + }, { + x: cos * relativeForce.x - sin * relativeForce.y, + y: sin * relativeForce.x + cos * relativeForce.y, + } + ); + }; + /** * Returns the sums of the properties of all compound parts of the parent body. * @method _totalProperties