Skip to content

Commit

Permalink
update timing improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Dec 23, 2021
1 parent 4230a8b commit 10a2a07
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 40 deletions.
3 changes: 1 addition & 2 deletions examples/ragdoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/timescale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 6 additions & 22 deletions src/collision/Resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions src/collision/SAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 0 additions & 8 deletions src/core/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,7 +137,6 @@ var Common = require('./Common');
};

Events.trigger(runner, 'beforeTick', event);
Events.trigger(engine, 'beforeTick', event); // @deprecated

// fps counter
runner.frameCounter += 1;
Expand Down
9 changes: 6 additions & 3 deletions src/core/Sleeping.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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;
}
Expand Down

0 comments on commit 10a2a07

Please sign in to comment.