0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-27 13:59:01 -05:00

update timing improvements

This commit is contained in:
liabru 2021-12-23 12:43:40 +00:00
parent 4230a8bec6
commit 10a2a07ec2
7 changed files with 16 additions and 40 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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