Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Studio Chat / [SOLVED] Problems heading north

Author
Message
wuergezwerg
5
Years of Service
User Offline
Joined: 30th Jul 2018
Location: Germany
Posted: 20th Feb 2020 20:12
Hello,

I am trying to code a compass and have a problem when the needle hits the 360°/0° border.

I have already found the only other AppGameKit compass (https://forum.thegamecreators.com/thread/212325) but I wanted to have the tilt compensation After searching for hours I found some formulas (https://ozzmaker.com/compass1/) but now I am stuck. When hitting 360° the compass turns around until it reaches 0° and then jumps around when pointing north.

Any ideas how to get it to work ?

Thanks in advance!



Compass code:

The author of this post has marked a post as an answer.

Go to answer

Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 21st Feb 2020 02:53
function CalcHeading(smoothness as float)
offset = 90
heading = ATanFull(Data.Result.Y,Data.Result.X)+offset
if(heading > 360)
heading = heading-360
endif

heading = SmoothValues(heading,smoothness)
endfunction heading

...
This section is the problem.
Remember that the Value Range is 0 to 359 Degrees... as 0 Degrees and 360 Degrees are the SAME Value.

Dark BASIC Professional used to actually have a Helper Function Specifically for Angles (called WrapValue) but basically it's just this function:



That should do the trick. ^_^

wuergezwerg
5
Years of Service
User Offline
Joined: 30th Jul 2018
Location: Germany
Posted: 21st Feb 2020 19:30
Thank you for your answer.

The result of the WrapAngle function is the same as my original code. As ATanFull returns an angle between 0 and 360 I only need to subtract if my offset pushes the value above 360 afterwards.

The problem comes from my attempt to smoothen the movement of the needle. As the magnetic sensor is a little "nervous" I need to average the value in some way.
Both versions set the angle correctly to 0° when reaching 360°, but to reach that angle the sprite turns 360° counter clockwise. As calculated values are somewhere between +5/-5 degrees the sprite turns full circles in both directions while pointing north.

Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Feb 2020 22:42
Why bother resetting the angle at all?
It would be a lot easier to just let it rotate freely. So when it goes beyond 360, you let it happen. The sprite will still point in the same direction. 360 + 90 = 450 but rotating a sprite by 450 degrees will still point it in the same direction as rotating it 90 degrees.
When you want to read the value, use mod()
angle = mod(compassAngle, 360)
So in the example above: angle = mod(450, 360). angle = 90

Raven
19
Years of Service
User Offline
Joined: 23rd Mar 2005
Location: Hertfordshire, England
Posted: 21st Feb 2020 23:37


I mean I'm shattered, as just got off work... and my brain might be a little asleep but that should do the trick, right?

wuergezwerg
5
Years of Service
User Offline
Joined: 30th Jul 2018
Location: Germany
Posted: 23rd Feb 2020 11:51
This post has been marked by the post author as the answer.
Thank you both! With your ideas I found a working solution.

Fixed Function:


If you want to test the results, here is the complete working code. Just add the arrow.png (something pointing upwards) and you can toggle the different corrections on and off.


Login to post a reply

Server time is: 2024-04-19 09:52:15
Your offset time is: 2024-04-19 09:52:15