Skip to content

DEV BLOG

SAS Improvements

Hello all, I thought I’d write a quick blog post summarizing the main changes that we’ve done to SAS in KSP2 Redux, as I think it’d make for an interesting semi-technical overview.

You might think that we could’ve just ripped KSP1’s SAS system into KSP2, but that approach would not have worked, mainly given that the core of the SAS logic in KSP2 is the same algorithm as it is in KSP1.

So then, why is SAS so stable in KSP1 while it is in KSP2, if the core is the same?

The simple answer is, it has to do with how data is fed into the SAS system, and how its outputs are applied to the controls, there has to be differences in there, maybe the data coming in is stale, or maybe the outputs are being applied differently. And this is what guided our changes and our investigations into the differences in the 3 main passes we looked into the SAS system.

When I was first looking into the SAS differences, I started trying to look at how the outputs were applied, and I made the mistaken assumption that the SAS outputs were somehow going through Unity’s input system, and as such the outputs were being smoothed in a way that they weren’t in KSP2, so I tried replicating that input system to see if it improved this. The replication itself just made all the inputs feel sluggish and not fun to play with, and it did not even fix the issues with SAS in the first place, as such these changes were scrapped.

Though, I’m still mentioning this because one improvement did come out of this pass of looking at it, the stability improvements for command pods/probe cores that are the sole part of a vessel. Essentially, what was going on there was that the moment of inertia calculations for a single part were using unity’s own moment of inertia calculations directly, while every other case would apply some form of scaling to the moment of inertia based off of mass, so what I did here was just mirror the mass scaling for this case.

Main Article: SAS, Flight Input, and Precision Mode

For this pass, we looked deeper into how the algorithm worked, and the data that is being fed into it, and there were 3 main things we noticed and subsequently fixed for Beta 5.

  1. Coasting Logic The coasting logic in KSP2 was worse than KSP1 at predicting overshoots.
  2. SAS Update Order The SAS commands were being sent out to the modules after the modules had updated their state for the frame, meaning they’d be applied later than before.
  3. Telemetry Data The telemetry data being fed to the SAS was in the Update loop, meaning during time warp it would not be fresh, leading to stability issues

(More information on these changes can be found in the main article by Munix)

The prior changes still weren’t enough to bring the SAS on par with KSP1’s performance and stability, so we decided to look further into it to see anything else that could be an issue, again focusing on the inputs to the SAS system, and where the outputs are going. By comparing this to KSP1, we found one crucial difference, which will require a bit of explanation.

KSP2 Introduced interfaces for parts that apply torque and other forces to the main rigidbody that were not present in KSP1, in KSP1 all parts did these on their own immediately in their update cycle.

This change was part of what introduced the instability that this pass fixed, as this torque was no longer being applied immediately.

So when was it being applied? During the RigidbodyBehaviour’s OnFixedUpdate method near the beginning of a physics update. The entire relevant loop, including other modules, looking something like

  1. Begin the physics update
  2. Copy Unity’s Rigidbody state into the data used for the simulation
  3. Write back the current torque being provided by ITorque components and other forces to the Unity Rigidbody
  4. Use the data copied in step 1 to update the SAS commands (SAS module)
  5. Use the SAS commands to update part torque output (Parts with ITorque)
  6. End the physics update

As can be seen in the loop, the actual torque output was not being applied until the next frame and the SAS was getting data fed into it that was read before these outputs were applied, so it had stale data and an output lag.

The fix to this was quite simple, we just had to give the RigidbodyBehaviour 2 slots in the update cycle, rather than just one, keeping the early update for reading the data into the simulation state, then applying the outputs later in the cycle when everything has had a chance to update their torque and other physics outputs.

Meaning that the new correct loop in Beta 5 Hotfix 2 looks something like:

  1. Begin the physics update
  2. Copy Unity’s Rigidbody state into the data used for the simulation
  3. Use the data copied in step 1 to update the SAS commands (SAS module)
  4. Use the SAS commands to update part torque output (Parts with ITorque)
  5. Write back the current torque being provided by ITorque components and other forces to the Unity Rigidbody
  6. End the physics update

v0.2.3 Beta 5 Hotfix 2

We have just released the second hotfix for the 5th beta of KSP2 Redux, which includes a bunch of minor bug fixes as well as further improvements to SAS stability.

You can install the update in the Updater application (If you don’t have it yet, download on the Updater GitHub page). - Simply select the newest beta (highest number) in the dropdown and click Install.

You can find the full changelog on the Redux GitHub page or on Discord: https://discord.com/channels/1078696971088433153/1444156522127954064/1498461514145730750.

v0.2.3 Beta 5 Hotfix 1

We have just released a hotfix for the 5th beta of KSP2 Redux, which includes a fix for issues with Chinese, Japanese and Korean, Color Manager presets disappearing, adds 3 new missions, and a couple more bug fixes.

You can install the update in the Updater application (If you don’t have it yet, download on the Updater GitHub page). - Simply select the newest beta (highest number) in the dropdown and click Install.

You can find the full changelog on the Redux GitHub page or here on Discord: https://discord.com/channels/1078696971088433153/1444156522127954064/1497756315089637488.

v0.2.3 Beta 5

We have just released the 5th beta of KSP2 Redux, which includes an upgrade of the game’s Unity version from 6000.3.10f1 to 6000.4.1f1, an early release of the modding SDK, an experimental KSP1 part mod loader, first few SM+ size (1.875m) parts, support for Discord rich presence, game statistics tracking, custom flags, improved SAS and flight controls, and as always, a ton of bugfixes.

You can download the new Updater application on the Updater GitHub page. This application will now be the primary way of installing, updating and uninstalling KSP2 Redux. New installation instructions are available on the Redux GitHub page.

You will need a stock install of KSP2 0.2.2 for the initial install (meaning you CANNOT upgrade from beta 4), but this won’t be the case for any future updates, those will just be installed on top of beta 5.

You can find the full changelog on the Redux GitHub page.

Along with this new release and the (at this point almost mythical) Updater/Launcher application, we have also been working on the new website: https://ksp2redux.org, as well as on updating the modding SDK and template project for Unity 6+. You can find the instructions to set up the modding environment here: https://docs.rendezvous.dev/s/0a4a3e31-0b40-48e0-bd81-e14856f80d3d/doc/getting-started-with-modding-ea0cMbSQ3V.

Custom flags can be added to the game by creating a new folder inside your Redux install’s mods folder. You can call this anything, e.g. MyMod. Then, inside it, create a flags folder, and put your flag files in (ideally PNG). On the next game launch, you should see the flags in the campaign creation menu.

KSP1 part mod import can be tested by creating a folder called KSP1 inside mods, and then putting KSP1 mod folders inside it (generally, whatever folder would go into GameData in KSP1 will go into the KSP1 folder). Please, keep in mind that this is a very experimental initial version of this feature, and it is meant to serve as more of just a tech demo for the future KSP1 mod porting Unity tools that we are working on, meaning we won’t be providing support for mods that are not working properly or breaking the game.

API Documentation Pipeline Update

A short dev update from what ive been doing for today:

I have been working on our workflow to automatically generate API documentation from the doc comments we write in the code.

This is not as simple as it may seem because unity does not have any way to export the xml documentation like msbuild would normally have, so instead I had to force unity in the CI pipeline to generate the project files as it would with rider, then have a modern dotnet sdk build the files to generate the documentation files, which took a lot of fanangling as you might imagine (i even had to write a script to selectively fix an error for this step that only occurs when using the dotnet sdk).

Then later on in the CI/CD pipeline, I invoke docfx to build all the documentation, and push to a github repo (for ease of hosting it on our own domain).

You can see current documentation here: http://api.ksp2redux.org/

This is for the develop branch so it wont accurately reflect whats in the beta, however once the next beta is out it will redirect for that beta.