Skip to content

Commit

Permalink
Prevent IndexOutOfBoundsException when getting slider path position
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 27, 2024
1 parent 92f155f commit c746ddb
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/ru/nsu/ccfit/zuev/osu/game/GameplaySlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,8 @@ public void init(final GameObjectListener listener, final Scene scene,
}

private PointF getPositionAt(final float percentage, final boolean updateBallAngle, final boolean updateEndArrowRotation) {
tmpPoint.set(position);

if (path.pointCount < 2) {
tmpPoint.set(position);
return tmpPoint;
}

Expand Down Expand Up @@ -415,6 +414,25 @@ private PointF getPositionAt(final float percentage, final boolean updateBallAng
}

int index = left - 1;

// Theoretically, this case should never be reached as it means the percentage would be less than 0
// (which is already covered by the case above). However, people seem to be having IndexOutOfBoundsException
// crashes here, so we'll just return the first point in the path.
if (index < 0) {
var nextPoint = getAbsolutePathPosition(1);

if (updateBallAngle) {
ballAngle = MathUtils.radToDeg(Utils.direction(position.x, position.y, nextPoint.x, nextPoint.y));
}

if (updateEndArrowRotation) {
endArrow.setRotation(MathUtils.radToDeg(Utils.direction(nextPoint.x, nextPoint.y, position.x, position.y)));
}

tmpPoint.set(position);
return tmpPoint;
}

float lengthProgress = (currentLength - path.getLength(index)) / (path.getLength(index + 1) - path.getLength(index));

PointF currentPoint = getAbsolutePathPosition(index);
Expand Down

0 comments on commit c746ddb

Please sign in to comment.