1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

How to get a ship's total mass?

Discussion in 'Programming (In-game)' started by Sepe, Aug 17, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Sepe

    Sepe Trainee Engineer

    Messages:
    8
    I am trying to make a script that adjusts gyros power relative to the ship's total mass.
     
    • Like Like x 1
  2. Sinbad

    Sinbad Senior Engineer

    Messages:
    2,788
    no clue, sorry. but its a great idea! watching with interest...
     
  3. Wicorel

    Wicorel Senior Engineer

    Messages:
    1,263
    There is no way to get ship mass, currently.

    You can get current mass from Inventory owners (including cargo containers), but that mass doesn't count for gyro inertia (as of 1.095).


    Edit for thrusters:
    Keen is scaling inertia with inventory multiplier. (again, as of 1.095) So 10000kg at 1x does not need the same THRUST power (not gyro) as 10x inventory.

    And there is also no way to get the inventory multiplier directly. It could be derived if:
    1) you ASSUME known 1x cargo volumes and
    2) are in survival so you can get max volume
     
  4. Tajin

    Tajin Apprentice Engineer

    Messages:
    270
    How about simply making it a calibration script?

    1. Let it find all gyros and their orientation
    2. Force them to rotate in one direction at full power for a second
    3. See how far the ship actually rotated in the given time
    4. Use that value to adjust the gyro-settings
     
  5. Sepe

    Sepe Trainee Engineer

    Messages:
    8
    Thanks for the responses.

    Tajin's approach would allow easy one time calibration for newly made ships but my intet was to create a script that
    monitors the ship's mass changes and calibrates its gyros accordingly. The ship in question is a modular cargo ship so
    a rotation test every time the mass changes could cause some serious destruction. :)

    It seems that this script might require some effort so I'll focus on something else for now because
    the changes in the rotation speed aren't really a problem at all.
     
  6. plaYer2k

    plaYer2k Master Engineer

    Messages:
    3,160
    The thing is that you compare apples and oranges.
    The mass is relevant for the linear acceleration and thus forces like from thrusters.

    The gyros however are applying a moment and influenced by the moment of inertia (~mass distribution). Furthermore the foment of inertias lowest value (as it is a Vector3) will get used for all three axis.

    So to test the mass once, you had to apply a force and meassure the linear acceleration.
    It gets tricky though when you use multi-grid systems.

    I made a pull request once to get the mass, moment of inertia tensor and several other values for a cube grid. Just waiting for it to get through.

    Edit:
    Like previously said though, there currently is no method to get the mass on the fly as some blocks are not accessible ingame (armor blocks).
     
  7. Sinbad

    Sinbad Senior Engineer

    Messages:
    2,788
    a disclaimer: I haven't played with inventory, so I don't know much about the interfaces. this is speculation based on scripts I have seen advertised.

    so here is the background:
    we have a text figure on the hud of how much the ship masses. write this down when all the cargo and other inventories are empty. thats the tare weight. store it as public text in a text panel so the pb can get to it.
    we have an overlay that lets us see the centre of mass.
    place a projector and project a blueprint of one armor block. adjust its position so its over the centre of mass with all inventories empty. this is where I'm unsure, but I believe the projector offsets can be read by the PB?
    there is a script that finds the local grid co-ordinates of script accessible blocks. it finds the looation of gyroscopes to calculate orienation. I suggest finding the location of blocks with inventories, then converting the cartesian coordinates to spherical coordinates. also find the location of the projector and add its offsets to its cartesian coordinates before converting them.
    there is a script that can tell you how much of what ores and components you have in inventory. all of these are known of a mass. adjust the distance radius component of each cargo containers spherical coordinates by the mass of the cargo it contains. now its the magnitude of a 3d vector. do the same with the projector coordinates and the tare mass.
    add all the magnitudes together to get the gross vehicle mass, then sum the vectors to get the shift in centre of gravity. a little more math (which I don't have inside my head, thats what I have internets for :p) would get you the moment of angular momentum for that gross mass and com location in all three axis. pick the lowest value and that is the one to use when calcolating how much gyro force to apply. the gross vehicle mass alone should give you a figure for how to adjust thruster overrides.

    massivly overcomplicated and mathematically messy. but could it work?
     
  8. Lynnux

    Lynnux Junior Engineer

    Messages:
    881
    Getting the mass and also the center of the mass should be possible as long as the following interfaces aren't bugged. I just don't know if the inventory will be considered, too. So this is what we have:
    Vector3I IMyCubeGrid.Min: Minimum coordinates of the grid
    Vector3I IMyCubeGrid.Max: Maximum coordinates of the grid
    IMySlimBlock GetCubeBlock(Vector3I pos): gets the cube at the grid Position or null if there's none
    IMySlimBlock.Mass: hopefully with inventory

    Go to every coordinate of the grid
    Get the SlimBlock at a grid coordinate
    Get the mass of the Slimblock
    Add it the the total mass and calculate the center of the mass

    Subgrids are also possible if you have at least one TerminalBlock on each subgrid. Then you can get all connected grids via the GridTerminalSystem and repeat the procedure above. Getting the total mass is easy as you can see.
    But for the center of mass you need the position of the subgrids in relation to your grid. You can use the Grid2World methods from JoeTheDestroyer to do this.

    It's all available, just some work ;)
     
  9. plaYer2k

    plaYer2k Master Engineer

    Messages:
    3,160
    And there is your issue already.
    With the ingame programming you do not get all blocks. You can only access blocks that derive from IMyTerminalBlock. There are however many blocks like armor blocks, windows, blast door blocks etc that are not accessible through the terminal but still contribute to the total mass.
    So that method is merely an approximation but no complete solution, sadly.
     
  10. Lynnux

    Lynnux Junior Engineer

    Messages:
    881
    I tried it yesterday night and even got another issue before:
    The properties X, Y and Z of Vector3I are bugged of course and there's no GetDim() workaround as for e.g. Vector3D. So you don't even get the Max and Min boundaries of the grid.

    Did you try already the GetCubeBlock method ? Doesn't it return SlimBlocks (=also Armor) ?
     
  11. JoeTheDestroyer

    JoeTheDestroyer Junior Engineer

    Messages:
    573
    Cast to Vector3 or Vector3D which do have GetDim().

    Quite a long time ago, GetCubeBlock would return any block. Later it was changed to only return SlimBlocks for blocks that derive from IMyTerminalBlock. We've never been told why the change was made.
     
  12. plaYer2k

    plaYer2k Master Engineer

    Messages:
    3,160
    I didnt know that casting works, good tip.
    This however always worked for me and is more useful due to me using Vector3I in relation with blocks and block orentations.
    Code:
    // VRageMath.Vector3I
    public int AxisValue(Base6Directions.Axis axis)
     
  13. Ronin1973

    Ronin1973 Master Engineer

    Messages:
    4,964
    In Space Engineers Toolbox, you can sort ships by total mass. So there's a way to poll that data. Whether that's available in the PB block is a different story.
     
  14. plaYer2k

    plaYer2k Master Engineer

    Messages:
    3,160
    Correctly, it is an entirely different story.

    SEToolBox accesses the DLLs directly to read the serialized informations in savegames. Thus it has also much more access than the highly sandboxed ingame programming we got available.
    So not, it is not possible to directly read the mass right now.

    The only alternative to "meassure mass" is to apply a constant force over time and see how the mass accelerates. Considering that no moment gets applied due to an off-center force, you can meassure the acceleration and thus determine the mass through f = m * a.
     
  15. Lynnux

    Lynnux Junior Engineer

    Messages:
    881
    Ok, continued with the script and after verification of the result with the SE Toolbox it's confirmed that armor blocks and conveyor blocks are currently not found by IMyCubeGrid.GetCubeBlock(). So let's create a bug report.
    Btw. Vector3 = new Vector3(Vector3I) can also be used to convert the Vector3I, of course.
     
  16. Ronin1973

    Ronin1973 Master Engineer

    Messages:
    4,964
    I don't think it would be much of a security risk to allow programmable blocks to access the ship mass data. It's displayed on the HUD and data that's available for the player to view.
     
  17. Wicorel

    Wicorel Senior Engineer

    Messages:
    1,263
Thread Status:
This last post in this thread was made more than 31 days old.