autoDetectAxis method

void autoDetectAxis()

Computes the variance of the body positions and estimates the best axis to use. Will automatically set property axisIndex.

Implementation

void autoDetectAxis() {
  double sumX = 0;
  double sumX2 = 0;
  double sumY = 0;
  double sumY2 = 0;
  double sumZ = 0;
  double sumZ2 = 0;
  final bodies = axisList;
  final N = bodies.length;
  final invN = 1 / N;

  for (int i = 0; i != N; i++) {
    final b = bodies[i];

    final centerX = b.position.x;
    sumX += centerX;
    sumX2 += centerX * centerX;

    final centerY = b.position.y;
    sumY += centerY;
    sumY2 += centerY * centerY;

    final centerZ = b.position.z;
    sumZ += centerZ;
    sumZ2 += centerZ * centerZ;
  }

  final varianceX = sumX2 - sumX * sumX * invN;
  final varianceY = sumY2 - sumY * sumY * invN;
  final varianceZ = sumZ2 - sumZ * sumZ * invN;

  if (varianceX > varianceY) {
    if (varianceX > varianceZ) {
      axisIndex = AxisIndex.x;
    } else {
      axisIndex = AxisIndex.z;
    }
  } else if (varianceY > varianceZ) {
    axisIndex = AxisIndex.y;
  } else {
    axisIndex = AxisIndex.z;
  }
}