Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Finding Local Coordinates - inverted x and z

Discussion in 'Programming Questions and Suggestions' started by Raikoh, Jan 21, 2017.

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

    Messages:
    4
    Hey guys. I am writing a script to face a ship towards a gps location.
    However the gps location relative to the cockpit's position on the local (ship) coordinate system seems to have the right magnitude but the x and z have the wrong sign (e.g. showing negative when it should be positive).

    Here's the relevant section of the code:
    Code:
    double x, y, z, x1, y1, z1;
    Vector4D D_T;
    
    public void Main() {
        IMyCockpit cockpit = GridTerminalSystem.GetBlockWithName("Cockpit") as IMyCockpit;
        IMyTextPanel panel = GridTerminalSystem.GetBlockWithName("Reference LCD") as IMyTextPanel;
        panel.WritePublicText("" , false);  //Wipe LCD
    
    
    //---Current Orientation and Position---//
        x1 = 0;  y1 = 0;  z1 = 0; //gps location
        x = cockpit.WorldMatrix.Translation.GetDim(0);
        y = cockpit.WorldMatrix.Translation.GetDim(1);
        z = cockpit.WorldMatrix.Translation.GetDim(2);
        panel.WritePublicText( "Cockpit World xyz:\n" + x + "\n" + y + "\n" + z + "\n" , true);
        panel.WritePublicText( "Target World xyz:\n" + x1 + "\n" + y1 + "\n" + z1 + "\n" , true);
    
    //---Transforming target world coordinates to local coordinates ----//
        D_T.X = x1;
        D_T.Y = y1;
        D_T.Z = z1;
        D_T.W = 1;
        D_T = Vector4D.Transform(D_T, Matrix.Invert(cockpit.WorldMatrix));
        panel.WritePublicText( "Transformed xyz:\n" + D_T.X + "\n" + D_T.Y + "\n" + D_T.Z + "\n", true);
    }
    [​IMG]
    The picture shows a screenshot taken inside the cockpit, the target is in front, to the left and below of the crosshair. The LCD gives the output from the script.
    From my understanding, the transformed z-value represents the forward distance from the cockpit to the target but I am getting negative 16, the transformed x-value should have a positive value when the target is to the left. Only the Y-value makes sense to me since the target is below the crosshair.
    This is my first time scripting and writing in C# so any other coding tips and tricks is welcomed.
     
    Last edited: Jan 22, 2017
  2. Sinbad Senior Engineer

    Messages:
    2,788
    i think the output is cartesian, as in XYZ axis of your ship. not a polar system with X azimuth, Y elevation and Z range.
    this
    http://steamcommunity.com/sharedfiles/filedetails/?id=569092217
    will give you all polar outputs, which i think make more sense.
    im not sure if it still works, but it should. there are newer ways of doing it than what this script does, but math is math even if its hidden behind a method in the api.
     
  3. Raikoh Trainee Engineer

    Messages:
    4
    I think working in Cartesian is fine?
    I didn't show the next section, but after I find the XYZ of the target relative to the cockpit. I dot product the vector with cockpit.WorldMatrix.Forward and use Acos the result to find the required angle adjustment. I also cross product the two vectors to work out which direction I should adjust.

    I had a brief look at your script, it seems like our methods are similar.
     
    Last edited: Jan 22, 2017
    • Informative Informative x 1
  4. Sinbad Senior Engineer

    Messages:
    2,788
    They most likely are, there are only so many ways you can splash math on the same set of numbers and still get sense out ;-)
     
  5. albmar Trainee Engineer

    Messages:
    69
    Have a look at the source code of Vector3D:
    Code:
    public struct Vector3D : IEquatable<Vector3D>
    {
    ...
    public static Vector3D Up = new Vector3D(0.0, 1, 0.0);
    public static Vector3D Down = new Vector3D(0.0, -1, 0.0);
    public static Vector3D Right = new Vector3D(1, 0.0, 0.0);
    public static Vector3D Left = new Vector3D(-1, 0.0, 0.0);
    public static Vector3D Forward = new Vector3D(0.0, 0.0, -1);
    public static Vector3D Backward = new Vector3D(0.0, 0.0, 1);
    ...
    }
    
    This shows you that X is right, Y is up and Z is backward.
     
  6. Raikoh Trainee Engineer

    Messages:
    4
    Ah thanks that explains a lot. Is there a reason why Z is positive for backward? Does it make the maths easier or something?
     
  7. albmar Trainee Engineer

    Messages:
    69
    There is no reason that this way math will be easier. So how you define it does not really matter, but what matters is that one will remain to that, otherwise you will go crazy :eek:ops:. In my experience Keen keeps to their definition (except the pitch, roll and yaw methods of the gyroscope).
    The only thing, I can think of, that can change the behavior is the order of directions. Since x, y and z define the order of a vector, this will effect the direction of rotations. If you take your right hand, your first 3 fingers beginning from the thumb will be the coordinate axis x, y and z. Let the first 2 point in the given directions and orient your fingers perpendicular to each other. You will see that the third one will correctly point backwards. This meas this coordinate system is right handed. If you try the same with your left hand, you'll see that the third will point forwards. As a rule of thumb :tu: if you want to rotate a positive value around the z-axis, this will always rotate from the x-axis to the y-axis, no matter what kind of coordinate system. As you can see our right handed coordinate system will rotate from right to up, which means it is rotating counter-clockwise.
     
  8. Ronin1973 Master Engineer

    Messages:
    4,955
    Your cockpit isn't necessarily aligned with direction of your grid. It's possible when making the grid that the grid's relative orientation is different from the cockpit. The grid will always behave in the orientation of the cockpit/flight seat when it is occupied by the player... but that doesn't change the orientation of the grid itself.
     
  9. Raikoh Trainee Engineer

    Messages:
    4
    I don't really understand what grid direction is but I am only using and concerned with the cockpit's alignment relative to the world's coordinate system.

    Yep I know about the right hand rule, but I just intuitively assumed z positive would be forward and so x must be positive to the left. Thanks again for clearing that up for me, I was able to finished my script.
     
  10. Ronin1973 Master Engineer

    Messages:
    4,955
    Every grid references its own internal structure in X, Y, Z. You have a central block at (o,o,o). Then each additional block is attached by the coordinate system (1,0,0), (-1,0,0) etc. So you can poll a grid for blocks at a certain place OR a block can tell you its coordinates within that relative grid.

    When you build the grid in the game, the game determines for itself which ways the X, Y, and Z coordinates run. So while you visually can tell where on the axis the front of your ship is, it may not necessarily be stored within the game with the same axis. When you install a cockpit and place a player in the cockpit, the gyroscopic and thruster controls align with that cockpit when input is applied.

    I'm not sure if what I'm explaining is relative to your query, but it's something to consider when dealing with a grid.
     
Thread Status:
This last post in this thread was made more than 31 days old.