0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

fixed constraint torque calculation

This commit is contained in:
liabru 2017-04-29 21:17:09 +01:00
parent daf26af006
commit 068fdc0f2a

View file

@ -29,6 +29,7 @@ var Common = require('../core/Common');
/** /**
* Creates a new constraint. * Creates a new constraint.
* All properties have default values, and many are pre-calculated automatically based on other properties. * 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`.
* See the properties section below for detailed information on what you can pass via the `options` object. * See the properties section below for detailed information on what you can pass via the `options` object.
* @method create * @method create
* @param {} options * @param {} options
@ -179,12 +180,13 @@ var Common = require('../core/Common');
relativeVelocity = Vector.sub(velocityPointB, velocityPointA), relativeVelocity = Vector.sub(velocityPointB, velocityPointA),
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),
normalImpulse = Vector.dot(normal, relativeVelocity) / (massTotal + inertiaTotal), resistanceTotal = massTotal + inertiaTotal,
normalImpulse = Vector.dot(normal, relativeVelocity) / resistanceTotal,
normalVelocity, normalVelocity,
torque, torque,
share; share;
if (normalImpulse < 0) { if (normalImpulse < 0 && constraint.angularStiffness < 1) {
normalVelocity = { normalVelocity = {
x: normal.x * normalImpulse, x: normal.x * normalImpulse,
y: normal.y * normalImpulse y: normal.y * normalImpulse
@ -203,7 +205,8 @@ var Common = require('../core/Common');
bodyA.position.y -= force.y * share; bodyA.position.y -= force.y * share;
if (normalVelocity) { if (normalVelocity) {
torque = Vector.cross(pointA, normalVelocity) * bodyA.inverseInertia * (1 - constraint.angularStiffness); share = (bodyA.inverseInertia + bodyA.inverseMass) / resistanceTotal;
torque = Vector.cross(pointA, normalVelocity) * share * bodyA.inverseInertia * constraint.stiffness * (1 - constraint.angularStiffness);
bodyA.constraintImpulse.angle += torque; bodyA.constraintImpulse.angle += torque;
bodyA.angle += torque; bodyA.angle += torque;
} }
@ -221,7 +224,8 @@ var Common = require('../core/Common');
bodyB.position.y += force.y * share; bodyB.position.y += force.y * share;
if (normalVelocity) { if (normalVelocity) {
torque = Vector.cross(pointB, normalVelocity) * bodyB.inverseInertia * (1 - constraint.angularStiffness); share = (bodyB.inverseInertia + bodyB.inverseMass) / resistanceTotal;
torque = Vector.cross(pointB, normalVelocity) * share * bodyB.inverseInertia * constraint.stiffness * (1 - constraint.angularStiffness);
bodyB.constraintImpulse.angle -= torque; bodyB.constraintImpulse.angle -= torque;
bodyB.angle -= torque; bodyB.angle -= torque;
} }