✅ Console FPS Player Direction

I am making a math error in the following code: https://gist.github.com/ZacharyPatten/e071bfefd00f82c8ad719e53fb561d5a
NOTE: this is a fork/reimplementation of: https://github.com/OneLoneCoder/CommandLineFPS/blob/master/CommandLineFPS.cpp
The code is mostly working, however, something is off and I haven't quite spotted it yet. When you hold the 'A' key, you are supposed to be turning left, however, the player icon in the minimap (< ^ > v) are rotating clockwise instead of counterclockwise (inversed). The same is true for holding the 'D' key as the icons rotate clockwise instead of counterclockwise. However the direction in the minimap appears to be accurate because the 'W' key always moves you in the direction you are facing. I'm wondering if this was a bug in the original code or something I added when I translated it from C++ to C#. 🤔 The original code just had a "P" in the minimap (not < ^ > v) for the player; it did not include player direction in the minimap.
34 Replies
ZacharyPatten
ZacharyPattenOP2y ago
@AdmSnyder 😛
AdmSnyder
AdmSnyder2y ago
first, this should be a constant: (float)Math.PI * 2 😛 also, you shouldn't need to mod 2PI there because you already had those while loops.
ZacharyPatten
ZacharyPattenOP2y ago
whoops I did the mod first and forgot to remove after adding the loops
AdmSnyder
AdmSnyder2y ago
but i don't see a math error there
ZacharyPatten
ZacharyPattenOP2y ago
code updated
AdmSnyder
AdmSnyder2y ago
and i suppose you can rule out overshooting because auf renderspeed
ZacharyPatten
ZacharyPattenOP2y ago
just updated the gist I'm mainly concerned with whatever is going on with the clockwise/counterclockwise inversion on the minimap. I'm not exactly sure what is happening yet
AdmSnyder
AdmSnyder2y ago
we are talking about line 225ff, right?
ZacharyPatten
ZacharyPattenOP2y ago
yeah... but I don't think it is with that section of the code something is inverted somewhere
AdmSnyder
AdmSnyder2y ago
no, that one looks good
ZacharyPatten
ZacharyPattenOP2y ago
im just not sure where the inversion is
AdmSnyder
AdmSnyder2y ago
try putting an x into the coordinates projected from the angle maybe so we see where it's actually heading the inversion is that y coords grow downwards? whereas cos is going upwards
Omnissiah
Omnissiah2y ago
what's you system of reference, is α=0 pointing down?
ZacharyPatten
ZacharyPattenOP2y ago
yes
Omnissiah
Omnissiah2y ago
and growing goes counter-clockwise
ZacharyPatten
ZacharyPattenOP2y ago
well that is what has me stumped. as "a" grows the minimap looks like you are turning left but the world view looks like you are turning right I can't tell if I jsut inverted the world view or not something seems like it is inverted
Omnissiah
Omnissiah2y ago
can i extemporaneously say that having if (OperatingSystem.IsWindows()) inside Update() is kinda weird it's not like the os changes while the game is playing if i read well
ZacharyPatten
ZacharyPattenOP2y ago
I don't think it is very weird personally. I only want to call user32.dll if we are on windows, other wise it will need to use different code to resolve it the code will break on other OS's if it tries to call user32.dll without the if check
Omnissiah
Omnissiah2y ago
yeah but you don't need to check that every cycle 😐 like, once the game is started the condition should be settled
ZacharyPatten
ZacharyPattenOP2y ago
I believe it is optimized by JIT so it won't check every cycle For example: https://github.com/dotnet/runtime/blob/bf7fb2ecbf69deb6a73bb8bbca1e56e7beec8d8a/src/libraries/Common/src/System/IO/PathInternal.CaseSensitivity.cs#L27 there is jus tone use case in the BCL it is used a lot if the BCL
Omnissiah
Omnissiah2y ago
sure, and i use it too, i just find it weird that it's not assigned to a readonly variable or something, even if it returns a constant anyway this
float eyeX = (float)Math.Sin(rayAngle);
float eyeY = (float)Math.Cos(rayAngle);
float eyeX = (float)Math.Sin(rayAngle);
float eyeY = (float)Math.Cos(rayAngle);
what is this doing when rayAngle is 0 and grows sin goes to 1 and cos goes to 0 this seems important for the matter because it seems to be clockwise oriented? but i really have to go sleep now
ZacharyPatten
ZacharyPattenOP2y ago
no worries thanks for looking my code is a fork of this code (but I translated it to C# rather than C++ and changed some stuff) https://www.youtube.com/watch?v=xW8skO7MFYw https://github.com/OneLoneCoder/CommandLineFPS/blob/master/CommandLineFPS.cpp but the eyeX and eyeY was from the original code. I'm still wrapping my head around the math a bit
Omnissiah
Omnissiah2y ago
you can draw x=cos(α), y=cos(α) and see how it behaves it takes just what 4 lines of code
ZacharyPatten
ZacharyPattenOP2y ago
yeah I have already tried several inversions but I havent' found the magic combination yet swapping the
x=cos(α), y=cos(α)
does seem like it fixes one thing but then the world view and movement angles are wrong so probably more than 4 linies of code that need adjusted
HimmDawg
HimmDawg2y ago
What if you just swap the conditions on the switch at l.340 for displaying > and < ?
ZacharyPatten
ZacharyPattenOP2y ago
unfortunately not that simple
Henkypenky
Henkypenky2y ago
it's not a bug, the math checks out, the problem is the representation of the minimap imo, you might need to invert it also, if i understood correctly you are basing the minimap on the map, maybe keeping separate maps is better, and updating the minimap as needed based on the position and the calculated a, and the minimap representation will be based on where you are facing, so it will be different in relation to the actual map very cool project btw
Omnissiah
Omnissiah2y ago
why doing random changes 😐 try to understand the code
HimmDawg
HimmDawg2y ago
Why not? It seemed like that fixes things fluffyFoxThink
ZacharyPatten
ZacharyPattenOP2y ago
when you press 'W' or 'S' you are currently moving in the minimap the correct direction. if I flip around the <, v, > and ^ characters rendered in the minimap, then when you press 'W' you no longer move in the direction the icon is facing it is likely possible that those characters need to be inverted, but they are not the only thing that needs to be inverted
HimmDawg
HimmDawg2y ago
I'd assume you only have to switch around > and <
ZacharyPatten
ZacharyPattenOP2y ago
you are welcome to try it, but as mentioned that only fixes the minimap rotation. then there are more issues where the direction you move doesn't line up with the direction you are facign in the minimap. There is something else that is inverted
HimmDawg
HimmDawg2y ago
Ah yeah, I only focussed on the minimap rotation first. I haven't found anything else so far
ZacharyPatten
ZacharyPattenOP2y ago
thanks for looking 🙂 and yeah it is proving a bit challenging to figure out exactly what is happening. I was able to fix it... I had to invert most of the X-axis indexing of the string[] map. still wrapping my head around it but it is working haven't updated the code at the link yet
Want results from more Discord servers?
Add your server