I’m making a space game, and I’d like to make a small drone ship orbit my bigger player ship. I’m not entirely sure how to make it orbit in a perfect circle. So far I can make it move in a diamond shape, but my attempts to correct for the circle shape have ended in failure.
Basically, I’m doing something like this:
float centerX = ship.getX() + (ship.getWidth() / 2);
float centerY = ship.getY() + (ship.getHeight() / 2);
float droneX = drone.getX();
float droneY = drone.getY();
float radius = drone.getRadius();
float xDiff = Math.abs(droneX - centerX);
float yDiff = Math.abs(droneY - centerY);
float moveByX = Math.abs(radius / (xDiff == 0 ? 1 : xDiff) / smoother);
float moveByY = Math.abs(radius / (yDiff == 0 ? 1 : yDiff) / smoother);
And then I move the drone by the moveByX and moveByY values. It works fine in a diamond shape, as I mentioned, but how can I improve this to calculate the correct circular pattern?
Okay, since you’re using x and y differences, it will only go in a straight line, which explains the diamond pattern. In order to get the circle, you’ll have to break out trigonometry.
After that you can use your movement code. And angle should probably be kept on the drone, and in radians.