I am making a game for Android using Java and libGdx. I have an ArrayList
of enemies that are updated each frame. The update method for the enemy looks like this:
public void update(float delta){
if (waypointIndex < waypointCount){
waypoint = path.getPoints().get(waypointIndex);
distanceToWaypointX = waypoint.getX() - getCenterX();
distanceToWaypointY = waypoint.getY() - getCenterY();
directionToWaypoint = (float) Math.atan2(distanceToWaypointY, distanceToWaypointX);
setRotation((float) Math.toDegrees(directionToWaypoint) - 180);
translationX = (float) (Math.cos(directionToWaypoint) * getMovementSpeed() * delta);
translationY = (float) (Math.sin(directionToWaypoint) * getMovementSpeed() * delta);
distanceToWaypoint = (float) Math.hypot(distanceToWaypointX, distanceToWaypointY);
if (distanceToWaypoint <= 5){
waypointIndex++;
}
distanceTraveled += Math.sqrt(Math.pow(translationX, 2) + Math.pow(translationY, 2));
translate(translationX, translationY);
}
}
This works OK, but with 100 enemies the FPS starts to dip into the low 50's. I would like to keep a consistent 60 FPS and I plan to have more than 100 enemies on the screen at a time. How can i improve this code to make it more efficient?
ArrayList
of 2d points (so just an x and y value) and the enemy will move towards each waypoint one at a time. Once it reaches its target, it will move to the next. So in other words it is just following a path made up of points. Each point is about 10px apart from the next. \$\endgroup\$