Pong - problems with boundary check

I always advice to check the boundaries separately because the *-1
can lead to stuttering (and lead to the problem with the double call to checkBoundaries() imho)

abs() gives always the positive value (e.g. 2.8 without the minus - sign); with *-1 this gives always a negative value. So no stuttering can occur.


  public void checkBoundaries() {
    if (xPos > width - rad) {
      xSpeed = abs(xSpeed) * -1;
    }

    if ( xPos < rad ) {
      xSpeed = abs(xSpeed);
    }

    if (yPos > height - rad) {
      ySpeed = abs(ySpeed) * -1;
      println("yDirection: " + yDirection + "yPos: " + yPos);
    }
    if ( yPos < rad ) {
      ySpeed = abs(ySpeed);
    }
  }
1 Like