I'm trying to calculate the angle at which a player is lookting relative to a point. I Have the absolute angle (based on the players cords and the cords of the target) and also the players roation. both have been normalized for -180 to 180 degrees. How can i calculate this? Think of it like this: if there is a pole in front of you and you are looking straight at it, the angle should be 0. if you look to the left, it should be increasingly negative, and if you look ton the right it should be increasingly positive.
#math help w/ angles [solved]
37 messages · Page 1 of 1 (latest)
just subtract the point angle from the player angle?
you would think. minecrafts angle system is so messed up. ive tried so many things.
just a sec im testing chat gpts idea
how so
why is it messed up
player angles are normalised to -180 180 anyways btw
Well for one 0 degress is at south, wheras pretty much every corrdinate system considers east to be 0. Plus it goes clockwise as oppsoed to how it normally goes counterclockwiuse. finally when you use player.getYaw() it seems to give you the angle without modulus
like if you spin to the right it inifitly counts upwards and if you spin left it infinitly counts down
okay
but then why wouldn't this work
-# there's also a reason for the last thing
idk maybe i should normalize it to 360 instead of -180 to 180?
why?
cause i think it gets messed up because sometimes its negative and sometimes postive. like werid stuff with 180 -- 180 being 360 idk
maybe i need to do something with absolute value
if the point yaw is 7, and player yaw is 7, subtracting point jaw from player jaw will get you 7 - 7 = 0
if point yaw is -28, and player yaw is -20, subtracting gets you -28 - (-20) = -8
if point yaw is 5, and player yaw is -5, subtracting gets you 5 - (-5) = 10
i dont see the issue
yeah i guess i messed up somwhere else? thats right but but it shows how far i am from the exact oppisite angle
like if i look at the point and spin around 180 it says 0. but also it messed up at other angles. jsut a sec ill make video
that'd help
mb im having to wait for it to compress
private void renderCompass(DrawContext context, RenderTickCounter tickDelta) {
int width = NunYaClient.MC.getWindow().getWidth();
int height = NunYaClient.MC.getWindow().getHeight();
// int rotation = (int) (((NunYaClient.MC.player.getYaw() + 180) % 360) - 180);
int rotation = (int) (NunYaClient.MC.player.getYaw() - 360 * Math.floor((NunYaClient.MC.player.getYaw() + 180) / 360));
context.drawCenteredTextWithShadow(NunYaClient.MC.textRenderer, rotation + "", width / 4, 3, 0xFFFFFFFF);
for (Waypoint waypoint : manager.getDimensionWaypoints()) {
double absoluteAngle = -Math.toDegrees(Math.atan2(NunYaClient.MC.player.getX() - waypoint.x, NunYaClient.MC.player.getZ() - waypoint.z));
double relativeAngle = rotation - absoluteAngle;
//WaypointUtils.drawIcon(waypoint.icon, waypoint.name, context, width / 4 + (int) relativeAngle, 10);
context.drawCenteredTextWithShadow(NunYaClient.MC.textRenderer, absoluteAngle + "", width / 4, 20, 0xFFFFFFFF);
context.drawCenteredTextWithShadow(NunYaClient.MC.textRenderer, relativeAngle + "", width / 4, 40, 0xFFFFFFFF);
}
}
sorry its a bit messy as ive been trying so many things
well i asked chatgpt how to subtract angles when they are from -180 to 180 and it gave me this
double relativeAngle = ((targetAngle - playerRot + 180) % 360 + 360) % 360 - 180;
removing the first 180 made it work perfectly. i dont like just copy pasting from chatgpt but at this point im just ready for it to work lol. youve been asnwering a lot of my questions so thank you for that!
its double relativeAngle = targetAngle - playerRot