Skip to content

Commit

Permalink
Small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
flemming-n-larsen committed Apr 28, 2024
1 parent 2a01631 commit 32f9f10
Showing 1 changed file with 7 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ fun clamp(value: Double, min: Double, max: Double): Double {
fun calcNewBotSpeed(currentSpeed: Double, targetSpeed: Double): Double {
val delta = targetSpeed - currentSpeed
return if (currentSpeed == 0.0) {
if (targetSpeed >= 0) {
val step = delta.coerceAtMost(ACCELERATION)
val step = abs(delta).coerceAtMost(ACCELERATION)
if (delta >= 0) {
(currentSpeed + step).coerceAtMost(MAX_FORWARD_SPEED)
} else {
val step = (-delta).coerceAtMost(ACCELERATION)
(currentSpeed - step).coerceAtLeast(MAX_BACKWARD_SPEED)
}
} else if (currentSpeed > 0) {
Expand All @@ -38,13 +37,13 @@ fun calcNewBotSpeed(currentSpeed: Double, targetSpeed: Double): Double {
val step = delta.coerceAtLeast(DECELERATION)
(currentSpeed + step).coerceAtLeast(MAX_BACKWARD_SPEED)
}
} else {
if (delta < 0) {
val step = (-delta).coerceAtMost(ACCELERATION)
(currentSpeed - step).coerceAtLeast(-MAX_FORWARD_SPEED)
} else {
} else { // currentSpeed < 0
if (delta >= 0) {
val step = (-delta).coerceAtLeast(DECELERATION)
(currentSpeed - step).coerceAtMost(-MAX_BACKWARD_SPEED)
} else {
val step = (-delta).coerceAtMost(ACCELERATION)
(currentSpeed - step).coerceAtLeast(-MAX_FORWARD_SPEED)
}
}
}
Expand Down

0 comments on commit 32f9f10

Please sign in to comment.