Jump to content

The official unoffical "help a fellow plugin developer" thread


Recommended Posts

Okay. Time for what I'm hoping is an easy question.

I'm looking to call the force value of lift from atmospheric forces during flight as a number.

Or more simply, how much lift force are my wings giving me? Returned as a number so I can plug it into my thrust calculation.

I've been looking around trying to find out how to calculate this and I've made exactly zero progress. I've checked several other mods and was not able to find if they are calculating lift.

The only real lead I had was that the Object Browser in Visual Studio told me there was a CenterOfLiftQuery.lift value available, but from a Developer's post back in 2012 that query is available only in the editor.

So, I'm currently stuck at square one and don't even know how to start.

I'm not trying to edit wings or generate lift or anything, I simply want to know at this instant, how much lift force is being generated from the atmosphere?

Anyone have any ideas?

D.

Okay, quoting myself since I've figured an answer out and figure it's worth sharing as the lift forces are not exposed anywhere in the KSP API that I could find.

The problem I faced was that I have an automated engine throttle that works great as long as no lift from wings is present. I needed to calculate how much lift the wings are generating and throttle back that much.

Note that I was only concerned with vertical acceleration (straight up and down) for my mod.

To find the lift required taking advantage of the fact that OnFixedUpdate() runs a specific number of times a second.

First, using a timer, calculate how many times OnFixedUpdate() runs per second in this game instance. I believe both the Delta-Time Physics in the Game Settings and the lag from high part count ships affect it so it's done on the fly in code.

Then get the difference between Vessel.verticalvelocity on this FixedUpdate frame and the last, then multiply it by the number of FixedUpdate frames per second to get my 'instantaneous' acceleration. On my test machine, FixedUpdate was running 50 times a second, so if the velocity difference was 0.01, my current acceleration was 0.5m/s^2. This turned out to be accurate, but very 'bouncy' as it could vary as much as 30m/s^2 from one FixedUpdate frame to the next, so I average the acceleration over the last 5 FixedUpdate frames to get a smooth number.

Because I do my actual thrust calculation in Newtons, I then multipy the ActualAcceleration, from the last FixedUpdate frame I have just calculated, by the Vessel.mass to get how much force was actually exerted on my Vessel. I then subtract what my mod applied the previous frame with just the engines and that difference is how much lift is being applied by the wings on the Vessel.

Kludgy, but it works. I could not find any sort of way to get the actual force numbers for lift in the flight scene so I had to calculate it out manually. As I care about only straight up and down I did not have to mess with Vectors that the forces are being applied so just using Vessel.verticalvelocity works for my purposes.

D.

Link to comment
Share on other sites

You were perhaps tkaing it hard on yourself.

First, no need for a timer, Time.fixedDeltaTime is the time taken by one physics frame, no need for a timer to get it.

Then, the lift generated by wings is a compiled with a force. This force is in kN, is calculated by the wings each frame and isn't stored by ModuleAerodynamicLift. However you can just recalculate it with the right equations and obtain the same lift. Looping through all the parts that have the module each frame and calculating the force isn't that hard and is likely more precise than your instantaneous upwards acceleration.

Link to comment
Share on other sites

EDIT: Nevermind! I didn't think to add the ScienceContainerModule thing to the part itself. Works beautifully. Are you okay if I copy or break this code down and make my own rough version?

Sure; there is a bit based on GPL code from station science there, and there was an even bigger bit of GPL code by Toadicus, but that became unnecessary in 0.23. If you don't need to generate reports piecemeal, you don't need most of the complexity however. Actually, the main reason I went with the container approach in the first case, is so that I can keep multiple partially-complete reports for different biomes, and add to them as the plane flies around.

I probably should post this mod somewhere in Development or something, in case somebody finds it useful.

Link to comment
Share on other sites

I had a look with Interstellar's method and found that yours was much simpler for my purposes. I just needed a way to add a report of my own to the ship/container. I think I can reconstruct it so I can have it as my own but if my project ever finishes I'll be sure to credit you for the help :)

Link to comment
Share on other sites

You were perhaps tkaing it hard on yourself.

First, no need for a timer, Time.fixedDeltaTime is the time taken by one physics frame, no need for a timer to get it.

Then, the lift generated by wings is a compiled with a force. This force is in kN, is calculated by the wings each frame and isn't stored by ModuleAerodynamicLift. However you can just recalculate it with the right equations and obtain the same lift. Looping through all the parts that have the module each frame and calculating the force isn't that hard and is likely more precise than your instantaneous upwards acceleration.

Except I did not have those equations, or not in a form I could use.

I did search for old threads and there were some derived equations for how KSP generates lift, but nothing in a form I could use (or with enough details I could re-work them for this purpose). And so I did this because I believed it would be faster then trying to work the lift equations out for myself and because it should be compatible with both the base game and FAR (tests on that are still pending though).

However, thank you for the Time.fixedDeltaTime tip, I thought that value was the same thing as the Physics Delta Time slider in the game settings box.

D.

Link to comment
Share on other sites

Does anyone know what KSP is doing, exactly, when the user disables the GUI by pressing F2? Someone was asking if they could keep my HUD visible while disabling the rest of the GUI. Obviously they aren't completely disabling Unity's GUI drawing since you can still show the console, but they're doing something pretty global since I didn't have to add any code to make my GUI disappear. Has anyone done any work along those lines? Thanks.

Link to comment
Share on other sites

RenderingManager that you're using implements hiding automatically, and to avoid that you'd have to define OnGui callbacks directly.

Another thing to consider is of course that some people may want to hide it; and using the predraw queue of RenderingManager guarantees that the locked HUD is below navball and some other stock GUI parts.

Link to comment
Share on other sites

Except I did not have those equations, or not in a form I could use.

I did search for old threads and there were some derived equations for how KSP generates lift, but nothing in a form I could use (or with enough details I could re-work them for this purpose). And so I did this because I believed it would be faster then trying to work the lift equations out for myself and because it should be compatible with both the base game and FAR (tests on that are still pending though).

However, thank you for the Time.fixedDeltaTime tip, I thought that value was the same thing as the Physics Delta Time slider in the game settings box.

D.

I think with enough cleverness, the equations for both FAR and stock lift can be obtained, but that's up to you :P

Link to comment
Share on other sites

RenderingManager that you're using implements hiding automatically, and to avoid that you'd have to define OnGui callbacks directly.

Another thing to consider is of course that some people may want to hide it; and using the predraw queue of RenderingManager guarantees that the locked HUD is below navball and some other stock GUI parts.

Yeah, that's kind of what I was figuring. Although you could still disable gauges before going into hidden mode, that would probably be more work than most people would want to do. And I don't really want to write two systems for drawing the windows to make it a toggleable option, so I guess he'll just have to live without.

Link to comment
Share on other sites

Stupid Simple Cfg Modding Question here:

I was trying to duplicate one of the Squad parts however I do not want to copy the entire part folder because the game will need to handle the mesh file and eat up memory.

Also I was trying to keep all my cfgs in one folder, so I do not want to place any additional cfg in the Squad folder.

Will all that in mind, I have copied the original cfg, changed the name = XXX and

model = Squad/Parts/Engine/liquidEngine1/model

to be honest I don't even think this is gonna work, however from the Globe I srb of KW Rockery, the line

model = KWRocketry/Parts/Solids/025mGlobeI/KW_025mSRBGlobeI

exists instead of model = KW_025mSRBGlobeI

When I loaded up the game the part won't show up. I assume it's because the model file cannot be found.

Also I have been modding the game entirely with modulemanager,

If duplicating can be done with MM please tell me how.

I don't think modulemanager can work outside Part{} though.

Link to comment
Share on other sites

How do I get altitude (AGL and AMSL) from this version of KSP for my plugin? I have tried most examples I have found but about all of them always return 0 altitude.

Well first to get ASL, you can get it using FlightGlobals.getAltitudeAtPos(FlightGlobals.ship_pos);

Getting AGL is a little more tricky though. This is how I did it for RealChute:

ASL = FlightGlobals.getAltitudeAtPos(pos);
terrainAlt = this.vessel.pqsAltitude;
if (this.vessel.mainBody.ocean && terrainAlt < 0) { terrainAlt = 0; }
trueAlt = ASL - terrainAlt;

That should do the trick in a PartModule.

Stupid Simple Cfg Modding Question here:

I was trying to duplicate one of the Squad parts however I do not want to copy the entire part folder because the game will need to handle the mesh file and eat up memory.

Also I was trying to keep all my cfgs in one folder, so I do not want to place any additional cfg in the Squad folder.

Will all that in mind, I have copied the original cfg, changed the name = XXX and

model = Squad/Parts/Engine/liquidEngine1/model

to be honest I don't even think this is gonna work, however from the Globe I srb of KW Rockery, the line

model = KWRocketry/Parts/Solids/025mGlobeI/KW_025mSRBGlobeI

exists instead of model = KW_025mSRBGlobeI

When I loaded up the game the part won't show up. I assume it's because the model file cannot be found.

Also I have been modding the game entirely with modulemanager,

If duplicating can be done with MM please tell me how.

I don't think modulemanager can work outside Part{} though.

You'll need to use MODEL{} nodes if you want to copy models with URLs.

Link to comment
Share on other sites

Hello, I am very new to modding and am working on a mod for rovers someone asked me to create a noded wheel for easier use with the Infernal Robotics mod and upon trying to set the node definitions I cannot seem to find out how set up a node that goes out of the x-axis... I am right now using the stock medium wheel

I have set up correct node definitions but here is what I have currently set up after trying many times to make the node appear at the right place... however now it doesn't even show a attachment node:

// --- node definitions ---

node_attach = 1, 0.0, 0.0, 0.0, 0.0, 0.0, 1

attachRules = 1,0,0,0,0

I would appreciate any guidance :D

Link to comment
Share on other sites

How do I parse a FloatCurve from a config file?

I'm trying to add some sounds to the engines, whose pitch/volume is tied to throttle level. I know I could use the new FX module, but I want multiple sounds at a time (layered). I wanted to mimic the curves for pitch and volume, but I'm a bit confused on how to get them from the config to the code.

So, in the .cfg is:


volumeCurve
{
key = 0.0 0.3
key = 1.0 1.0
}

So, 30% volume at 0 throttle, and 100% volume at 100% throttle. In the .dll I've done this to set the FXGroup's volume:


audioGroup.audio.volume = GameSettings.SHIP_VOLUME * volumeCurve.Evaluate(currentThrust/maxThrust);

If I define it linearly in the code, like so:


audioGroup.audio.volume = GameSettings.SHIP_VOLUME * Mathf.Lerp(0.33f, 1f, currentThrust/maxThrust);

It works like I expect.

I suspect there is something I have to do to the config node that I am unaware of, so any direction would help! :)

EDIT: I seem to have figured out how to accomplish this with the EFFECT{} nodes. Just needed to make sure each AUDIO node had a unique name, and we're in business. But, now I need to know if I can stop a currently running effect. I can set the effectPower to 0 but if I don't define a curve with the 0.0 key, then it runs indefinitely.

Edited by Raptor831
Link to comment
Share on other sites

How do you check if RCS is on? I tried FlightUIController.fetch.rcs.enabled and FlightUIController.fetch.rcs.isOn, but both were always false. (this was the only thing I could find so far relating to whether rcs was on or off..)

Link to comment
Share on other sites

Odd, you are correct that there is no Get method for that.

After some poking around, this is a two part answer.

First, it looks like Action Groups are defined in the KSPActionGroup boolean enum.

This enum is then assigned to each vessel to get a true/false for each action group on the vessel. This is stored in MyVessel.ActionGroups.groups location.

However, the .groups is simply an enum of booleans so you have to refer to the KSPActionGroup enum to find which position is the RCS group.

I did a quick test with

print(MyVessel.ActionGroups.groups[15]);

and it turns out position 15 is actiongroup 9 as the 9 key would swap between true and false (being printed in the log) for that group.

At this point I'm out of time, you can either find the correct position via testing, or you can do something fancier and look up the position for RCS from the actiongroups enum.

(I don't have code handy for that on hand though.)

D.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...