From 10a2a07ec277cc65d0c0edffbe74262b500e8aa0 Mon Sep 17 00:00:00 2001 From: liabru Date: Thu, 23 Dec 2021 12:43:40 +0000 Subject: [PATCH] update timing improvements --- examples/ragdoll.js | 3 +-- examples/timescale.js | 2 +- src/collision/Resolver.js | 28 ++++++---------------------- src/collision/SAT.js | 2 -- src/core/Engine.js | 4 ++-- src/core/Runner.js | 8 -------- src/core/Sleeping.js | 9 ++++++--- 7 files changed, 16 insertions(+), 40 deletions(-) diff --git a/examples/ragdoll.js b/examples/ragdoll.js index 31f4ec1..12eeba4 100644 --- a/examples/ragdoll.js +++ b/examples/ragdoll.js @@ -83,7 +83,7 @@ Example.ragdoll = function() { lastTime = Common.now(); Events.on(engine, 'afterUpdate', function(event) { - var timeScale = event.delta / 1000; + var timeScale = timeScale = (event.delta || (1000 / 60)) / 1000; // tween the timescale for slow-mo if (mouse.button === -1) { @@ -94,7 +94,6 @@ Example.ragdoll = function() { // every 1.5 sec (real time) if (Common.now() - lastTime >= 2000) { - // flip the timescale if (timeScaleTarget < 1) { timeScaleTarget = 1; diff --git a/examples/timescale.js b/examples/timescale.js index e4ce018..3bd8d57 100644 --- a/examples/timescale.js +++ b/examples/timescale.js @@ -65,7 +65,7 @@ Example.timescale = function() { lastTime = Common.now(); Events.on(engine, 'afterUpdate', function(event) { - var timeScale = event.delta / 1000; + var timeScale = timeScale = (event.delta || (1000 / 60)) / 1000; // tween the timescale for bullet time slow-mo engine.timing.timeScale += (timeScaleTarget - engine.timing.timeScale) * 12 * timeScale; diff --git a/src/collision/Resolver.js b/src/collision/Resolver.js index bd7a7b7..33775e6 100644 --- a/src/collision/Resolver.js +++ b/src/collision/Resolver.js @@ -229,16 +229,9 @@ var Bounds = require('../geometry/Bounds'); var timeScale = delta / Common._timeUnit, timeScale2 = timeScale * timeScale, timeScale3 = timeScale2 * timeScale, - impulse = Vector._temp[0], - tempA = Vector._temp[1], - tempB = Vector._temp[2], - tempC = Vector._temp[3], - tempD = Vector._temp[4], - tempE = Vector._temp[5], - timeScaleSquared = timeScale * timeScale, - restingThresh = Resolver._restingThresh * timeScaleSquared, + restingThresh = Resolver._restingThresh * timeScale2, frictionNormalMultiplier = Resolver._frictionNormalMultiplier, - restingThreshTangent = Resolver._restingThreshTangent * timeScaleSquared, + restingThreshTangent = Resolver._restingThreshTangent * timeScale2, NumberMaxValue = Number.MAX_VALUE, pairsLength = pairs.length, tangentImpulse, @@ -265,7 +258,7 @@ var Bounds = require('../geometry/Bounds'); contactsLength = contacts.length, contactShare = 1 / contactsLength, inverseMassTotal = bodyA.inverseMass + bodyB.inverseMass, - friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier * timeScaleSquared; + friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier * timeScale2; // update body velocities bodyAVelocity.x = bodyA.position.x - bodyA.positionPrev.x; @@ -297,19 +290,10 @@ var Bounds = require('../geometry/Bounds'); tangentVelocity = tangentX * relativeVelocityX + tangentY * relativeVelocityY; // coulomb friction - // var tangentImpulse = tangentVelocity, - // maxFriction = Infinity; - - // if (tangentSpeed > pair.friction * pair.frictionStatic * normalForce * timeScale3) { - // maxFriction = tangentSpeed * timeScale; - // tangentImpulse = Common.clamp( - // pair.friction * tangentVelocityDirection * timeScale3, - // -maxFriction, maxFriction - // ); var normalOverlap = pair.separation + normalVelocity; var normalForce = Math.min(normalOverlap, 1) * timeScale3; normalForce = normalOverlap < 0 ? 0 : normalForce; - + var frictionLimit = normalForce * friction; if (tangentVelocity > frictionLimit || -tangentVelocity > frictionLimit) { @@ -336,7 +320,7 @@ var Bounds = require('../geometry/Bounds'); tangentImpulse *= share; // handle high velocity and resting collisions separately - if (normalVelocity < 0 && normalVelocity * normalVelocity > Resolver._restingThresh * timeScale2) { + if (normalVelocity < 0 && normalVelocity * normalVelocity > restingThresh) { // high normal velocity so clear cached contact normal impulse contact.normalImpulse = 0; } else { @@ -349,7 +333,7 @@ var Bounds = require('../geometry/Bounds'); } // handle high velocity and resting collisions separately - if (tangentVelocity * tangentVelocity > Resolver._restingThreshTangent * timeScale2) { + if (tangentVelocity * tangentVelocity > restingThreshTangent) { // high tangent velocity so clear cached contact tangent impulse contact.tangentImpulse = 0; } else { diff --git a/src/collision/SAT.js b/src/collision/SAT.js index 7c89bd9..cc0e8c5 100644 --- a/src/collision/SAT.js +++ b/src/collision/SAT.js @@ -20,8 +20,6 @@ var deprecated = Common.deprecated; (function() { - SAT._reuseMotionThresh = 0.2; - /** * Detect collision between two bodies using the Separating Axis Theorem. * @deprecated replaced by Collision.collides diff --git a/src/core/Engine.js b/src/core/Engine.js index 69af0e5..418a1ec 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -92,8 +92,8 @@ var Body = require('../body/Body'); delta *= timing.timeScale; // increment timestamp - timing.timestamp += delta * timing.timeScale; - timing.lastDelta = delta * timing.timeScale; + timing.timestamp += delta; + timing.lastDelta = delta; // create an event object var event = { diff --git a/src/core/Runner.js b/src/core/Runner.js index f9faa99..f6464fb 100644 --- a/src/core/Runner.js +++ b/src/core/Runner.js @@ -110,13 +110,6 @@ var Common = require('./Common'); var timing = engine.timing, delta; - // create an event object - var event = { - timestamp: timing.timestamp - }; - - Events.trigger(runner, 'beforeTick', event); - if (runner.isFixed) { // fixed timestep delta = runner.delta; @@ -144,7 +137,6 @@ var Common = require('./Common'); }; Events.trigger(runner, 'beforeTick', event); - Events.trigger(engine, 'beforeTick', event); // @deprecated // fps counter runner.frameCounter += 1; diff --git a/src/core/Sleeping.js b/src/core/Sleeping.js index 53be2e8..9e0ca15 100644 --- a/src/core/Sleeping.js +++ b/src/core/Sleeping.js @@ -24,7 +24,9 @@ var Common = require('./Common'); * @param {number} delta */ Sleeping.update = function(bodies, delta) { - var timeScale = delta / Common._timeUnit; + var timeScale = delta / Common._timeUnit, + timeScale2 = timeScale * timeScale, + motionSleepThreshold = Sleeping._motionSleepThreshold * timeScale2; // update bodies sleeping status for (var i = 0; i < bodies.length; i++) { @@ -43,11 +45,12 @@ var Common = require('./Common'); // biased average motion estimation between frames body.motion = Sleeping._minBias * minMotion + (1 - Sleeping._minBias) * maxMotion; - if (body.sleepThreshold > 0 && body.motion < Sleeping._motionSleepThreshold * timeScale * timeScale) { + if (body.sleepThreshold > 0 && body.motion < motionSleepThreshold) { body.sleepCounter += 1; - if (body.sleepCounter >= body.sleepThreshold / timeScale) + if (body.sleepCounter >= body.sleepThreshold / timeScale) { Sleeping.set(body, true); + } } else if (body.sleepCounter > 0) { body.sleepCounter -= 1; }