Skip to content

Commit

Permalink
update body velocity properties after resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jan 4, 2022
1 parent 7130c4a commit d52f7e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/body/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/render/Render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d52f7e6

Please sign in to comment.