Skip to content

Commit 068fdc0

Browse files
committed
fixed constraint torque calculation
1 parent daf26af commit 068fdc0

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/constraint/Constraint.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ var Common = require('../core/Common');
2929
/**
3030
* Creates a new constraint.
3131
* All properties have default values, and many are pre-calculated automatically based on other properties.
32+
* To simulate a revolute constraint (or pin joint) set `length: 0` and `stiffness: 1`.
3233
* See the properties section below for detailed information on what you can pass via the `options` object.
3334
* @method create
3435
* @param {} options
@@ -179,12 +180,13 @@ var Common = require('../core/Common');
179180
relativeVelocity = Vector.sub(velocityPointB, velocityPointA),
180181
massTotal = (bodyA ? bodyA.inverseMass : 0) + (bodyB ? bodyB.inverseMass : 0),
181182
inertiaTotal = (bodyA ? bodyA.inverseInertia : 0) + (bodyB ? bodyB.inverseInertia : 0),
182-
normalImpulse = Vector.dot(normal, relativeVelocity) / (massTotal + inertiaTotal),
183+
resistanceTotal = massTotal + inertiaTotal,
184+
normalImpulse = Vector.dot(normal, relativeVelocity) / resistanceTotal,
183185
normalVelocity,
184186
torque,
185187
share;
186188

187-
if (normalImpulse < 0) {
189+
if (normalImpulse < 0 && constraint.angularStiffness < 1) {
188190
normalVelocity = {
189191
x: normal.x * normalImpulse,
190192
y: normal.y * normalImpulse
@@ -203,7 +205,8 @@ var Common = require('../core/Common');
203205
bodyA.position.y -= force.y * share;
204206

205207
if (normalVelocity) {
206-
torque = Vector.cross(pointA, normalVelocity) * bodyA.inverseInertia * (1 - constraint.angularStiffness);
208+
share = (bodyA.inverseInertia + bodyA.inverseMass) / resistanceTotal;
209+
torque = Vector.cross(pointA, normalVelocity) * share * bodyA.inverseInertia * constraint.stiffness * (1 - constraint.angularStiffness);
207210
bodyA.constraintImpulse.angle += torque;
208211
bodyA.angle += torque;
209212
}
@@ -221,7 +224,8 @@ var Common = require('../core/Common');
221224
bodyB.position.y += force.y * share;
222225

223226
if (normalVelocity) {
224-
torque = Vector.cross(pointB, normalVelocity) * bodyB.inverseInertia * (1 - constraint.angularStiffness);
227+
share = (bodyB.inverseInertia + bodyB.inverseMass) / resistanceTotal;
228+
torque = Vector.cross(pointB, normalVelocity) * share * bodyB.inverseInertia * constraint.stiffness * (1 - constraint.angularStiffness);
225229
bodyB.constraintImpulse.angle -= torque;
226230
bodyB.angle -= torque;
227231
}

0 commit comments

Comments
 (0)