checkForBounce function

Offset checkForBounce(
  1. double nextX,
  2. double nextY,
  3. double maxX,
  4. double maxY,
  5. Particle particle,
)

Implementation

Offset checkForBounce(
    double nextX, double nextY, double maxX, double maxY, Particle particle) {
  if (nextX > maxX) {
    // Exceeding right bound
    particle.updateVelocity =
        Offset(-particle.currentVelocity.dx, particle.currentVelocity.dy);
    nextX = maxX;
  } else if (nextX < 0) {
    // Exceeding left bound
    particle.updateVelocity =
        Offset(-particle.currentVelocity.dx, particle.currentVelocity.dy);
    nextX = 0;
  }
  if (nextY > maxY) {
    // Exceeding bottom bound
    particle.updateVelocity =
        Offset(particle.currentVelocity.dx, -particle.currentVelocity.dy);
    nextY = maxY;
  } else if (nextY < 0) {
    // Exceeding top bound
    particle.updateVelocity =
        Offset(particle.currentVelocity.dx, -particle.currentVelocity.dy);
    nextY = 0;
  }
  return Offset(nextX, nextY);
}