0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-20 17:10:11 -05:00

revert Body.applyForce

This commit is contained in:
liabru 2022-12-28 20:45:37 +00:00
parent f8b64b673e
commit f7da96413b
3 changed files with 12 additions and 9 deletions

View file

@ -108,15 +108,17 @@ Example.events = function() {
Composite.add(world, stack); Composite.add(world, stack);
var shakeScene = function(engine) { var shakeScene = function(engine) {
var timeScale = (1000 / 60) / engine.timing.lastDelta;
var bodies = Composite.allBodies(engine.world); var bodies = Composite.allBodies(engine.world);
for (var i = 0; i < bodies.length; i++) { for (var i = 0; i < bodies.length; i++) {
var body = bodies[i]; var body = bodies[i];
if (!body.isStatic && body.position.y >= 500) { if (!body.isStatic && body.position.y >= 500) {
// Scale force accounting for mass // scale force for mass and time applied
var forceMagnitude = (0.03 * body.mass); var forceMagnitude = (0.03 * body.mass) * timeScale;
// apply the force over a single update
Body.applyForce(body, body.position, { Body.applyForce(body, body.position, {
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude y: -forceMagnitude + Common.random() * -forceMagnitude

View file

@ -42,16 +42,18 @@ Example.timescale = function() {
Bodies.rectangle(0, 300, 50, 600, { isStatic: true }) Bodies.rectangle(0, 300, 50, 600, { isStatic: true })
]); ]);
var explosion = function(engine) { var explosion = function(engine, delta) {
var timeScale = (1000 / 60) / delta;
var bodies = Composite.allBodies(engine.world); var bodies = Composite.allBodies(engine.world);
for (var i = 0; i < bodies.length; i++) { for (var i = 0; i < bodies.length; i++) {
var body = bodies[i]; var body = bodies[i];
if (!body.isStatic && body.position.y >= 500) { if (!body.isStatic && body.position.y >= 500) {
// Scale force accounting for mass // scale force for mass and time applied
var forceMagnitude = (0.05 * body.mass); var forceMagnitude = (0.05 * body.mass) * timeScale;
// apply the force over a single update
Body.applyForce(body, body.position, { Body.applyForce(body, body.position, {
x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]), x: (forceMagnitude + Common.random() * forceMagnitude) * Common.choose([1, -1]),
y: -forceMagnitude + Common.random() * -forceMagnitude y: -forceMagnitude + Common.random() * -forceMagnitude
@ -153,4 +155,4 @@ Example.timescale.for = '>=0.14.2';
if (typeof module !== 'undefined') { if (typeof module !== 'undefined') {
module.exports = Example.timescale; module.exports = Example.timescale;
} }

View file

@ -813,10 +813,9 @@ var Axes = require('../geometry/Axes');
* @param {vector} force * @param {vector} force
*/ */
Body.applyForce = function(body, position, force) { Body.applyForce = function(body, position, force) {
var timeScale = body.deltaTime / Body._baseDelta;
body.force.x += force.x / timeScale;
body.force.y += force.y / timeScale;
var offset = { x: position.x - body.position.x, y: position.y - body.position.y }; var offset = { x: position.x - body.position.x, y: position.y - body.position.y };
body.force.x += force.x;
body.force.y += force.y;
body.torque += offset.x * force.y - offset.y * force.x; body.torque += offset.x * force.y - offset.y * force.x;
}; };