Find the direction a joystick is pointing

Hi all!

For a project I want to use a joystick and know which direction it is pointing (ie. South-East). I would like to know if this is possible and general advice on how to implement it. I think it should be doable if there is a compass involved. By knowing the direction of the compass it should be possible to figure out where the joystick is pointing relative to this?

Hope I made myself understandable :P

Is the joystick going to be fixed in place or mobile?
The first case is easy, but for the second, yes, you’ll need a magnetic compass.

One thing I can´t wrap my head around is how I will be able to tell the direction my joystick is pointing relative to the compass.

Pick a simple example and work it through.

Your compass measures 90 degrees (East)

You joystick is in the UP position.

There was a recent thread about modulo 360 arithmetic and compasses.

Thank you for the tip. Reading threads on modulo right now.