So I'm modding a 3D game, my goal is to place a label on the screen pointing to the unit's direction. I'm getting the horizontal angle by projecting these vectors in a plane, like this:
private double getXAxisAngle(EntityPlayer player,EntityPlayer player2){
double meYaw = player2.rotationYaw;
double meX = player2.posX;
double meZ = player2.posZ;
double playerX = player.posX -meX;
double playerZ = player.posZ-meZ;
double yawX = Math.cos(Math.toRadians(meYaw));
double yawY = Math.sin(Math.toRadians(meYaw));
double dist = getDist(playerX,playerZ);
playerX = playerX / dist;
playerZ = playerZ / dist;
dist = getDist(yawX,yawY); //get the vector magnitude
yawX = yawX / dist;
yawY = yawY / dist;
double angle1 = Math.toDegrees(Math.atan2(yawY, yawX));
double angle2 = Math.toDegrees(Math.atan2(playerZ, playerX));
double angle = angle1-angle2;
angle+=90; //So when it's facing the other unit it becomes 0
if(angle > 180)
angle-=360;
else if(angle < -180)
angle += 360;
return angle;
}
This function gives me an angle between 0/-180 and 0/+180, depending on which side I'm facing then I get this angle and transform into a coord into the screen like this:
private double angleXToPosition(double angle,FontRenderer fontrenderer,String label)
{
ScaledResolution scaledresolution =
new ScaledResolution(mc.gameSettings, mc.displayWidth, mc.displayHeight);
double width = scaledresolution.getScaledWidth();
double namePosX =
(int)(angle * ((width / 2 - (fontrenderer.getStringWidth(label) / 2)) / 50)) +
width / 2 - (fontrenderer.getStringWidth(label) / 2);
//If the angle is bigger then 50 the label sticks on the side of the screen
if (angle > 50)
namePosX = width - (fontrenderer.getStringWidth(label));
else if (angle < -50)
namePosX = 0; //Same as above
return namePosX;
}
And it works until this, the label moves perfectly to the sides, accompanying the unit position on the screen, but now I'm trying to make the label move up and down and the same functions doesn't work, and I can't figure this out.
Some info: the pitch of the player when facing the sky is -90 and when facing the ground is 90.
