Profile image

[FINAL TEASER] Realeasing this Saturday + The "Eureka!" code

48.8k CaptainNoble  7 days ago




The "Eureka!" code

This one's a long story so feel free to skip to the comments, as for those who stayed:

So… a few days ago, while I was locked away in the gulag (iykyk) I came up with what is probably the most interesting FT code I’ve come up with in all my time playing SimplePlanes so far. It’s probably pretty simple for those of you who’ve been here longer than I have but it’s a pretty big deal for me, and I’m so happy I had to share this

About a year ago, I started playing around with PIDs (Proportional Integral Derivative), they basically take a variable and perform an action to bring that variable to a preset/desired value. Anyways, I eventually figured out how to make a functioning autopilot, but there was one tiny issue, the heading control, which those of you who have downloaded some of my recent planes might’ve noticed. SimplePlanes does not calculate heading from 0-360 in the typical sense but rather from 0-180 then from -180 back to 0. At first I thought it should be an easy fix, which it was for only half the issue, all I had to do was convert the in game “Heading” (Big H) to my own “heading” variable (Small h)

I’ve used this function variable on my older builds except I didn’t yet put it into its own single variable

Then there was a new problem which I hadn’t thought about, if the range between the target heading and current heading was any larger than 180 degrees, the plane would very annoyingly take the longest route (Think of the heading as a circular train track with a barrier at the 360/000 degree point, the train cannot cross that point and so must always go clockwise from it or anticlockwise from the other side) for instance, imagine I wanted to go from 002 to 359, easy right?, just a left turn of 3 degrees but no, the plane goes all the way, 357 degrees.

While this had been an issue I’ve ignored for so long, I decided I was finally going to fix it so I started by drawing a small diagram which I should have done from the start as it made it significantly easier to picture:

so, from the image, assuming I wanted to go from 2 to 359, the PID would start in a clockwise motion but I want it to go anti-clockwise for all heading differences greater than 180 degrees clockwise (Vice versa for the opposite scenario where we’d want to turn right), how? By simply re-positioning the headings whenever the conditions arouse. So, to make the PID think it should turn left 3 degrees instead of right 357, I had to kind of lie to it by quite literally shifting the headings by 180 degrees, so by adding 180 degrees to my current (182) and subtracting 180 degrees from the target (179), the plane will finally turn 3 degrees to the left as it does not realize it’s near that 000 obstacle we talked about earlier, it effectively jumps over it, part of the formula was:

For short left turns
(heading-aphdg)<-180?(aphdg-180):aphdg… for the target
(heading-aphdg)<-180?(heading+180):heading… for the current

For short right turns
(heading-aphdg)>180?(aphdg+180):aphdg… for the target
(heading-aphdg)>180?(heading-180):heading… for the current

Combining the two into the final PID variable was:
hse= -(PID(((heading-aphdg)>180?(aphdg+180):((heading-aphdg)<-180?(aphdg-180):aphdg)), ((heading-aphdg)>180?(heading-180):((heading-aphdg)<-180?(heading+180):heading)), 0.003, 0, 0.13))*hdgapon

I had to work in a rotator then paste because the Variable slot was too small and it was tricky to do all that in there, Jundroo, please make them bigger in SP2 <3


The funny part, I was later told by a friend of mine on SPABC that I could’ve just used the “deltaangle” function insert comically large :skull emoji:.
Eitherway, I had solved an Issue I ignored for long, might be simple to some but I’m lowkey happy I did this instead of using the deltaangle hehe lol.