diff --git a/src/body/Body.js b/src/body/Body.js index 2c92c29..9f6c7f8 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 a302732..5d529a3 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 ab8dcf7..68739a9 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;