Wed Feb 23, 2022 11:58 am

A player angle is 0 to 359 degrees.

However the angle does NOT wrap back to zero if you keep turning, instead it keeps incrementing or decrementing as you turn.

If you turn clockwise twice, your angle is reported as 720
if you turn counterclockwise twice, your angle is reported as -720

This means your angle can be reported in the thousands or negative thousands if you make more of one turn than another.

a monster has a field called movedir, set like monster.movedir = 2. Valid values are 0 to 7 (8 in total) representing n, ne, e, se, etc

How do I convert a player angle to the closest movedir angle?

Wed Feb 23, 2022 12:16 pm

Something like:
Code:
`movedir = (angle / 45) % 8;if (movedir < 0) { movedir += 8; }`
Last edited by AFADoomer on Wed Feb 23, 2022 7:01 pm, edited 1 time in total.

Wed Feb 23, 2022 12:38 pm

I ended up doing something like

Code:
`    if(pa <= 22.5 || pa >= 337.5){        friend.movedir = 0; // e      }else if(pa > 22.5 && pa <= 67.5){        friend.movedir = 1; // ne      }else if(pa > 67.5 && pa <= 112.5){        friend.movedir = 2; // n      }else if(pa > 112.5 && pa <= 157.5){        friend.movedir = 3; // nw      }else if(pa > 157.5 && pa <= 202.5){        friend.movedir = 4; // w      }else if(pa > 202.5 && pa <= 247.5){        friend.movedir = 5; // sw      }else if(pa > 247.5 && pa <= 292.5){        friend.movedir = 6; // s      }else if(pa > 292.5 && pa < 337.5){        friend.movedir = 7; // se      }`

Although your method works, it does a weird thing and the movedir always points to the cardinal direction to the right, even if the left direction is closer.

What I mean is... imagine you're standing facing exactly north, but then you look right 1 degree. I expect that the movedir would still point north because im just barely offcenter, but instead it will snap to ne. My method seems to fix this, though it's quite ugly.

Wed Feb 23, 2022 1:52 pm

malon3 wrote:Although your method works, it does a weird thing and the movedir always points to the cardinal direction to the right, even if the left direction is closer.

What I mean is... imagine you're standing facing exactly north, but then you look right 1 degree. I expect that the movedir would still point north because im just barely offcenter, but instead it will snap to ne. My method seems to fix this, though it's quite ugly.

It's because of the math. Look at this code:
Code:
`movedir = (angle / 45) % 8;`

dividing that angle by 45 is essentially rounding that angle to the nearest 45. All values in each 45-degree range will be snapped to the bottom of that range. That's the behavior you're seeing. To counter that, shift your value by half of the range and it will then snap to the middle of the range instead of the range "edges". Your range is 45 so half that would be 22.5 so like this:
Code:
`movedir = ((angle+22.5) / 45) % 8;`

I recently made a compass mod where I needed to point it in the correct direction and used similar math. I was using 16 directional steps instead of 8 for smoother graphics, but the concept is the same.

Wed Feb 23, 2022 5:24 pm

Sir Robin wrote:
malon3 wrote:Although your method works, it does a weird thing and the movedir always points to the cardinal direction to the right, even if the left direction is closer.

What I mean is... imagine you're standing facing exactly north, but then you look right 1 degree. I expect that the movedir would still point north because im just barely offcenter, but instead it will snap to ne. My method seems to fix this, though it's quite ugly.

It's because of the math. Look at this code:
Code:
`movedir = (angle / 45) % 8;`

dividing that angle by 45 is essentially rounding that angle to the nearest 45. All values in each 45-degree range will be snapped to the bottom of that range. That's the behavior you're seeing. To counter that, shift your value by half of the range and it will then snap to the middle of the range instead of the range "edges". Your range is 45 so half that would be 22.5 so like this:
Code:
`movedir = ((angle+22.5) / 45) % 8;`

I recently made a compass mod where I needed to point it in the correct direction and used similar math. I was using 16 directional steps instead of 8 for smoother graphics, but the concept is the same.

duuuude you're a genius!
I knew it was something like that, I tried
Code:
`(angle / 22.5) % 8`
earlier and it didnt work obviously, damn thanks so much bro it works perfectly now

Wed Feb 23, 2022 10:42 pm

malon3 wrote:I knew it was something like that, I tried
Code:
`(angle / 22.5) % 8`
earlier and it didnt work obviously, damn thanks so much bro it works perfectly now

You were on the right track. You knew that you wanted to shift that 45 degree range by half a range, so you divide by 22.5 instead of 45 to get 16 half-ranges.
But then you still need to shift the value by one of those half-ranges:
Code:
`(angle / 22.5 + 1) % 8`

But that's in 16 ranges, where in the end you only want 8, so you need to divide it again:
Code:
`((angle / 22.5 + 1) / 2) % 8`

That's essentially the same function as I gave you, but it takes more steps, therefore is slower to do. That's why you should do the shift before you do the divide, so you only have to divide once.

Here's something I do when I'm trying to figure out math: When I have a function that doesn't give me what I want, I write out all the inputs and outputs that I have, then write out what I wish I had, and see what the difference is and see if I can figure out the math to get there.
So for you, you have this function `(angle/45)%8` and
0..44 gives 0
45..89 gives 1
90..134 gives 2
And what you want is
-22..22 gives 0
23..67 gives 1
68..112 gives 2
So what's the difference between what you have and what you want? Input -22 in every case (or -22.5 to be exact) and since the DIFFERENCE is NEGATVIE that's a double negative and becomes a positive. So you add that 22.5 to your input (the angle) and that gives you what you want. So we're back to `((angle + 22.5) / 45) % 8`