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

Need help from maths people regarding sine(Solved)

Discussion in 'Scripting' started by prattmyster, Mar 1, 2022.

  1. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    I'm trying to find out the intersection point along 2 transform axis.
    I thought the easiest way of doing this would be using trigonometry, as i have the length of one side of the triangle(the distance between the 2 points). and with the axis i can work out all 3 angles. However i am pretty sure i am using the right equation however the lengths of the other 2 sides are massively off. cna anyone have a look and give me some tips would be much appreciated.
    Code (CSharp):
    1.         public static Vector3 Interception(Vector3 pointA, Transform axisA, Vector3 pointB, Transform axisB)
    2.         {
    3.             float A = 180 - Vector3.Angle(pointB - pointA, axisA.forward);
    4.             float B = 180 - Vector3.Angle(pointA - pointB, axisB.forward);
    5.             float c = Vector3.Distance(pointA, pointB);
    6.             float C = 180 - (A + B);
    7.  
    8.             A = Mathf.Sin(A);
    9.             B = Mathf.Sin(B);
    10.             C = Mathf.Sin(C);
    11.  
    12.             float a = (c * A) / C;
    13.             float b = (c * B) / C;
    14.  
    15.             if (a < 0) a = a * -1;
    16.             if (b < 0) b = b * -1;
    17.  
    18.             Debug.Log(A);
    19.             Debug.Log(C);
    20.  
    21.             Debug.DrawLine(pointA, pointB);
    22.             Debug.DrawLine(pointA, pointA - (axisA.forward * a));
    23.             Debug.DrawLine(pointB, pointB - (axisB.forward * b));
    24.             return Vector3.zero;
    25.         }
    ignore the return at the end just put that in so it does not throw errors at me.

    And solved it i forgot to convert the angles to radians XD been bugging me for days this has.
     

    Attached Files:

    Last edited: Mar 1, 2022
    Kurt-Dekker likes this.
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    Ah yes, I remember the first time I did that, circa 1979 on a TRS-80 Model 1 with Level II BASIC.

    For future finders, in Unity3D you can convert to / from radians to degrees by multiplying by
    Mathf.Rad2Deg
    and
    Mathf.Deg2Rad
    .

    You WILL need this: tons of Quaternion functions are in degrees while the trig math functions are all radians.
     
    prattmyster likes this.
  3. prattmyster

    prattmyster

    Joined:
    Apr 15, 2015
    Posts:
    17
    Yeah I misread the I formation given with the mathf.sin I thought you had to put the degrees in not the radians. Got there in the end thankfully ☺️