Search Unity

Resolved Get distance between to parallel planes

Discussion in 'Entity Component System' started by FederalRazer89, Sep 28, 2022.

  1. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Hello

    I have a sort of math problem which I need to solve be able to progress, and it would be more helpful for me to understand more about interacting with quaternion.

    The problem is that I need to check the distance between two planes, which have the same normal/direction and is parallel to each other. In theory I can solve this on paper with Pythagoras, but every attempt have yielded the wrong results. I have a naïve way which I could solve this by testing distance on the one of the planes until I find the lowest value between the planes. But I there should be a way to approach this with math, which is more performant.

    in the picture bellow i want to get the distance which is illustrated in green. If it was 2D i could use Pythagoras, but 3d complicates things.
    If i understand this i will have more tools for approaching geometric problems which would greatly help me.

    illustration in blender (green is the desirable distance).
    upload_2022-9-28_11-33-18.png

    current setup in unity
    upload_2022-9-28_11-37-58.png



    Code (CSharp):
    1.  
    2.     private void CheckDistanceBetweenPlanes(float offsetX, float offsetY)
    3.     {
    4.  
    5.         float3 point1 = new float3(0, 0, 0);
    6.         float3 point2 = new float3(0, 0, 0);
    7.  
    8.         float3 normal1 = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
    9.         float3 normal2 = new float3(0, 0, 0);
    10.  
    11.         float3 targetPos = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
    12.         float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);
    13.  
    14.         float3 offsetPosition = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);
    15.         float3 offsetPosition2 = new float3(testObject2.transform.position.x, testObject2.transform.position.y, testObject2.transform.position.z);
    16.  
    17.         float degreesToRad = math.PI / 180f;
    18.  
    19.         float quadAngleRad1 = degreesToRad * angle1;
    20.         float quadAngleRad2 = degreesToRad * (270 + angle1);
    21.  
    22.         float3 newOriginDir = math.normalize(normal1);
    23.         quaternion rotation1 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad1);
    24.         quaternion rotation2 = quaternion.AxisAngle(math.normalize(newOriginDir), quadAngleRad2);
    25.  
    26.         offsetPosition = offsetPosition - newOriginDir;
    27.         offsetPosition2 = offsetPosition2 - newOriginDir;
    28.  
    29.  
    30.         //First Quadrant test
    31.         float3 position1 = newOriginDir;
    32.         float3 position2 = position1 - (math.cross(position1, math.forward(rotation1)));
    33.         float3 position3 = position2 - (math.cross(position1, math.forward(rotation2)));
    34.         float3 position4 = position1 - (math.cross(position1, math.forward(rotation2)));
    35.  
    36.         position1 = position1 + offsetPosition;
    37.         position2 = position2 + offsetPosition;
    38.         position3 = position3 + offsetPosition;
    39.         position4 = position4 + offsetPosition;
    40.  
    41.         QuadDebugLines(position1, position2, position3, position4);
    42.  
    43.  
    44.         //Secound Quadrant test
    45.         float3 positionSecound1 = newOriginDir;
    46.         float3 positionSecound2 = positionSecound1 - (math.cross(positionSecound1, math.forward(rotation1)));
    47.         float3 positionSecound3 = positionSecound2 - (math.cross(positionSecound1, math.forward(rotation2)));
    48.         float3 positionSecound4 = positionSecound1 - (math.cross(positionSecound1, math.forward(rotation2)));
    49.  
    50.         positionSecound1 = positionSecound1 + offsetPosition2;
    51.         positionSecound2 = positionSecound2 + offsetPosition2;
    52.         positionSecound3 = positionSecound3 + offsetPosition2;
    53.         positionSecound4 = positionSecound4 + offsetPosition2;
    54.  
    55.         QuadDebugLines(positionSecound1, positionSecound2, positionSecound3, positionSecound4);
    56.  
    57.         CrossPositionDebug(positionSecound1);
    58.     }
    59.  
     
  2. vertxxyz

    vertxxyz

    Joined:
    Oct 29, 2014
    Posts:
    109
    If you know the planes are parallel then the distance is just the length of the projection of the vector made by any two points on the planes, onto the planes' normal vector.
    (ie. the scalar portion of
    math.project
    )

    Code (CSharp):
    1. float3 v = point2 - point1;
    2. float distance = math.abs(math.dot(v, normal) / math.dot(normal, normal));
     
    FederalRazer89 and Antypodish like this.
  3. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Thanks that solved it.
     
    vertxxyz likes this.