From d52f7e6dcf27c7faa20fa52d2a9f197f96d7ed6f Mon Sep 17 00:00:00 2001 From: liabru Date: Tue, 4 Jan 2022 10:52:37 +0000 Subject: [PATCH] update body velocity properties after resolving --- src/body/Body.js | 21 +++++++++++++++++---- src/core/Engine.js | 19 ++++++++++++++++++- src/render/Render.js | 5 +---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/body/Body.js b/src/body/Body.js index 2c92c29d..9f6c7f83 100644 --- a/src/body/Body.js +++ b/src/body/Body.js @@ -747,10 +747,6 @@ var Axes = require('../geometry/Axes'); body.anglePrev = body.angle; body.angle += body.angularVelocity; - // track speed and acceleration - body.speed = Vector.magnitude(body.velocity); - body.angularSpeed = Math.abs(body.angularVelocity); - // transform the body geometry for (var i = 0; i < body.parts.length; i++) { var part = body.parts[i]; @@ -774,6 +770,23 @@ var Axes = require('../geometry/Axes'); } }; + /** + * Updates properties `body.velocity`, `body.speed`, `body.angularVelocity` and `body.angularSpeed`. + * @method updateVelocities + * @param {body} body + */ + Body.updateVelocities = function(body) { + var timeScale = Common._timeUnit / body.deltaTime, + bodyVelocity = body.velocity; + + bodyVelocity.x = (body.position.x - body.positionPrev.x) * timeScale; + bodyVelocity.y = (body.position.y - body.positionPrev.y) * timeScale; + body.speed = Math.sqrt((bodyVelocity.x * bodyVelocity.x) + (bodyVelocity.y * bodyVelocity.y)); + + body.angularVelocity = (body.angle - body.anglePrev) * timeScale; + body.angularSpeed = Math.abs(body.angularVelocity); + }; + /** * Applies a force to a body from a given world-space position, including resulting torque. * @method applyForce diff --git a/src/core/Engine.js b/src/core/Engine.js index a3027322..5d529a38 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -112,7 +112,7 @@ var Body = require('../body/Body'); Detector.setBodies(detector, allBodies); } - // reset all composite modified flags + // reset all composite modified flags if (world.isModified) { Composite.setModified(world, false, false, true); } @@ -171,6 +171,9 @@ var Body = require('../body/Body'); Resolver.solveVelocity(pairs.list, delta); } + // update body speed and velocity properties + Engine._bodiesUpdateVelocities(allBodies); + // trigger collision events if (pairs.collisionActive.length > 0) Events.trigger(engine, 'collisionActive', { pairs: pairs.collisionActive }); @@ -284,6 +287,20 @@ var Body = require('../body/Body'); } }; + /** + * Applies `Body.updateVelocities` to all given `bodies`. + * @method _bodiesUpdate + * @private + * @param {body[]} bodies + */ + Engine._bodiesUpdateVelocities = function(bodies) { + var bodiesLength = bodies.length; + + for (var i = 0; i < bodiesLength; i++) { + Body.updateVelocities(bodies[i]); + } + }; + /** * A deprecated alias for `Runner.run`, use `Matter.Runner.run(engine)` instead and see `Matter.Runner` for more information. * @deprecated use Matter.Runner.run(engine) instead diff --git a/src/render/Render.js b/src/render/Render.js index ab8dcf7a..68739a97 100644 --- a/src/render/Render.js +++ b/src/render/Render.js @@ -10,7 +10,6 @@ var Render = {}; module.exports = Render; -var Body = require('../body/Body'); var Common = require('../core/Common'); var Composite = require('../body/Composite'); var Bounds = require('../geometry/Bounds'); @@ -1107,10 +1106,8 @@ var Mouse = require('../core/Mouse'); if (!body.render.visible) continue; - var velocity = Body.getVelocity(body); - c.moveTo(body.position.x, body.position.y); - c.lineTo(body.position.x + velocity.x * 2, body.position.y + velocity.y * 2); + c.lineTo(body.position.x + body.velocity.x * 2, body.position.y + body.velocity.y * 2); } c.lineWidth = 3;