Dart Documentationbox2d_consoleFrictionJoint

FrictionJoint class

class FrictionJoint extends Joint {
  final Vector _localAnchorA;
  final Vector _localAnchorB;

  Vector _linearImpulse;
  num _angularImpulse;
  num _maxForce;
  num _maxTorque;

  FrictionJoint(FrictionJointDef def)
      : super(def),
        _localAnchorA = new Vector.copy(def.localAnchorA),
        _localAnchorB = new Vector.copy(def.localAnchorB),
        _linearImpulse = new Vector(),
        _angularImpulse = 0.0,
        _maxForce = def.maxForce,
        _maxTorque = def.maxTorque { }

  Vector getLocalAnchorA(Vector argOut) {
    bodyA.getWorldPointToOut(_localAnchorA, argOut);
  }

  Vector getLocalAnchorB(Vector argOut) {
    bodyB.getWorldPointToOut(_localAnchorB, argOut);
  }

  void getReactionForce(num inv_dt, Vector argOut) {
    argOut.setFrom(_linearImpulse).mulLocal(inv_dt);
  }

  num getReactionTorque(num inv_dt) => inv_dt * _angularImpulse;

  void set maxForce(num force) {
    assert(force >= 0.0);
    _maxForce = force;
  }

  num get maxForce() => _maxForce;

  void set maxTorque(num torque) {
    assert(torque >= 0.0);
    _maxTorque = torque;
  }

  num get maxTorque() => _maxTorque;

  void initVelocityConstraints(TimeStep step) {
    // Compute the effective mass matrix.
    Vector r1 = new Vector();
    Vector r2 = new Vector();

    r1.setFrom(_localAnchorA).subLocal(bodyA.localCenter);
    r2.setFrom(_localAnchorB).subLocal(bodyB.localCenter);
    Matrix22.mulMatrixAndVectorToOut(bodyA.originTransform.rotation, r1, r1);
    Matrix22.mulMatrixAndVectorToOut(bodyB.originTransform.rotation, r2, r2);

    // J = [-I -r1_skew I r2_skew]
    // [ 0 -1 0 1]
    // r_skew = [-ry; rx]

    // Matlab
    // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
    // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
    // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]

    Matrix22 K = new Matrix22();
    K.col1.x = bodyA.invMass + bodyB.invMass +
               bodyA.invInertia * r1.y * r1.y + bodyB.invInertia * r2.y * r2.y;
    K.col1.y = -bodyA.invInertia * r1.x * r1.y - bodyB.invInertia * r2.x * r2.y;
    K.col2.x = K.col1.y;
    K.col2.y = bodyA.invMass + bodyB.invMass +
               bodyA.invInertia * r1.x * r1.x + bodyB.invInertia * r2.x * r2.x;

    Matrix22 linearMass = new Matrix22();
    linearMass.setFrom(K);
    linearMass.invertLocal();

    num angularMass = bodyA.invInertia + bodyB.invInertia;
    if (angularMass > 0.0) {
      angularMass = 1.0 / angularMass;
    }

    if (step.warmStarting) {
      // Scale impulses.
      _linearImpulse.mulLocal(step.dtRatio);
      _angularImpulse *= step.dtRatio;

      Vector P = new Vector();
      P.setFrom(_linearImpulse);

      bodyA.linearVelocity.x -= bodyA.invMass * P.x;
      bodyA.linearVelocity.y -= bodyA.invMass * P.y;
      bodyA.angularVelocity -= bodyA.invInertia * (Vector.crossVectors(r1, P) + _angularImpulse);

      bodyB.linearVelocity.x += bodyB.invMass * P.x;
      bodyB.linearVelocity.y += bodyB.invMass * P.y;
      bodyB.angularVelocity += bodyB.invInertia * (Vector.crossVectors(r2, P) + _angularImpulse);
    } else {
      _linearImpulse.setZero();
      _angularImpulse = 0.0;
    }
  }

  void solveVelocityConstraints(TimeStep step) {
    // Solve angular friction
    {
      final num Cdot = bodyB.angularVelocity - bodyA.angularVelocity;
      num angularMass = bodyA.invInertia + bodyB.invInertia;
      if (angularMass > 0.0) {
        angularMass = 1.0 / angularMass;
      }
      num impulse = -angularMass * Cdot;

      final num oldImpulse = _angularImpulse;
      final num maxImpulse = step.dt * _maxTorque;
      _angularImpulse = MathBox.clamp(_angularImpulse + impulse, -maxImpulse, maxImpulse);
      impulse = _angularImpulse - oldImpulse;

      bodyA.angularVelocity -= bodyA.invInertia * impulse;
      bodyB.angularVelocity += bodyB.invInertia * impulse;
    }

    // Solve linear friction
    {
      Vector r1 = new Vector();
      Vector r2 = new Vector();

      r1.setFrom(_localAnchorA).subLocal(bodyA.localCenter);
      r2.setFrom(_localAnchorB).subLocal(bodyB.localCenter);
      Matrix22.mulMatrixAndVectorToOut(bodyA.originTransform.rotation, r1, r1);
      Matrix22.mulMatrixAndVectorToOut(bodyB.originTransform.rotation, r2, r2);

      Vector temp = new Vector();
      Vector Cdot = new Vector();

      Vector.crossNumAndVectorToOut(bodyA.angularVelocity, r1, temp);
      Vector.crossNumAndVectorToOut(bodyB.angularVelocity, r2, Cdot);

      Cdot.addLocal(bodyB.linearVelocity).subLocal(bodyA.linearVelocity).subLocal(temp);

      Matrix22 K = new Matrix22();
      K.col1.x = bodyA.invMass + bodyB.invMass +
                 bodyA.invInertia * r1.y * r1.y + bodyB.invInertia * r2.y * r2.y;
      K.col1.y = -bodyA.invInertia * r1.x * r1.y - bodyB.invInertia * r2.x * r2.y;
      K.col2.x = K.col1.y;
      K.col2.y = bodyA.invMass + bodyB.invMass +
                 bodyA.invInertia * r1.x * r1.x + bodyB.invInertia * r2.x * r2.x;

      Matrix22 linearMass = new Matrix22();
      linearMass.setFrom(K);
      linearMass.invertLocal();

      Vector impulse = new Vector();
      Matrix22.mulMatrixAndVectorToOut(linearMass, Cdot, impulse);
      impulse.negateLocal();

      Vector oldImpulse = new Vector();
      oldImpulse.setFrom(_linearImpulse);
      _linearImpulse.addLocal(impulse);

      num maxImpulse = step.dt * _maxForce;
      if (_linearImpulse.lengthSquared > maxImpulse * maxImpulse) {
        _linearImpulse.normalize();
        _linearImpulse.mulLocal(maxImpulse);
      }

      impulse.setFrom(_linearImpulse).subLocal(oldImpulse);

      bodyA.linearVelocity.x -= impulse.x * bodyA.invMass;
      bodyA.linearVelocity.y -= impulse.y * bodyA.invMass;
      bodyA.angularVelocity -= bodyA.invInertia * Vector.crossVectors(r1, impulse);

      bodyB.linearVelocity.x += impulse.x * bodyB.invMass;
      bodyB.linearVelocity.y += impulse.y * bodyB.invMass;
      bodyB.angularVelocity += bodyB.invInertia * Vector.crossVectors(r2, impulse);
    }
  }

  bool solvePositionConstraints(num baumgarte) => true;
}

Extends

Joint > FrictionJoint

Constructors

new FrictionJoint(FrictionJointDef def) #

FrictionJoint(FrictionJointDef def)
    : super(def),
      _localAnchorA = new Vector.copy(def.localAnchorA),
      _localAnchorB = new Vector.copy(def.localAnchorB),
      _linearImpulse = new Vector(),
      _angularImpulse = 0.0,
      _maxForce = def.maxForce,
      _maxTorque = def.maxTorque { }

Properties

final bool active #

inherited from Joint

Short-cut function to determine if either body is inactive.

bool get active() {
  return bodyA.active && bodyB.active;
}

Body bodyA #

inherited from Joint
Body bodyA;

Body bodyB #

inherited from Joint
Body bodyB;

bool collideConnected #

inherited from Joint
bool collideConnected;

JointEdge edgeA #

inherited from Joint
JointEdge edgeA;

JointEdge edgeB #

inherited from Joint
JointEdge edgeB;

num invIA #

inherited from Joint
num invIA;

num invIB #

inherited from Joint
num invIB;

num invMassA #

inherited from Joint
num invMassA;

num invMassB #

inherited from Joint
num invMassB;

bool islandFlag #

inherited from Joint
bool islandFlag;

final Vector localCenterA #

inherited from Joint
final Vector localCenterA;

final Vector localCenterB #

inherited from Joint
final Vector localCenterB;

num maxForce #

num get maxForce() => _maxForce;
void set maxForce(num force) {
  assert(force >= 0.0);
  _maxForce = force;
}

num maxTorque #

num get maxTorque() => _maxTorque;
void set maxTorque(num torque) {
  assert(torque >= 0.0);
  _maxTorque = torque;
}

final Type runtimeType #

inherited from Object

A representation of the runtime type of the object.

external Type get runtimeType;

int type #

inherited from Joint
int type;

Object userData #

inherited from Joint
Object userData;

Operators

bool operator ==(other) #

inherited from Object

The equality operator.

The default behavior for all Objects is to return true if and only if this and other are the same object.

If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.

bool operator ==(other) => identical(this, other);

Methods

void destructor() #

inherited from Joint

Override to handle destruction of joint

void destructor() { }

new FrictionJoint(FrictionJointDef def) #

FrictionJoint(FrictionJointDef def)
    : super(def),
      _localAnchorA = new Vector.copy(def.localAnchorA),
      _localAnchorB = new Vector.copy(def.localAnchorB),
      _linearImpulse = new Vector(),
      _angularImpulse = 0.0,
      _maxForce = def.maxForce,
      _maxTorque = def.maxTorque { }

void getAnchorA(Vector argOut) #

inherited from Joint

Get the anchor point on bodyA in world coordinates.

void getAnchorA(Vector argOut) { }

void getAnchorB(Vector argOut) #

inherited from Joint

Get the anchor point on bodyB in world coordinates.

void getAnchorB(Vector argOut) { }

Vector getLocalAnchorA(Vector argOut) #

Vector getLocalAnchorA(Vector argOut) {
  bodyA.getWorldPointToOut(_localAnchorA, argOut);
}

Vector getLocalAnchorB(Vector argOut) #

Vector getLocalAnchorB(Vector argOut) {
  bodyB.getWorldPointToOut(_localAnchorB, argOut);
}

void getReactionForce(num inv_dt, Vector argOut) #

Get the reaction force on body2 at the joint anchor in Newtons.

docs inherited from Joint
void getReactionForce(num inv_dt, Vector argOut) {
  argOut.setFrom(_linearImpulse).mulLocal(inv_dt);
}

num getReactionTorque(num inv_dt) #

Get the reaction torque on body2 in N*m.

docs inherited from Joint
num getReactionTorque(num inv_dt) => inv_dt * _angularImpulse;

int hashCode() #

inherited from Object

Get a hash code for this object.

All objects have hash codes. Hash codes are guaranteed to be the same for objects that are equal when compared using the equality operator ==. Other than that there are no guarantees about the hash codes. They will not be consistent between runs and there are no distribution guarantees.

If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.

external int hashCode();

void initVelocityConstraints(TimeStep step) #

void initVelocityConstraints(TimeStep step) {
  // Compute the effective mass matrix.
  Vector r1 = new Vector();
  Vector r2 = new Vector();

  r1.setFrom(_localAnchorA).subLocal(bodyA.localCenter);
  r2.setFrom(_localAnchorB).subLocal(bodyB.localCenter);
  Matrix22.mulMatrixAndVectorToOut(bodyA.originTransform.rotation, r1, r1);
  Matrix22.mulMatrixAndVectorToOut(bodyB.originTransform.rotation, r2, r2);

  // J = [-I -r1_skew I r2_skew]
  // [ 0 -1 0 1]
  // r_skew = [-ry; rx]

  // Matlab
  // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]

  Matrix22 K = new Matrix22();
  K.col1.x = bodyA.invMass + bodyB.invMass +
             bodyA.invInertia * r1.y * r1.y + bodyB.invInertia * r2.y * r2.y;
  K.col1.y = -bodyA.invInertia * r1.x * r1.y - bodyB.invInertia * r2.x * r2.y;
  K.col2.x = K.col1.y;
  K.col2.y = bodyA.invMass + bodyB.invMass +
             bodyA.invInertia * r1.x * r1.x + bodyB.invInertia * r2.x * r2.x;

  Matrix22 linearMass = new Matrix22();
  linearMass.setFrom(K);
  linearMass.invertLocal();

  num angularMass = bodyA.invInertia + bodyB.invInertia;
  if (angularMass > 0.0) {
    angularMass = 1.0 / angularMass;
  }

  if (step.warmStarting) {
    // Scale impulses.
    _linearImpulse.mulLocal(step.dtRatio);
    _angularImpulse *= step.dtRatio;

    Vector P = new Vector();
    P.setFrom(_linearImpulse);

    bodyA.linearVelocity.x -= bodyA.invMass * P.x;
    bodyA.linearVelocity.y -= bodyA.invMass * P.y;
    bodyA.angularVelocity -= bodyA.invInertia * (Vector.crossVectors(r1, P) + _angularImpulse);

    bodyB.linearVelocity.x += bodyB.invMass * P.x;
    bodyB.linearVelocity.y += bodyB.invMass * P.y;
    bodyB.angularVelocity += bodyB.invInertia * (Vector.crossVectors(r2, P) + _angularImpulse);
  } else {
    _linearImpulse.setZero();
    _angularImpulse = 0.0;
  }
}

new Joint(JointDef def) #

inherited from Joint
Joint(JointDef def) :
  type = def.type,
  _prev = null,
  _next = null,
  bodyA = def.bodyA,
  bodyB = def.bodyB,
  collideConnected = def.collideConnected,
  islandFlag = false,
  userData = def.userData,

  localCenterA = new Vector(),
  localCenterB = new Vector(),
  edgeA = new JointEdge(),
  edgeB = new JointEdge() { }

factory Joint.create(World argWorld, JointDef def) #

inherited from Joint
factory Joint.create(World argWorld, JointDef def) {
  switch(def.type){
    case JointType.MOUSE:
      throw new NotImplementedException();
      //  return new MouseJoint(def);
    case JointType.DISTANCE:
      return new DistanceJoint(def);
    case JointType.PRISMATIC:
      throw new NotImplementedException();
      //  return new PrismaticJoint(def);
    case JointType.REVOLUTE:
      return new RevoluteJoint(def);
    case JointType.WELD:
      throw new NotImplementedException();
      //return new WeldJoint(def);
    case JointType.FRICTION:
      return new FrictionJoint(def);
    case JointType.LINE:
      throw new NotImplementedException();
      //return new LineJoint(def);
    case JointType.GEAR:
      throw new NotImplementedException();
      //return new GearJoint(def);
    case JointType.PULLEY:
      throw new NotImplementedException();
      //return new PulleyJoint(def);
    case JointType.CONSTANT_VOLUME:
      return new ConstantVolumeJoint(argWorld, def);
  }
  return null;
}

noSuchMethod(String name, List args) #

inherited from Object

noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod. If noSuchMethod returns a value, that value becomes the result of the original invocation.

The default behavior of noSuchMethod is to throw a noSuchMethodError.

external Dynamic noSuchMethod(String name, List args);

const Object() #

inherited from Object

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

const Object();

bool solvePositionConstraints(num baumgarte) #

This returns true if the position errors are within tolerance.

docs inherited from Joint
bool solvePositionConstraints(num baumgarte) => true;

void solveVelocityConstraints(TimeStep step) #

void solveVelocityConstraints(TimeStep step) {
  // Solve angular friction
  {
    final num Cdot = bodyB.angularVelocity - bodyA.angularVelocity;
    num angularMass = bodyA.invInertia + bodyB.invInertia;
    if (angularMass > 0.0) {
      angularMass = 1.0 / angularMass;
    }
    num impulse = -angularMass * Cdot;

    final num oldImpulse = _angularImpulse;
    final num maxImpulse = step.dt * _maxTorque;
    _angularImpulse = MathBox.clamp(_angularImpulse + impulse, -maxImpulse, maxImpulse);
    impulse = _angularImpulse - oldImpulse;

    bodyA.angularVelocity -= bodyA.invInertia * impulse;
    bodyB.angularVelocity += bodyB.invInertia * impulse;
  }

  // Solve linear friction
  {
    Vector r1 = new Vector();
    Vector r2 = new Vector();

    r1.setFrom(_localAnchorA).subLocal(bodyA.localCenter);
    r2.setFrom(_localAnchorB).subLocal(bodyB.localCenter);
    Matrix22.mulMatrixAndVectorToOut(bodyA.originTransform.rotation, r1, r1);
    Matrix22.mulMatrixAndVectorToOut(bodyB.originTransform.rotation, r2, r2);

    Vector temp = new Vector();
    Vector Cdot = new Vector();

    Vector.crossNumAndVectorToOut(bodyA.angularVelocity, r1, temp);
    Vector.crossNumAndVectorToOut(bodyB.angularVelocity, r2, Cdot);

    Cdot.addLocal(bodyB.linearVelocity).subLocal(bodyA.linearVelocity).subLocal(temp);

    Matrix22 K = new Matrix22();
    K.col1.x = bodyA.invMass + bodyB.invMass +
               bodyA.invInertia * r1.y * r1.y + bodyB.invInertia * r2.y * r2.y;
    K.col1.y = -bodyA.invInertia * r1.x * r1.y - bodyB.invInertia * r2.x * r2.y;
    K.col2.x = K.col1.y;
    K.col2.y = bodyA.invMass + bodyB.invMass +
               bodyA.invInertia * r1.x * r1.x + bodyB.invInertia * r2.x * r2.x;

    Matrix22 linearMass = new Matrix22();
    linearMass.setFrom(K);
    linearMass.invertLocal();

    Vector impulse = new Vector();
    Matrix22.mulMatrixAndVectorToOut(linearMass, Cdot, impulse);
    impulse.negateLocal();

    Vector oldImpulse = new Vector();
    oldImpulse.setFrom(_linearImpulse);
    _linearImpulse.addLocal(impulse);

    num maxImpulse = step.dt * _maxForce;
    if (_linearImpulse.lengthSquared > maxImpulse * maxImpulse) {
      _linearImpulse.normalize();
      _linearImpulse.mulLocal(maxImpulse);
    }

    impulse.setFrom(_linearImpulse).subLocal(oldImpulse);

    bodyA.linearVelocity.x -= impulse.x * bodyA.invMass;
    bodyA.linearVelocity.y -= impulse.y * bodyA.invMass;
    bodyA.angularVelocity -= bodyA.invInertia * Vector.crossVectors(r1, impulse);

    bodyB.linearVelocity.x += impulse.x * bodyB.invMass;
    bodyB.linearVelocity.y += impulse.y * bodyB.invMass;
    bodyB.angularVelocity += bodyB.invInertia * Vector.crossVectors(r2, impulse);
  }
}

String toString() #

inherited from Object

Returns a string representation of this object.

external String toString();