getVelocityReflection method

  1. @override
Vector2 getVelocityReflection(
  1. PositionComponent other,
  2. CollisionData data
)
override

Implementation

@override
Vector2 getVelocityReflection(
  PositionComponent other,
  CollisionData data,
) {
  if (_bouncingObjectEnabled) {
    if (velocity.length < _minBounceVelocity) {
      return super.getVelocityReflection(other, data);
    }
    final otherVelocity =
        (other is Movement) ? other.velocity : Vector2.zero();
    final relativeVelocity = otherVelocity - velocity;

    if (relativeVelocity.dot(data.normal) > 0) {
      return super.getVelocityReflection(other, data);
    }

    final bRestitution =
        (other is SimpleElasticCollision) ? other._restitution : _restitution;

    final double e = min(_restitution, bRestitution);

    var j = -(1 + e) * relativeVelocity.dot(data.normal);

    final mass = (this is Forces) ? (this as Forces).mass : 1;
    final massB = (other is Forces) ? other.mass : 1;
    j /= mass + massB;

    final impulse = data.normal * j;

    onBounce(other, data, impulse);

    // Sistema base: velocity -= getVelocityReflection
    // Para reflexão com coeficiente e: v_final = -e * v_normal + v_tangencial
    // Como sistema subtrai nosso retorno, retornamos: v_normal + impulse
    final normalComponent = data.normal * velocity.dot(data.normal);
    return normalComponent + impulse;
  }
  return super.getVelocityReflection(other, data);
}