1

Can anyone explain me this code? i don't understand condition in the loop while and "predicted X". why predicted_X < 18? what does 18 mean?

while((++deltaTime)*BULLET_SPEED < 
        Point2D.Double.distance(myX, myY, predictedX, predictedY)) {
    predictedX += Math.sin(enemyHeading) * e.getVelocity();
    predictedY += Math.cos(enemyHeading) * e.getVelocity();
    enemyHeading += enemyHeadingChange;
    if(predictedX < 18.0 
            || predictedY < 18.0
            || predictedX > getBattleFieldWidth() - 18.0
            || predictedY > getBattleFieldHeight() - 18.0) {
        predictedX = Math.min(Math.max(18.0, predictedX), 
                getBattleFieldWidth() - 18.0);  
        predictedY = Math.min(Math.max(18.0, predictedY), 
                getBattleFieldHeight() - 18.0);
        break;
    }
}
Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
AnNg
  • 75
  • 2
  • 6
  • That's why you (not you, generic you) shouldn't use magic numbers and have meaningfully named constants instead. – Federico klez Culloca Apr 05 '18 at 14:49
  • I'm not sure about this but 18.0 looks like a float OR maybe it's a decimal??? – L_Church Apr 05 '18 at 14:49
  • This code is simply ugly and unreadable. From my understanding, it looks like 18 is an internal margin of the battle field that the developer chose as a magic number. So he is checking that both `predictedX` and `predictedY` don't go over the min/max margins (if they do, he clamps) – Oneiros Apr 05 '18 at 14:52

1 Answers1

0

The robot´s width is 36px so 18.0 is half of it. The condition just makes sure that your predictedX/Y isn´t outside of the battlefield

Quang
  • 16