So I’m having trouble making my placer face a planet. I have the angle between the player and the planet, I also have the angle that the player is currently at, now what I want to do with these is make my player face the planet but with an incremental change. (I do this because I want my placer to be able to orbit the planet)
The problem is with the math, I increment the player rotation to match the angle between the player and the planet however because angles work in 0 to 360 my player won’t orbit because player rotation might be 2 however angle to planet is 280 so the game will turn the player around, sorry for the bad explanation.
Does anyone know how to make my player successfully orbit my planet?
Here is my code:
double rotation = Math.toDegrees(Math.atan2(currentPlanet.pos[1]-currentPlayer.pos[1], currentPlanet.pos[0]-currentPlayer.pos[0]));
if(rotation < 0)
{
rotation += 360;
}
if(currentPlayer.rotation < rotation)
{
currentPlayer.rotation += 0.15*delta;
}
if(currentPlayer.rotation > rotation)
{
currentPlayer.rotation -= 0.15*delta;
}
The problem is 350° is also -10°. You want the smaller absolute value.
The solution is very simple. Use modulo operation to translate your angles into correct range.
Pass your angle difference to this function. Sign of the result will tell you in which direction you should turn:
You might want to not turn at all if
abs(diff)is very small.I’m not sure if it will make your player orbit your planet. You will need to set correct angular and linear speed.