#math help w/ angles [solved]

37 messages · Page 1 of 1 (latest)

keen wasp
#

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.

glass nova
#

just subtract the point angle from the player angle?

keen wasp
#

you would think. minecrafts angle system is so messed up. ive tried so many things.

#

just a sec im testing chat gpts idea

glass nova
#

why is it messed up

#

player angles are normalised to -180 180 anyways btw

keen wasp
#

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

glass nova
#

okay

glass nova
glass nova
keen wasp
glass nova
#

why?

keen wasp
#

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

glass nova
#

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

keen wasp
#

let me try it again

#

maybe i messed up somewhere else

keen wasp
glass nova
#

what

#

wdym

#

een opposite angle

keen wasp
#

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

glass nova
#

that'd help

keen wasp
#

mb im having to wait for it to compress

glass nova
#

okay yeah that's weird

#

what are you doing rn?

keen wasp
#
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

keen wasp
# glass nova okay yeah that's weird

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!

glass nova
#

its double relativeAngle = targetAngle - playerRot

keen wasp
#

idk man. this is the only thing ive tried where it doesnt do the weird angle skip or be pointing in the wrong direction

#

math help w/ angles [solved ish]

#

math help w/ angles [solved]