Jump to content

API for ModuleAlternator, and two litte questions related to the class Orbit


Recommended Posts

Hello,

i am searching for some info about the ModuleAlternator module in KSP.

I couldn't find anything useful in the public KSP API Documentation, or somewhere else through google.

Specifically, i need to find a way to get the resource consumption/creation rate (and of course what ressource it is) of the ModuleAlternator.

That's my main question, anything below this is optional.

Some background info:

Why?

I am attempting to write my first mod, it is also my first attempt at creating something C# related on my own.

It would be a mod that enables timewarp while some engines (electric engines for example) are still running. Unfortunately there aren't any still around that work. I tried to fix one of them on/for my own, and partially succeeded; but there is one bug (found in all related mods) that i could not fix, but i think i have an idea*(see below). It's the issue where a craft "pulls" it apoapsis around and cannot really pass it, no matter how low the acceleration is, messing the orbit up (basically rotating the orbit in it's plane). So, i thought i should try to roll out my own mod.

Why do i need ModuleAlternator? Because some engines use it (especially NF-electric engines in Realism Overhaul), and it would be a shame if they didn't work in timewarp (while the engine is running).

I hope somebody knows more about ModuleAlternator, otherwise i would have to create another module only for this and use ModuleManager to set it to the ModuleAlternator-parameters for every part.

That would be easy, but i don't like this approach at all. A little too intrusive and unelegant, knowing how ModuleAlternator works and how to communicate with it would be much better.

* All timewarp-engines-mods i have seen that also have this issue (all, basically) use the orbit.UpdateFromStateVectors(...) method to update the orbit. I narrowed the issue down to this method.

I would try to use myOrbit.UpdateFromOrbitAtUT (Orbit orbit, double UT, CelestialBody toBody) method instead, creating another Orbit-class that has the correct parameters.

Well, i would need to figure out some orbital physics/mathematics, but that is a minor issue. Google will help me out at finding the needed equations :wink:

My two other questions:

While i'm at it, i have two other little questions:

I found out (the hard way) that the Orbit class needs the y and z coordinates of vectors swapped, is it correct that this is the only class that does this?

And concerning orbital mechanics, what is meant by "epoch" in "mean anomaly at epoch" in ksp? Wiki says "The value M0 denotes the mean anomaly at epoch, which is the mean anomaly at the time the measurement was taken" when was "the measurement taken" in ksp? Is it at universalTime = 0 ?

I hope somebody can help! Any opinion or hint (also not directly related to the questions) is very welcome! :)

Please note that this is my first attempt at programming mods for ksp, also my first real attempt at c#. My only experience at this is from looking at source files and manipulating existing mods to fit my needs :D

Edited by Tonnz
Link to comment
Share on other sites

Oh, forget about my first question.

I found out that you can use the Object Browser of Visual Studio to find out more information about this kind of stuff.

I used notepad++ to create, and the inbuilt windows command-line tool to compile .cs files :blush:

EDIT: Figured the Alternator thing out! :D

Edited by Tonnz
Link to comment
Share on other sites

The Y/Z switch originates with the vessel class. Y (up) is forward for a rocket on the launchpad (and thus vessel.up is facing for every vessel), and Z (forward) becomes down relative to the rocket. If you need to check vector orientations, fastest way is to draw them using the ArrowPointer class

ArrowPointer pointer;
public void drawArrow(Vector3 dir)
{
if (pointer == null)
ArrowPointer.Create(part.transform, Vector3.zero, dir, 10, true);
else
pointer.direction = dir;

// if used in release code, clean it up manually with: Destroy(pointer); after you're finished drawing
}

Edited by Crzyrndm
Link to comment
Share on other sites

Thanks for helping! :)

Well, i'm confused a little:

You say that y is up for a rocket on the launchpad. How does z (forward) become "down"? Wouldn't "down" be just the negative values for Y? I'm confused. Are we talking about actual coordinates or pre-set directions, where z would return a vector that is "forward" relative to the rocket? I'm going to have a closer look at the ArrowPointer class. Will it actually draw the direction in-game? I'll have to try it out, would really be useful :)

Link to comment
Share on other sites

Arrow pointer is an ingame visualisation (it's what stock uses for aero force display I believe) and yes it's extremely useful (except for the directions it refuses to draw in for some reason. Don't know why that happens. Make sure to spin around a bit if you can't see it).

RE: facing vectors

Well that was slightly confusing. It would seem you are talking about vector.x/y/z, while I was thinking about vessel.transform.forward(Z)/up(Y)/right(X). Anyway...

Transform t = vessel.transform; // the position and rotation of the root part of the vessel
Vector3 vesselFacing = t.up; // vessel relative forward is transform.up (vessel facing vector)
Vector3 vesselRight = t.right; // vessel relative right
Vector3 vesselDown = t.forward; // if up is forward and right is right, forward becomes vessel relative down

You should be able to use one of the three above vectors instead of manually switching y/z unless I am very much mistaken (Vector.y/z have no real meaning in KSP because world down can be any direction so switching it sounds very dangerous to me)

Link to comment
Share on other sites

RE: facing vectors

Well that was slightly confusing. It would seem you are talking about vector.x/y/z, while I was thinking about vessel.transform.forward(Z)/up(Y)/right(X). Anyway...

Transform t = vessel.transform; // the position and rotation of the root part of the vessel
Vector3 vesselFacing = t.up; // vessel relative forward is transform.up (vessel facing vector)
Vector3 vesselRight = t.right; // vessel relative right
Vector3 vesselDown = t.forward; // if up is forward and right is right, forward becomes vessel relative down

You should be able to use one of the three above vectors instead of manually switching y/z unless I am very much mistaken (Vector.y/z have no real meaning in KSP because world down can be any direction so switching it sounds very dangerous to me)

Thank you, now i get it. I was thinking about positions rather than directions. One further question: Are the y/z cord. swapped for all Transforms? Because this would give me a nice explanation for the problem i just ran into (my craft got dragged towards the planet i'm orbiting, very subtle effect, but enough to drag me inside the atmosphere from LKO). I use an integration approach for calculating the distance a craft has travelled while on-rails warp (only accounting for the distance the running engines added). I use the thrustTransforms Transform from the ModuleEngines to determine the direction in which the engine fires, i base my calculation on this, and swap y/z when updating my orbit with it. If it was already swapped to begin with, it would mess everything up. I think it's time for me to use ArrowPointer ​and see what is going on. The velocity-change works fine though, and is also swapped. I'll have to take a closer look.

Edited by Tonnz
Link to comment
Share on other sites

Some things use .forward, some don't. Fastest way to check a whole lot at once is to project them onto vessel.up

float projectionLength = Vector3.Dot(vessel.up, vectorToCheck.normalised); // will be 1 or very close to it for aligned vectors, 0 for perpendicular vectors, -1 for opposing vectors. It's the cosine of the angle between them

You can just do one huge debug statement with all the vectors you want to check that way

Link to comment
Share on other sites

Some things use .forward, some don't. Fastest way to check a whole lot at once is to project them onto vessel.up

float projectionLength = Vector3.Dot(vessel.up, vectorToCheck.normalised); // will be 1 or very close to it for aligned vectors, 0 for perpendicular vectors, -1 for opposing vectors. It's the cosine of the angle between them

You can just do one huge debug statement with all the vectors you want to check that way

I should have thought about that :confused:. I'm also going to try that one out.

Thank you very much for your help so far. I'll get back if more questions/problems arise :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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...