createFrictionFromAverage method

void createFrictionFromAverage(
  1. int numContacts
)

Take the average N latest contact point on the plane.

Implementation

void createFrictionFromAverage(int numContacts) {
  // The last contactEquation
  ContactEquation c = result[result.length - 1];

  // Create the result: two "average" friction equations
  if (!createFrictionEquationsFromContact(c,frictionResult) || numContacts == 1) {
    return;
  }

  final f1 = frictionResult[frictionResult.length - 2];
  final f2 = frictionResult[frictionResult.length - 1];

  _averageNormal.setZero();
  _averageContactPointA.setZero();
  _averageContactPointB.setZero();

  final bodyA = c.bi;
  //final bodyB = c.bj;
  for (int i = 0; i != numContacts; i++) {
    c = result[result.length - 1 - i];
    if (c.bi != bodyA) {
      _averageNormal.vadd(c.ni, _averageNormal);
      _averageContactPointA.vadd(c.ri, _averageContactPointA);
      _averageContactPointB.vadd(c.rj, _averageContactPointB);
    } else {
      _averageNormal.vsub(c.ni, _averageNormal);
      _averageContactPointA.vadd(c.rj, _averageContactPointA);
      _averageContactPointB.vadd(c.ri, _averageContactPointB);
    }
  }

  final invNumContacts = 1 / numContacts;
  _averageContactPointA.scale(invNumContacts, f1.ri);
  _averageContactPointB.scale(invNumContacts, f1.rj);
  f2.ri.copy(f1.ri); // Should be the same
  f2.rj.copy(f1.rj);
  _averageNormal.normalize();
  _averageNormal.tangents(f1.t, f2.t);
}