mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
update timing improvements
This commit is contained in:
parent
4230a8bec6
commit
10a2a07ec2
7 changed files with 16 additions and 40 deletions
|
@ -83,7 +83,7 @@ Example.ragdoll = function() {
|
||||||
lastTime = Common.now();
|
lastTime = Common.now();
|
||||||
|
|
||||||
Events.on(engine, 'afterUpdate', function(event) {
|
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
|
// tween the timescale for slow-mo
|
||||||
if (mouse.button === -1) {
|
if (mouse.button === -1) {
|
||||||
|
@ -94,7 +94,6 @@ Example.ragdoll = function() {
|
||||||
|
|
||||||
// every 1.5 sec (real time)
|
// every 1.5 sec (real time)
|
||||||
if (Common.now() - lastTime >= 2000) {
|
if (Common.now() - lastTime >= 2000) {
|
||||||
|
|
||||||
// flip the timescale
|
// flip the timescale
|
||||||
if (timeScaleTarget < 1) {
|
if (timeScaleTarget < 1) {
|
||||||
timeScaleTarget = 1;
|
timeScaleTarget = 1;
|
||||||
|
|
|
@ -65,7 +65,7 @@ Example.timescale = function() {
|
||||||
lastTime = Common.now();
|
lastTime = Common.now();
|
||||||
|
|
||||||
Events.on(engine, 'afterUpdate', function(event) {
|
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
|
// tween the timescale for bullet time slow-mo
|
||||||
engine.timing.timeScale += (timeScaleTarget - engine.timing.timeScale) * 12 * timeScale;
|
engine.timing.timeScale += (timeScaleTarget - engine.timing.timeScale) * 12 * timeScale;
|
||||||
|
|
|
@ -229,16 +229,9 @@ var Bounds = require('../geometry/Bounds');
|
||||||
var timeScale = delta / Common._timeUnit,
|
var timeScale = delta / Common._timeUnit,
|
||||||
timeScale2 = timeScale * timeScale,
|
timeScale2 = timeScale * timeScale,
|
||||||
timeScale3 = timeScale2 * timeScale,
|
timeScale3 = timeScale2 * timeScale,
|
||||||
impulse = Vector._temp[0],
|
restingThresh = Resolver._restingThresh * timeScale2,
|
||||||
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,
|
|
||||||
frictionNormalMultiplier = Resolver._frictionNormalMultiplier,
|
frictionNormalMultiplier = Resolver._frictionNormalMultiplier,
|
||||||
restingThreshTangent = Resolver._restingThreshTangent * timeScaleSquared,
|
restingThreshTangent = Resolver._restingThreshTangent * timeScale2,
|
||||||
NumberMaxValue = Number.MAX_VALUE,
|
NumberMaxValue = Number.MAX_VALUE,
|
||||||
pairsLength = pairs.length,
|
pairsLength = pairs.length,
|
||||||
tangentImpulse,
|
tangentImpulse,
|
||||||
|
@ -265,7 +258,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
contactsLength = contacts.length,
|
contactsLength = contacts.length,
|
||||||
contactShare = 1 / contactsLength,
|
contactShare = 1 / contactsLength,
|
||||||
inverseMassTotal = bodyA.inverseMass + bodyB.inverseMass,
|
inverseMassTotal = bodyA.inverseMass + bodyB.inverseMass,
|
||||||
friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier * timeScaleSquared;
|
friction = pair.friction * pair.frictionStatic * frictionNormalMultiplier * timeScale2;
|
||||||
|
|
||||||
// update body velocities
|
// update body velocities
|
||||||
bodyAVelocity.x = bodyA.position.x - bodyA.positionPrev.x;
|
bodyAVelocity.x = bodyA.position.x - bodyA.positionPrev.x;
|
||||||
|
@ -297,19 +290,10 @@ var Bounds = require('../geometry/Bounds');
|
||||||
tangentVelocity = tangentX * relativeVelocityX + tangentY * relativeVelocityY;
|
tangentVelocity = tangentX * relativeVelocityX + tangentY * relativeVelocityY;
|
||||||
|
|
||||||
// coulomb friction
|
// 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 normalOverlap = pair.separation + normalVelocity;
|
||||||
var normalForce = Math.min(normalOverlap, 1) * timeScale3;
|
var normalForce = Math.min(normalOverlap, 1) * timeScale3;
|
||||||
normalForce = normalOverlap < 0 ? 0 : normalForce;
|
normalForce = normalOverlap < 0 ? 0 : normalForce;
|
||||||
|
|
||||||
var frictionLimit = normalForce * friction;
|
var frictionLimit = normalForce * friction;
|
||||||
|
|
||||||
if (tangentVelocity > frictionLimit || -tangentVelocity > frictionLimit) {
|
if (tangentVelocity > frictionLimit || -tangentVelocity > frictionLimit) {
|
||||||
|
@ -336,7 +320,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
tangentImpulse *= share;
|
tangentImpulse *= share;
|
||||||
|
|
||||||
// handle high velocity and resting collisions separately
|
// 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
|
// high normal velocity so clear cached contact normal impulse
|
||||||
contact.normalImpulse = 0;
|
contact.normalImpulse = 0;
|
||||||
} else {
|
} else {
|
||||||
|
@ -349,7 +333,7 @@ var Bounds = require('../geometry/Bounds');
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle high velocity and resting collisions separately
|
// 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
|
// high tangent velocity so clear cached contact tangent impulse
|
||||||
contact.tangentImpulse = 0;
|
contact.tangentImpulse = 0;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -20,8 +20,6 @@ var deprecated = Common.deprecated;
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
SAT._reuseMotionThresh = 0.2;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Detect collision between two bodies using the Separating Axis Theorem.
|
* Detect collision between two bodies using the Separating Axis Theorem.
|
||||||
* @deprecated replaced by Collision.collides
|
* @deprecated replaced by Collision.collides
|
||||||
|
|
|
@ -92,8 +92,8 @@ var Body = require('../body/Body');
|
||||||
delta *= timing.timeScale;
|
delta *= timing.timeScale;
|
||||||
|
|
||||||
// increment timestamp
|
// increment timestamp
|
||||||
timing.timestamp += delta * timing.timeScale;
|
timing.timestamp += delta;
|
||||||
timing.lastDelta = delta * timing.timeScale;
|
timing.lastDelta = delta;
|
||||||
|
|
||||||
// create an event object
|
// create an event object
|
||||||
var event = {
|
var event = {
|
||||||
|
|
|
@ -110,13 +110,6 @@ var Common = require('./Common');
|
||||||
var timing = engine.timing,
|
var timing = engine.timing,
|
||||||
delta;
|
delta;
|
||||||
|
|
||||||
// create an event object
|
|
||||||
var event = {
|
|
||||||
timestamp: timing.timestamp
|
|
||||||
};
|
|
||||||
|
|
||||||
Events.trigger(runner, 'beforeTick', event);
|
|
||||||
|
|
||||||
if (runner.isFixed) {
|
if (runner.isFixed) {
|
||||||
// fixed timestep
|
// fixed timestep
|
||||||
delta = runner.delta;
|
delta = runner.delta;
|
||||||
|
@ -144,7 +137,6 @@ var Common = require('./Common');
|
||||||
};
|
};
|
||||||
|
|
||||||
Events.trigger(runner, 'beforeTick', event);
|
Events.trigger(runner, 'beforeTick', event);
|
||||||
Events.trigger(engine, 'beforeTick', event); // @deprecated
|
|
||||||
|
|
||||||
// fps counter
|
// fps counter
|
||||||
runner.frameCounter += 1;
|
runner.frameCounter += 1;
|
||||||
|
|
|
@ -24,7 +24,9 @@ var Common = require('./Common');
|
||||||
* @param {number} delta
|
* @param {number} delta
|
||||||
*/
|
*/
|
||||||
Sleeping.update = function(bodies, 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
|
// update bodies sleeping status
|
||||||
for (var i = 0; i < bodies.length; i++) {
|
for (var i = 0; i < bodies.length; i++) {
|
||||||
|
@ -43,11 +45,12 @@ var Common = require('./Common');
|
||||||
// biased average motion estimation between frames
|
// biased average motion estimation between frames
|
||||||
body.motion = Sleeping._minBias * minMotion + (1 - Sleeping._minBias) * maxMotion;
|
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;
|
body.sleepCounter += 1;
|
||||||
|
|
||||||
if (body.sleepCounter >= body.sleepThreshold / timeScale)
|
if (body.sleepCounter >= body.sleepThreshold / timeScale) {
|
||||||
Sleeping.set(body, true);
|
Sleeping.set(body, true);
|
||||||
|
}
|
||||||
} else if (body.sleepCounter > 0) {
|
} else if (body.sleepCounter > 0) {
|
||||||
body.sleepCounter -= 1;
|
body.sleepCounter -= 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue