0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-12-02 10:49:45 -05:00

improve delta factors in resolver and constraint stiffness

This commit is contained in:
liabru 2022-08-05 23:41:24 +01:00
parent a5729684d1
commit 07d13fe903
3 changed files with 8 additions and 5 deletions

View file

@ -49,9 +49,10 @@ var Bounds = require('../geometry/Bounds');
* Find a solution for pair positions. * Find a solution for pair positions.
* @method solvePosition * @method solvePosition
* @param {pair[]} pairs * @param {pair[]} pairs
* @param {number} delta
* @param {number} positionIterations * @param {number} positionIterations
*/ */
Resolver.solvePosition = function(pairs, positionIterations) { Resolver.solvePosition = function(pairs, delta, positionIterations) {
var i, var i,
pair, pair,
collision, collision,
@ -61,6 +62,7 @@ var Bounds = require('../geometry/Bounds');
contactShare, contactShare,
positionImpulse, positionImpulse,
positionDampen = Resolver._positionDampen * Common.clamp(20 / positionIterations, 0, 1), positionDampen = Resolver._positionDampen * Common.clamp(20 / positionIterations, 0, 1),
slopDampen = delta / Common._timeUnit,
pairsLength = pairs.length; pairsLength = pairs.length;
// find impulses required to resolve penetration // find impulses required to resolve penetration
@ -91,7 +93,7 @@ var Bounds = require('../geometry/Bounds');
bodyA = collision.parentA; bodyA = collision.parentA;
bodyB = collision.parentB; bodyB = collision.parentB;
normal = collision.normal; normal = collision.normal;
positionImpulse = pair.separation - pair.slop; positionImpulse = pair.separation - pair.slop * slopDampen;
if (bodyA.isStatic || bodyB.isStatic) if (bodyA.isStatic || bodyB.isStatic)
positionImpulse *= 2; positionImpulse *= 2;

View file

@ -186,8 +186,9 @@ var Common = require('../core/Common');
// solve distance constraint with Gauss-Siedel method // solve distance constraint with Gauss-Siedel method
var difference = (currentLength - constraint.length) / currentLength, var difference = (currentLength - constraint.length) / currentLength,
isRigid = constraint.stiffness >= 1 || constraint.length === 0, isRigid = constraint.stiffness >= 1 || constraint.length === 0,
stiffness = isRigid ? constraint.stiffness : constraint.stiffness * timeScale * timeScale, stiffness = isRigid ? constraint.stiffness * timeScale
damping = constraint.damping * timeScale, : constraint.stiffness * timeScale * timeScale,
damping = constraint.damping * timeScale * timeScale,
force = Vector.mult(delta, difference * stiffness), force = Vector.mult(delta, difference * stiffness),
massTotal = (bodyA ? bodyA.inverseMass : 0) + (bodyB ? bodyB.inverseMass : 0), massTotal = (bodyA ? bodyA.inverseMass : 0) + (bodyB ? bodyB.inverseMass : 0),
inertiaTotal = (bodyA ? bodyA.inverseInertia : 0) + (bodyB ? bodyB.inverseInertia : 0), inertiaTotal = (bodyA ? bodyA.inverseInertia : 0) + (bodyB ? bodyB.inverseInertia : 0),

View file

@ -149,7 +149,7 @@ var Body = require('../body/Body');
// iteratively resolve position between collisions // iteratively resolve position between collisions
Resolver.preSolvePosition(pairs.list); Resolver.preSolvePosition(pairs.list);
for (i = 0; i < engine.positionIterations; i++) { for (i = 0; i < engine.positionIterations; i++) {
Resolver.solvePosition(pairs.list, engine.positionIterations); Resolver.solvePosition(pairs.list, delta, engine.positionIterations);
} }
Resolver.postSolvePosition(allBodies); Resolver.postSolvePosition(allBodies);