Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

a Math problem (trigonometry)

Discussion in 'Editor & General Support' started by Dabartos, Aug 12, 2018.

  1. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    Hello,
    I do not know where to post math questions so excuse me if this is the wrong sub.
    can anyone give me a quick lesson on how to calculate the point in the picture?

    Much appreciated.
    (The purple arrow is the transform.forward vector)

    mathProblem.png
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Let me make sure I'm understanding this diagram correctly. Are you trying to get the point at which the forward of the transform intersects with the ground plane?

    If so, then you can just raycast against a Plane:

    Code (CSharp):
    1. var plane = new Plane( Vector3.up, Vector3.zero );
    2.  
    3. var ray = new Ray( transform.position, transform.forward );
    4.  
    5. float d;
    6. plane.Raycast( ray, out d );
    7.  
    8. var pointOnPlane = ray.GetPoint( d );
     
  3. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    I am hoping to get a view on how would it be achieved using only trigonometric functions instead of using planes and rays.

    Thanks anyway
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    https://docs.unity3d.com/ScriptReference/Transform.TransformDirection.html

    Use
    TransformDirection()
    . Something like this:

    Code (csharp):
    1. float distance = 3.5f;
    2. Vector3 forwardWorldPos = Camera.transform.TransformDirection(transform.forward * distance);
    That'll project a local vector out by x distance then let TransformDirection() convert it to a world position.

    *edit*
    sorry didn't fully understand your question because the picture wasn't totally clear to me where the purple thing was pointing.

    Here's a link that explains how to solve for these types of things.
     
    Last edited: Aug 13, 2018
  5. Dabartos

    Dabartos

    Joined:
    May 26, 2016
    Posts:
    33
    these are really good and I believe someone will find them useful but I am really interested only in the math part of the problem, I have the implementation covered.

    Thanks for the link to the math site LaneFox, I'll check it out :)
     
  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    There is not enough information for a trigonometric interpretation. There is no way to determine any angles from the picture, as was previously mentioned. It could be pointing anywhere.
     
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    There's certainly enough information to determine a trigonometric solution. For this purpose I am going to use my own labeled version of the diagram for clarity:



    First we need to find theta. You can do this since you know the normalized direction of AC (let's call this normal N), as well as the vector AB.

    Theta is calculated as
    acos( AB · N )
    (the dot signifies a dot product).

    Now you need to find the length of AC. Now that you have theta this can be done simply using
    cos( θ ) * len( AB )
    .

    Finally, multiply N by the length of AC, and add the resulting vector to point A to get C.


    For the record, raycasting against a plane is also a trigonometric solution to the problem. When exploded out of methods and into pure math, it looks like this:


    Where P is the plane's normal, D is the ray direction (in this case the transform forward) and O is the ray origin (in this case the transform position).

    You find the distance at which the ray intersects the plane by taking
    v = D · P
    and
    n = O · P
    . The distance equals
    -n / v
    .
     
    Last edited: Aug 14, 2018
  8. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    How do you know the normalized direction of AC? You don't know the coordinates of C (?, 0, ?)
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    As stated in the original post, we have the transform.forward of the object, which gives us the direction but not the length.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Agreed, but the OP requested that we use trigonometry only, assuming based only on the diagram and the statement "I am really interested only in the math part". Yes you can get the angles from Unity, but not from the diagram. Semantics at this point, sounds like we are in agreement.
     
    Madgvox likes this.