Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would I calculate this 2 formulas in c#?

Discussion in 'Scripting' started by Stef_Morojna, Oct 9, 2016.

  1. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    So this are the 2 formulas that i'm having problems with, because well apparently I can't multiply / divide vectors in C#

    r = position
    v = velocity
    h = specific angular momentum
    e = eccentricity



    Does anyone know of a solution?
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    In Vector multipliction you have 2 possibilities:
    Vector A * Vector B this is known as the dot product and returns a Scalar (just a number):
    https://www.mathsisfun.com/algebra/vectors-dot-product.html

    Unity Code:
    Code (CSharp):
    1. Vector3 vectorA;
    2. Vector3 vectorB;
    3.  
    4. float scalar = Vector3.Dot(vectorA,vectorB);
    Second Possibility is:
    VectorA x VectorB this is known as the cross product and returns a Vector:
    https://www.mathsisfun.com/algebra/vectors-cross-product.html

    Unity Code:
    Code (CSharp):
    1. Vector3 vectorA;
    2. Vector3 vectorB;
    3.  
    4. Vector3 vectorC = Vector3.Cross(vectorA,vectorB);
    Your equations look like you should be taking the Cross Product. Where if you were using the standard C# symbol for multiplication your were taking the Dot product.

    EDIT: One more thing the I bet you the last term of the 2nd equation:
    Vector r / r
    Is vector being divided by its magnitude, normalizing the vector to unit length. Unity has this built in:
    Code (CSharp):
    1. Vector3 vectorPosition;
    2.  
    3. vectorPosition = vectorPosition.normalized;
     
    Last edited: Oct 9, 2016
  3. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Thank you very much! Have a nice day :)