Aligning ship to planet gravity (2024)

  1. NanobyteTrainee Engineer

    Messages:
    25
    Does anybody have any clue on how to approach this? Aligning ship to planet gravity (2)

    Nanobyte,Nov 15, 2015

    #1

  2. plaYer2kMaster Engineer

    Messages:
    3,160
    sure, here is a suggestion for how to approach it
    • get the natural gravity through a remote
    • get all gyros you want to use in order to turn
    • get the current orientation so you can compute the angular offset from current to ideal orientation
    • transform the orientation offset to all gyros to turn them in that direction
    • optionally apply some smooth magic like PID or other dynamic meassure system to guess the moment of inertia tensor in order to build a simple physics model
    or if all that is too much coding then just get the gravity vector and use the remote controls autopilot with some simple vector math

    plaYer2k,Nov 16, 2015

    #2

  3. NanobyteTrainee Engineer

    Messages:
    25
    Thanks player, i like a coding challenge so ill start with the above, and if that causes me too much headaches ill continue with the last option Aligning ship to planet gravity (5)

    Nanobyte,Nov 16, 2015

    #3

  4. plaYer2kMaster Engineer

    Messages:
    3,160
    Sure that. Also if you got issues at one of the steps, dont hestitate to ask for help.

    For months now i was too busy/lazy to do the "orient gyro towards point" now but i will need it too soon. I will also need to code that myself because i dont like most of the implementations i have seen.
    So i dont know all the answers myself but "the community" as a whole does. Not that the tast is particularly hard but i simply didnt bother doing it yet Aligning ship to planet gravity (7)

    Good luck and enjoy the tinkering.

    plaYer2k,Nov 16, 2015

    #4

  5. NanobyteTrainee Engineer

    Messages:
    25
    Ok i will keep you updated on anything that works out the way it should, im currently trying to align my vehicles down vector with the data from GetNaturalGravity()
    But im not sure if that vector even is a normal direction vector. Its been a long since i've done vector math so im a little rusty on that end Aligning ship to planet gravity (9)

    Nanobyte,Nov 16, 2015

    #5

  6. HarrekinMaster Engineer

    Messages:
    3,077
    Vector from ship to centre of planet.
    Vector of ships direction of travel.

    Get dot product and adjust gyros to keep it as close to zero as possible.

    Harrekin,Nov 16, 2015

    #6

  7. plaYer2kMaster Engineer

    Messages:
    3,160
    But what is a one-dimensional value worth when you got three dimensions to rotate in Aligning ship to planet gravity (12)
    So no it aint that simple, but it aint much worse than that too.

    plaYer2k,Nov 16, 2015

    #7

  8. NanobyteTrainee Engineer

    Messages:
    25
    I'm stumped at the moment, i think im getting confused by the gyro itself. Not sure if i'll be able to break this one

    Nanobyte,Nov 16, 2015

    #8

  9. HarrekinMaster Engineer

    Messages:
    3,077
    Youre only looking to maintain a right angle from the ship to the centre of the planet tho so only two options, no?

    Harrekin,Nov 16, 2015

    #9

  10. plaYer2kMaster Engineer

    Messages:
    3,160
    Yeah but in which direction is important aswell. What is a value of 0.3 worth when you dont know around which of the three axis the gyros have to turn around? Would you want to define that in a fixed manner for every program and physical setup you got?

    Instead the car needs to have a defined orientation. Transforming the gravity vector through the inverted orientation matrix gives a local gravity vector. With that local vector you can then form the parameters for rotations around that anchor block. Things like PID come to mind there.
    Now that you got your rotational parameters you can transform these to the local orientation of each gyro to kill their three rotational parameters aswell as power as optional parameter.

    plaYer2k,Nov 16, 2015

    #10

  11. NaosythTrainee Engineer

    Messages:
    9
    I actually programmed something really similar yesterday. I can share my code when I get home from work in a few hours. I determine my roll and pitch using my forward and right vectors in relation to the gravity vector. I then use a PID controller to adjust my pitch and roll to bring my ship to a stop, using a helicopter-like flight model.

    Naosyth,Nov 16, 2015

    #11

  12. AutoMcDSenior Engineer

    Messages:
    2,369
    I have a somewhat related idea that maybe you can include:
    To automatically enable/disable UP thrusters so that the ship thrust/weight Going upwards matches the side thruster performance, after factoring in weight and gravity.

    This would make something with a LOT of cargo lifting capacity much more predictable to fly.

    AutoMcD,Nov 16, 2015

    #12

  13. NanobyteTrainee Engineer

    Messages:
    25
    Sounds cool Naosyth, if you wouldn't mind sharing, i am still bumping into things to try and figure out how to get it working correctly Aligning ship to planet gravity (19)

    Nanobyte,Nov 16, 2015

    #13

  14. JoeTheDestroyerJunior Engineer

    Messages:
    573
    Threw this together, seems to work reasonably well:

    Just have a timer running the PB and "trigger now"-ing itself. It's setup to align based on the remote control block. (If you have more than one, it will take the first one it finds.) If you want to use a different reference, just changing how "down" is assigned should be enough (down is in grid-local coordinates).

    By default it will use all the gyros it can find (well, 999). This works best if you don't need any steering. If you need steering try setting the limit lower, then the script will allow you to pitch/roll but will slowly correct it (yaw should be unaffected.)

    Last edited: Nov 17, 2015

    JoeTheDestroyer,Nov 17, 2015

    #14

    • Aligning ship to planet gravity (21) Like x 3
    • NanobyteTrainee Engineer

      Messages:
      25
      I will not need the steering, i just want to make a drone that is able to achieve the above, but i keep having the feeling that im either getting the wrong values, or that i keep doing something wrong in the math

      Nanobyte,Nov 17, 2015

      #15

    • NanobyteTrainee Engineer

      Messages:
      25
      Derp, i didnt notice you input code at first, thanks! ill play around with this tomorrow see if i can achieve what i like, im sure it will teach me multiple things Aligning ship to planet gravity (24)

      Nanobyte,Nov 17, 2015

      #16

    • NanobyteTrainee Engineer

      Messages:
      25
      Joe thank you very much for sharing ! This does exactly what i was trying to achieve, and i learned that the way i approached it especially the gyro part is what held me back. You saved me a lot of headaches Aligning ship to planet gravity (26)

      Nanobyte,Nov 17, 2015

      #17

    • AutoMcDSenior Engineer

      Messages:
      2,369
      Anyone have it shared on workshop? Aligning ship to planet gravity (28)

      AutoMcD,Nov 17, 2015

      #18

    • JoeTheDestroyerJunior Engineer

      Messages:
      573
      No problem, I'm glad all that digging I did in the gyroscope code on github finally proved useful Aligning ship to planet gravity (30)
      Not me, I don't bother much w/ the workshop.

      If somebody else wants to clean it up and post it, that's fine by me.

      JoeTheDestroyer,Nov 17, 2015

      #19

    • AutoMcDSenior Engineer

      Messages:
      2,369
      Ok. Makes it easier to use in-game when you can paste it in straight off steam.

      AutoMcD,Nov 17, 2015

      #20

    • JoeTheDestroyerJunior Engineer

      Messages:
      573
      I keep all my scripts in the local scripts folder, they can be used the same way as from steam that way. (And I can edit them with Notepad++, rather than the awful in game editor.)

      You can use the "Browse Workshop" -> "Create from editor" option to do this (after you paste the script into the editor the first time).

      JoeTheDestroyer,Nov 17, 2015

      #21

    • NaosythTrainee Engineer

      Messages:
      9
      I reworked my helicopter braking script a bit. It no longer uses a PID loop, just some math to determine angles and rotation rates. You can take a look at it here if you're curious how it works. It's my first Space Engineers program, so any feedback would be great. It takes a slightly different approach to aligning the ship with the planet's gravity, so maybe it will still be useful to you.

      Naosyth,Nov 17, 2015

      #22

    • JoeTheDestroyerJunior Engineer

      Messages:
      573

      Code:

      long now = System.DateTime.Now.Ticks / System.TimeSpan.TicksPerMillisecond;double dt = now - oldTime;
      This will give unexpected results when your simspeed drops (i.e. game time is slower than real time). If it weren't broken atm, you should use ElapsedTime. As it is, the only real choice is to run your script every tick (timer TriggerNow) so that dt=1000/60.

      Code:

      // Get orientation vectorsVector3 normalVec = Vector3.Normalize(remotePos - computerPos);Vector3 forwardVec = Vector3.Normalize(gyroPos - computerPos);Vector3 rightVec = Vector3.Normalize(timerPos - computerPos);
      This isn't necessary anymore. Use the WorldMatrix property of blocks/grids to get a translation/orientation matrix, then the matrix Forward, Right, Up properties to get the directions you want.

      Code:

      Vector3 gravityVec = Vector3.Multiply(Vector3.Normalize(Remote.GetNaturalGravity()), -1);

      Code:

      Vector3 gravityVec = -Vector3.Normalize(Remote.GetNaturalGravity());
      I'm not sure about your math, it looks like it just boils down to proportional control to me. But I maybe missing something.

      The thing about gyros is that even though Keen says everywhere (UI, value names) that the 3 values are yaw, pitch, and roll, they really aren't. They are the X (pitch), Y (yaw) and Z (roll) axis of a rotation vector (well, a rotational velocity vector, really). When only one value is non-zero, then the two are equivalent. When the values are small, they are reasonable approximations of each other. But when the values are large, you will get odd behavior if you treat them as if they were truly yaw/pitch/roll.

      JoeTheDestroyer,Nov 18, 2015

      #23

      • Aligning ship to planet gravity (35) Informative x 1
      • NaosythTrainee Engineer

        Messages:
        9
        Yeah, I initially had a PID controller, but it wasn't working well, and proportional control just worked way better.
        I didn't consider the sim speed issue, definitely a good point.
        Ah, I was trying to figure that out, but didn't have much success. I'll take another look. Thanks!

        Also, for the gyros, right now it only uses one gyro, which I knew the orientation of. I'm planning on making it work with all of the ship's gyros at some point, so I'll make it use the appropriate axis when I do so.

        Edit: Got it working with all gyros now, and changing dt to just be 1000/60 made it much less jittery at low speeds. Overall it's working great, thanks for the feedback and advice Aligning ship to planet gravity (37)

        Last edited: Nov 18, 2015

        Naosyth,Nov 18, 2015

        #24

      • PsicoPatoSenior Engineer

        Messages:
        1,097
        You think this works with spawnships? I am planing to put this on my mod so my spawnships just orientate by themselfs right after spawning.

        Another question. Do i need to set a loop on the Timer block to run the scrip time to time, or running it one time is enough?

        PsicoPato,Nov 18, 2015

        #25

      • JoeTheDestroyerJunior Engineer

        Messages:
        573
        So, yes. To expand, you need:
        1. 1 or more gyroscopes
        2. A remote control oriented so it's "down" face is pointed towards where you want the planet to be
        3. A programmable block w/ the script in it
        4. A timer set to:
          1. Run the programmable block (the argument doesn't matter, leave it blank)
          2. Do "TriggerNow" on the timer itself (setting up a loop)
        It should work, though I'm concerned about timer looping itself via TriggerNow. In the past, an active TriggerNow had issues surviving a save/load cycle, and I suspect it is probably not saved in blueprints at all. To be safe, you should probably add a second timer, looping itself via Start (which has less issues) and also TriggerNow-ing the first timer.

        Also, in this case I would recommend using the "LIMIT_GYROS" option (see the comment in the source) so that the player can still steer their ship, to some extent.

        JoeTheDestroyer,Nov 18, 2015

        #26

        • Aligning ship to planet gravity (40) Informative x 1
        • sepen_Trainee Engineer

          Messages:
          52
          Thanks a lot for this hint! I praise your source code digging dedication! Aligning ship to planet gravity (42)
          Hah! PID is its very own can of worms, it really is.

          sepen_,Nov 19, 2015

          #27

          • Aligning ship to planet gravity (43) Like x 2
          • LynnuxJunior Engineer

            Messages:
            881
            Sorry, seeing this late.
            Made a script some time ago (before planets): Avionics - Catlanding
            In my Zecke vehicle I've added the usage of the gravity vector. Then the ship can be steered left/rigth and forward/backward while keeping the attitude. I'll update the script soon but first I want to know if I also have to add a speed limiter toggling the inertia dampeners and keeping the ship falling at a certain speed.

            Lynnux,Nov 20, 2015

            #28

          • SibzApprentice Engineer

            Messages:
            361
            Could you explain what this is doing?

            I think it changes the normalised vector into one that's relative to the current block. I.e. getting the green vector:
            Aligning ship to planet gravity (46)
            Did I understand this right?

            Sibz,Nov 22, 2015

            #29

            • Aligning ship to planet gravity (47) Like x 1
            • Aligning ship to planet gravity (48) Informative x 1
            • JoeTheDestroyerJunior Engineer

              Messages:
              573
              Yep, that's it precisely. (Although your drawing says local grid, current block is actually the correct one.)

              JoeTheDestroyer,Nov 22, 2015

              #30

              • Aligning ship to planet gravity (50) Like x 2
              Aligning ship to planet gravity (2024)

              References

              Top Articles
              Latest Posts
              Article information

              Author: Van Hayes

              Last Updated:

              Views: 5556

              Rating: 4.6 / 5 (46 voted)

              Reviews: 93% of readers found this page helpful

              Author information

              Name: Van Hayes

              Birthday: 1994-06-07

              Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

              Phone: +512425013758

              Job: National Farming Director

              Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

              Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.