mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
added constraint damping
This commit is contained in:
parent
fe9b05d520
commit
44c4d1ff8d
1 changed files with 44 additions and 3 deletions
|
@ -28,7 +28,8 @@ var Common = require('../core/Common');
|
|||
/**
|
||||
* Creates a new constraint.
|
||||
* All properties have default values, and many are pre-calculated automatically based on other properties.
|
||||
* To simulate a revolute constraint (or pin joint) set `length: 0` and `stiffness: 1`.
|
||||
* To simulate a revolute constraint (or pin joint) set `length: 0` and a high `stiffness` value (e.g. `0.7` or above).
|
||||
* If the constraint is unstable, try lowering the `stiffness` value and / or increasing `engine.constraintIterations`.
|
||||
* See the properties section below for detailed information on what you can pass via the `options` object.
|
||||
* @method create
|
||||
* @param {} options
|
||||
|
@ -63,7 +64,8 @@ var Common = require('../core/Common');
|
|||
constraint.id = constraint.id || Common.nextId();
|
||||
constraint.label = constraint.label || 'Constraint';
|
||||
constraint.type = 'constraint';
|
||||
constraint.stiffness = constraint.stiffness || 1;
|
||||
constraint.stiffness = constraint.stiffness || (constraint.length > 0 ? 1 : 0.7);
|
||||
constraint.damping = constraint.damping || 0;
|
||||
constraint.angularStiffness = constraint.angularStiffness || 0;
|
||||
constraint.angleA = constraint.bodyA ? constraint.bodyA.angle : constraint.angleA;
|
||||
constraint.angleB = constraint.bodyB ? constraint.bodyB.angle : constraint.angleB;
|
||||
|
@ -177,7 +179,22 @@ var Common = require('../core/Common');
|
|||
inertiaTotal = (bodyA ? bodyA.inverseInertia : 0) + (bodyB ? bodyB.inverseInertia : 0),
|
||||
resistanceTotal = massTotal + inertiaTotal,
|
||||
torque,
|
||||
share;
|
||||
share,
|
||||
normal,
|
||||
normalVelocity,
|
||||
relativeVelocity;
|
||||
|
||||
if (constraint.damping) {
|
||||
var zero = Vector.create();
|
||||
normal = Vector.div(delta, currentLength);
|
||||
|
||||
relativeVelocity = Vector.sub(
|
||||
bodyB && Vector.sub(bodyB.position, bodyB.positionPrev) || zero,
|
||||
bodyA && Vector.sub(bodyA.position, bodyA.positionPrev) || zero
|
||||
);
|
||||
|
||||
normalVelocity = Vector.dot(normal, relativeVelocity);
|
||||
}
|
||||
|
||||
if (bodyA && !bodyA.isStatic) {
|
||||
share = bodyA.inverseMass / massTotal;
|
||||
|
@ -190,6 +207,12 @@ var Common = require('../core/Common');
|
|||
bodyA.position.x -= force.x * share;
|
||||
bodyA.position.y -= force.y * share;
|
||||
|
||||
// apply damping
|
||||
if (constraint.damping) {
|
||||
bodyA.positionPrev.x -= constraint.damping * normal.x * normalVelocity * share;
|
||||
bodyA.positionPrev.y -= constraint.damping * normal.y * normalVelocity * share;
|
||||
}
|
||||
|
||||
// apply torque
|
||||
torque = (Vector.cross(pointA, force) / resistanceTotal) * Constraint._torqueDampen * bodyA.inverseInertia * (1 - constraint.angularStiffness);
|
||||
bodyA.constraintImpulse.angle -= torque;
|
||||
|
@ -207,6 +230,12 @@ var Common = require('../core/Common');
|
|||
bodyB.position.x += force.x * share;
|
||||
bodyB.position.y += force.y * share;
|
||||
|
||||
// apply damping
|
||||
if (constraint.damping) {
|
||||
bodyB.positionPrev.x += constraint.damping * normal.x * normalVelocity * share;
|
||||
bodyB.positionPrev.y += constraint.damping * normal.y * normalVelocity * share;
|
||||
}
|
||||
|
||||
// apply torque
|
||||
torque = (Vector.cross(pointB, force) / resistanceTotal) * Constraint._torqueDampen * bodyB.inverseInertia * (1 - constraint.angularStiffness);
|
||||
bodyB.constraintImpulse.angle += torque;
|
||||
|
@ -366,6 +395,18 @@ var Common = require('../core/Common');
|
|||
* @default 1
|
||||
*/
|
||||
|
||||
/**
|
||||
* A `Number` that specifies the damping of the constraint,
|
||||
* i.e. the amount of resistance applied to each body based on their velocities to limit the amount of oscillation.
|
||||
* Damping will only be apparent when the constraint also has a very low `stiffness`.
|
||||
* A value of `0.1` means the constraint will apply heavy damping, resulting in little to no oscillation.
|
||||
* A value of `0` means the constraint will apply no damping.
|
||||
*
|
||||
* @property damping
|
||||
* @type number
|
||||
* @default 0
|
||||
*/
|
||||
|
||||
/**
|
||||
* A `Number` that specifies the target resting length of the constraint.
|
||||
* It is calculated automatically in `Constraint.create` from initial positions of the `constraint.bodyA` and `constraint.bodyB`.
|
||||
|
|
Loading…
Reference in a new issue