0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-25 13:39:06 -05:00

update body velocity properties after resolving

This commit is contained in:
liabru 2022-01-04 10:52:37 +00:00
parent 7130c4ae80
commit d52f7e6dcf
3 changed files with 36 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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;