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

[Guide] Quick and dirty Introduction to Vector Math

Discussion in 'Programming Guides and Tools' started by Immersive, Feb 2, 2015.

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

    Immersive Apprentice Engineer

    Messages:
    122
    A Vector, in mathematical terms, is a multi-dimensional 'step.' That is, it is a step (or, difference) in multiple directions at once. As far as SE (and physical reality) is concerned there are 3 Dimensions, typically expressed as X, Y and Z co-ordinates.

    A Vector, in programming terms, is a construct that can hold the X, Y and Z measurements. There are two ways to use these measurements.
    1. Positional: Measures offset from some other position
    2. Directional: Measures a unit direction
    This can be a bit confusing at first. The key point to remember is that when using a Vector as Directional, it must have a length as close to 1 as possible. This is called Normalising, and is easy to do
    Code:
     var vecUnitLength = Vector3.Normalize( vecTotalDisplacement ); 
    
    Some standard Space Engineers Directional vectors:
    Code:
    [FONT= &#39]var Forward = new Vector3( 0, 0,-1);[/FONT]
    [FONT= &#39]var Back    = new Vector3( 0, 0, 1);[/FONT]
    [FONT= &#39]var Left    = new Vector3(-1, 0, 0);[/FONT]
    [FONT= &#39]var Right   = new Vector3( 1, 0, 0);[/FONT]
    [FONT= &#39]var Up      = new Vector3( 0, 1, 0);[/FONT]
    [FONT= &#39]var Down    = new Vector3( 0,-1, 0);[/FONT]
    
    Get the components using
    Code:
     vec.GetDim(n); // n: 0 for X, 1 for Y and 2 for Z 
    Use string formatting when viewing
    Code:
     vec.GetDim(n).ToString("N2"); // Number, two decimal places
    To get a displacement to vecA from vecB
    Code:
     var vecBA = vecA - vecB; 
    To get a direction to vecA from vecB
    Code:
     var vecDirBA = Vector3.Normalize( vecA - vecB ); 
    To get an angle requires two Vectors
    Code:
     var angle = Math.Acos( vec1.Dot( vec2 ) ); 
    To get the axis of rotation
    Code:
     var axis = vec1.Cross( vec2 ); 
    To rotate, or Transform, a Vector between reference frames (or, co-ordinate spaces) we require a Transformation Matrix.
    To create a Matrix for a rotation
    Code:
     var matTransform = Matrix.CreateFromAxisAngle( axis, angle ); 
    To perform the vector transform
    Code:
     var vecTransformed = Vector3.Transform( vecOriginal, matTransform );
     
  2. happyjack27

    happyjack27 Apprentice Engineer

    Messages:
    452
    Code:
    var angle = Math.Acos( vec1.Dot( vec2 ) ); 
    
    assumes vec1 and vec2 are normalized.

    more generally it would be:

    Code:
    var angle = Math.Acos( vec1.Normalize().Dot( vec2.Normalize() ) ); 
    
    or alternatively:

    Code:
    var angle = Math.Acos( vec1.Dot( vec2 )  / (vec1.Length()*vec2.Length())); 
    
     
    Last edited by a moderator: Feb 3, 2015
Thread Status:
This last post in this thread was made more than 31 days old.