createJoint method

  1. @override
int createJoint(
  1. int bodyA,
  2. int bodyB,
  3. JointSettings3D settings
)
override

Joins bodyA to bodyB, or to the world when bodyB is 0; 0 when it cannot be made. Both must be bodies createBody made, not a character's body.

Implementation

@override
int createJoint(int bodyA, int bodyB, JointSettings3D settings) {
  // Only bodies made by createBody: a joint must not hold a character's
  // stand-in body, which goes with its character.
  final sixDofLists =
      settings.sixDofModes.length >= 6 &&
      settings.sixDofMin.length >= 6 &&
      settings.sixDofMax.length >= 6;
  if (_gone ||
      !_bodyShapes.containsKey(bodyA) ||
      (bodyB != 0 && !_bodyShapes.containsKey(bodyB)) ||
      (settings.kind == JointKind3D.sixDof && !sixDofLists)) {
    return 0;
  }
  final s = _jointSettings.ref;
  void put(Array<Float> to, Vector3 v) {
    to[0] = v.x;
    to[1] = v.y;
    to[2] = v.z;
  }

  s.kind = settings.kind.index;
  put(s.pointA, settings.pointA);
  put(s.pointB, settings.pointB);
  put(s.axis, settings.axis);
  put(s.normal, settings.normal);
  s
    ..hasLimits = settings.hasLimits
    ..limitMin = settings.limitMin
    ..limitMax = settings.limitMax
    ..springFrequency = settings.springFrequency
    ..springDamping = settings.springDamping
    ..maxFriction = settings.maxFriction
    ..motorState = settings.motorState.index
    ..motorTarget = settings.motorTarget
    ..motorMaxForce = settings.motorMaxForce;
  var free = 0, fixed = 0;
  for (var a = 0; a < (sixDofLists ? 6 : 0); a++) {
    switch (settings.sixDofModes[a]) {
      case SixDofAxisMode3D.free:
        free |= 1 << a;
      case SixDofAxisMode3D.fixed:
        fixed |= 1 << a;
      case SixDofAxisMode3D.limited:
        break;
    }
    s.sixDofMin[a] = settings.sixDofMin[a];
    s.sixDofMax[a] = settings.sixDofMax[a];
  }
  s
    ..sixDofFree = free
    ..sixDofFixed = fixed;
  final joint = jj_joint_create(handle, bodyA, bodyB, _jointSettings);
  if (joint != 0) {
    _jointBodies[joint] = (bodyA, bodyB);
    (_bodyJoints[bodyA] ??= {}).add(joint);
    if (bodyB != 0) (_bodyJoints[bodyB] ??= {}).add(joint);
  }
  return joint;
}