Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Get x and y coordinates of point in plane

Discussion in 'Scripting' started by ricardoCoelho, Sep 4, 2020.

  1. ricardoCoelho

    ricardoCoelho

    Joined:
    Jul 6, 2020
    Posts:
    18
    Hello guys, I need some help with a math/logic problem.

    I have a plane defined by its center (world coordinates), forward and up direction, and a point in a random location on that plane. I'd like to calculate the 'local' X and Y coordinates of that point relative to the plane center.

    This is something that will run multiple times per frame so I cannot do something like instantiate a gameobject plane and a gameobject cube in theyr respective positions, rotate the gameobject plane to match the imaginary plane direction, make the cube a child of the plane and extract the X and Y values, and destroy these 2 gameobjects.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,777
    You could have a single reference GameObject (no need to create and destroy new ones, nor to create child objects), and move that Transform around to match your plane, and use .InverseTransformPoint. Something like:
    Code (csharp):
    1. private static Transform referenceObject;
    2. public static Vector2 GetLocalXAndYRelativeToPlane(Vector3 planeCenter, Vector3 forward, Vector3 up, Vector3 worldPoint)  {
    3.    if (referenceObject == null) referenceObject = new GameObject("Reference").transform;
    4.  
    5.    referenceObject.position = planeCenter;
    6.    referenceObject.rotation = LookRotation(forward, up);
    7.    Vector3 rtn = referenceObject.InverseTransformPoint(worldPoint);
    8.    return rtn;
    9. }
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,216
    This is just plain linear algebra. So given a 3d point that lies inside our plane, we can simply subtract the plane origin from that point to get a relative vector from our origin to that point in the plane. Now you just have to calculate the dot product between each of your basis vectors and your relative direction vector to get the coordinates within that plane. Of course the basis vectors should to be normalized but it's actually not required since they actually form a 2d coordinate space on their own.

    So here's an example:


    Code (CSharp):
    1. // given information
    2. Vector3 origin;
    3. Vector3 planeXDir;
    4. Vector3 planeYDir;
    5. Vector3 pointOnPlane;
    6.  
    7. Vector3 v = pointOnPlane - origin;
    8. Vector2 positionInPlane = new Vector2(Vector3.Dot(planeXDir, v), Vector3.Dot(planeYDir, v));
    9.  
    That's exactly what a 2x3 projection matrix would do. I would highly recommend to watch the Essence of linear algebra series by 3b1b. If you are not familiar with linear algebra I would recommend to start the series at the beginning. However the important part relevant here is chapter 9 about the dot product and duality.