Jump to content

Timewarp Rotation Fix


skykooler

Recommended Posts

I would like to ammend my previous statement. You want to use rigidbody.MomentOfInertiaTensor to create the principal axis representation of the Moment of Inertia Tensor. Separately, you then want to use rigidbody.inertiaTensorRotation to express the angular velocity you want in terms of the principal axis basis. Multiply the first times the second to get the angular momentum, then transform it back to inertial coordinates. Dividing by fixed delta time will give you the torque you need to get the rigidbody up to the angular velocity in a single physics step.

Link to comment
Share on other sites

I have no idea what you just said, but I think SyFy could base a TV show on your last two comments alone. :D

I would like to ammend my previous statement. You want to use rigidbody.MomentOfInertiaTensor to create the principal axis representation of the Moment of Inertia Tensor. Separately, you then want to use rigidbody.inertiaTensorRotation to express the angular velocity you want in terms of the principal axis basis. Multiply the first times the second to get the angular momentum, then transform it back to inertial coordinates. Dividing by fixed delta time will give you the torque you need to get the rigidbody up to the angular velocity in a single physics step.
Link to comment
Share on other sites

Okay, so the dynamics of rigidbodies are very difficult to describe without launching into a full on lecture, so I wrote some C# code in unity to show what I was trying to say:


Vector3 principalMOIs = rigidbody.inertiaTensor;

// All quantities below are coordinatized in terms of the inertially fixed universal basis.

// Calculate the principal axes of inertia
principalXAxis = transform.rotation * rigidbody.inertiaTensorRotation * Vector3.right;
principalYAxis = transform.rotation * rigidbody.inertiaTensorRotation * Vector3.up;
principalZAxis = transform.rotation * rigidbody.inertiaTensorRotation * Vector3.forward;

// Calculate moments of inertia
Ixx = principalMOIs.x * principalXAxis.x * principalXAxis.x + principalMOIs.y * principalYAxis.x * principalYAxis.x + principalMOIs.z * principalZAxis.x * principalZAxis.x;
Iyy = principalMOIs.x * principalXAxis.y * principalXAxis.y + principalMOIs.y * principalYAxis.y * principalYAxis.y + principalMOIs.z * principalZAxis.y * principalZAxis.y;
Izz = principalMOIs.x * principalXAxis.z * principalXAxis.z + principalMOIs.y * principalYAxis.z * principalYAxis.z + principalMOIs.z * principalZAxis.z * principalZAxis.z;

// Calculate products of inertia
Ixy = principalMOIs.x * principalXAxis.x * principalXAxis.y + principalMOIs.y * principalYAxis.x * principalYAxis.y + principalMOIs.z * principalZAxis.x * principalZAxis.y;
Ixz = principalMOIs.x * principalXAxis.x * principalXAxis.z + principalMOIs.y * principalYAxis.x * principalYAxis.z + principalMOIs.z * principalZAxis.x * principalZAxis.z;
Iyz = principalMOIs.x * principalXAxis.y * principalXAxis.z + principalMOIs.y * principalYAxis.y * principalYAxis.z + principalMOIs.z * principalZAxis.y * principalZAxis.z;

// Calculate moment of inertia tensor
ITensor = new[] { new Vector3(Ixx, Ixy, Ixz), new Vector3(Ixy, Iyy, Iyz), new Vector3(Ixz, Iyz, Izz) };

What you'll want to do is this:


Vector3 deltaAngVelocity = new Vector3(0, Mathf.Deg2Rad * 15, 0);
Vector3 deltaAngMomentum = new Vector3(Vector3.Dot(ITensor[0], deltaAngVelocity), Vector3.Dot(ITensor[1], deltaAngVelocity), Vector3.Dot(ITensor[2], deltaAngVelocity));
rigidbody.AddTorque(deltaAngMomentum, ForceMode.Impulse);

That will apply the amount of torque you need to instantly change your angular velocity by the desired amount. I've tested it for arbitrarily distributed point masses (basically the same kind of thing you're dealing with in KSP), and it does work. Hope that helps! :)

Edited by tntristan12
Link to comment
Share on other sites

I would like to ammend my previous statement. You want to use rigidbody.MomentOfInertiaTensor to create the principal axis representation of the Moment of Inertia Tensor. Separately, you then want to use rigidbody.inertiaTensorRotation to express the angular velocity you want in terms of the principal axis basis. Multiply the first times the second to get the angular momentum, then transform it back to inertial coordinates. Dividing by fixed delta time will give you the torque you need to get the rigidbody up to the angular velocity in a single physics step.

If I understand you correctly, this would subject every part of my (rotating) vessel to a torque of angular_velocity / physics_delta, right? Is it just me, or would this create acceleration potentialy exceeding the g-force limits you have to deal with when using e.g. Deadly Reentry, with otherwise safe angular velocity?

Link to comment
Share on other sites

If I understand you correctly, this would subject every part of my (rotating) vessel to a torque of angular_velocity / physics_delta, right? Is it just me, or would this create acceleration potentialy exceeding the g-force limits you have to deal with when using e.g. Deadly Reentry, with otherwise safe angular velocity?

You would only need to apply it to the root part. Applied torques are point independent for a rigid body. Now, keep in mind that bodies in KSP are not perfectly rigid, so there may or may not be some whipping effect. I'm not sure how to get around that. One possible way would be to divide the total torque applied by the number of parts and apply an equal torque to each part.

EDIT: The best way I can think of is to calculate the torque required to rotate each part (separately) to the desired angular velocity, then apply those impulses to each part. You can do this by looping through every part on the vessel and running the algorithm I have described. Because matrix operations are linear, superposition should apply and you will get the same angular velocity for the entire ship, but with torques distributed more evenly taking into account the mass distributions of each part.

Obviously there would be heavy torques for very fast spinning objects, but Unity has a limit on angular velocity anyway so I highly doubt you'd ever get torques that could damage the ship unless you built a centrifuge large enough to crush the crew anyway. :P

Edited by tntristan12
Link to comment
Share on other sites

how does it work with mechjeb? anyone has tried yet? with my use of mechjeb without this as soon as it aligns with the node it warps immediately but it's fine because the rotating stops, which this fixes..

Also anyone tried a huge ship with this? a space station?

Please don't take this the wrong way, but if killing rotation in stock is a bug, I hope they never fix it. It really isn't an issue.

It is an issue, you shouldn't be able to instantly stop your craft by just "skipping time", if you rotate in space, you continue rotating forever unless a force acted upon you (Reaction Wheel or RCS use) One issue this bug creates is the inability to create centrifuges, rotating space stations, rotating parts, etc

Edited by lyndonguitar
Link to comment
Share on other sites

For creating something you intentionally want spinning, I can see it being a problem. On the flip side, I would rather have the rotation stopped immediately as opposed to the spin being stuck on rails as well as the craft (unless I was designing it to rotate.) If I have RCS or SAS, then the SAS should continue to function properly under time acceleration, the ship should eventually stop. In that sense, the stock way of doing things is more accurate than the idea of it continually spinning without any SAS being applied until time warp is turned off (which is the way I understand this mod works,please correct me if I am wrong on that.) I would love to see it function properly, but not go from one extreme of insta-kill rotation to the other of forever spin.

EDIT: I see he has since updated the mod for SAS to work more properly. Still needs some tweaking, but I think it is on the right track.

Edited by metl
Link to comment
Share on other sites

I think I've encountered a bug with this mod. It appears to be multiplying the magnitude of rotation after coming out of timewarp.

In the video you can see I start out in 1X time with a small rotation, go to 50X, and back out to 1X. Each time I do this the rotation is greater. I turn on SAS and the rotation is slowly nulled, but if I turn it off, then timewarp again, I see a repeat of the same behavior of each round of timewarp increasing the rotational velocity.

I suspect this might just be numerical instability on Unity's side. Conservation of angular momentum is on the roadmap for my mod (admittedly rather far away), and since I'll have to do most of the integration myself, my approach should fix the issue. In the meantime, perhaps you can try applying a few corrections in the FixedUpdates after coming out of warp to get the angular momentum to its expected value. Good luck!

Link to comment
Share on other sites

Is the rate of RCS monopropellant consumed to keep orientation based on anything? It seems like maybe 3 or 4 times more than I would expect.

Unfortunately, there's a bug in KSP that prevents me from requesting less than 0.00001 unit per physics tick. As the amount should be proportional to the mass of the ship, I have it request 0.00001*mass. So it does consume a fair bit, especially noticeable on small crafts. My suggestion would be, if you want to end up pointing in a specific direction and don't have much monopropellant, start out that way and add a little roll instead - let the gyroscopic force keep you aligned. Alternately, if you have solar panels or RTGs, you can turn RCS off and then it will just consume electricity.

Link to comment
Share on other sites

Ideally an actual roll would be good, but for something like keeping a non-tracking solar array aligned with the sun during an interplanetary transit, it would be extremely difficult to set up manually. Do any mods offer the ability to very finely tune rotation? I guess you'd still need to do some math to determine the exact rate necessary.

Link to comment
Share on other sites

Ideally an actual roll would be good, but for something like keeping a non-tracking solar array aligned with the sun during an interplanetary transit, it would be extremely difficult to set up manually. Do any mods offer the ability to very finely tune rotation? I guess you'd still need to do some math to determine the exact rate necessary.

I want to add an option to allow orb-rate rotation for SAS, but I'm stuck in that I don't quite understand the coodinate system KSP is using.

Link to comment
Share on other sites

  • 3 weeks later...

Any updates on this plugin? I'm very very eager to see the rotation increase from time warp getting fixed. It's the only real problem I have with this great plugin. I sent out some probes to Duna, and the rotation got so amplified, it actually caused a significant deviation in the path of just one of the probes. Enough to cause an course error so large I didn't have enough delta-v to fix it.

Link to comment
Share on other sites

  • 2 weeks later...

Hey skykooler. I don't know if you're still working on this mod or not but it really does have potential. I would like to help you out on it if you'd like! I'm a graduate student studying aerospace engineering and the rotational dynamics of spacecraft in particular. Send me a PM if you'd like the help!

Link to comment
Share on other sites

Hey skykooler. I don't know if you're still working on this mod or not but it really does have potential. I would like to help you out on it if you'd like! I'm a graduate student studying aerospace engineering and the rotational dynamics of spacecraft in particular. Send me a PM if you'd like the help!

I'd love your help, but unfortunately my system is still down and I can't work on plugins until I get it fixed. Hopefully that will happen soon.

Link to comment
Share on other sites

  • 4 weeks later...
  • 4 weeks later...

Is anybody looking at this again? My offer to help still stands as I have a working knowledge of spacecraft rotation dynamics and coding in C#. However, I would need somebody more familiar with integrating code into the KSP framework and making it compatible with new updates / other mods. If the OP would give me his blessing I'd be happy to contribute in my off time (of which I have plenty since this semester just ended).

Link to comment
Share on other sites

Is anybody looking at this again? My offer to help still stands as I have a working knowledge of spacecraft rotation dynamics and coding in C#. However, I would need somebody more familiar with integrating code into the KSP framework and making it compatible with new updates / other mods. If the OP would give me his blessing I'd be happy to contribute in my off time (of which I have plenty since this semester just ended).

This could be used for (rotational) station-keeping for scanning and communication satellites, could it not?

You do not need the blessing of OP to contribute (if it is open source). You may or may not need the blessing of OP to take over the project.

I may have a direct interest as the maintainer of SCANsat, but only if this can afford SCANsat some benefit (in terms of demanding more realistic requirements of scanning vessels).

I didn't even know this existed until you bumped today.

Link to comment
Share on other sites

This mod really helped ween me off of using timewarp to steady my ship's rotation, but it's not ideal. As far as benefiting scansat, I think that in general it adds an extra layer of challenge to the proceedings, since you must consume resources to maintain attitude. Perhaps this would allow the implementation of some sort of gravity gradient stabilization simulation (it'd have to be faked since actual gravity gradient is no longer in the game) which would make planet-facing satellites possible. There are quite a few possibilities!

Link to comment
Share on other sites

I'm definitely interested in this... guilty here of warping to stop rotation of huge ships/asteroids. I'm a little worried about the claimed increases in rot speed after coming out of warp.

Hrmm... as far as orbital planet-facing, how about telling Mechjeb to hold a particular SURF orientation to establish the initial spin, then engaging warp? I would love love love to have cupolas always pointing down to Kerbin.

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