# Get distance between to parallel planes

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).

current setup in unity

Code for current illustration in unity

``````    private void CheckDistanceBetweenPlanes(float offsetX, float offsetY)
{

float3 point1 = new float3(0, 0, 0);
float3 point2 = new float3(0, 0, 0);

float3 normal1 = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
float3 normal2 = new float3(0, 0, 0);

float3 targetPos = new float3(testObject1.transform.position.x, testObject1.transform.position.y, testObject1.transform.position.z);
float3 origin = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);

float3 offsetPosition = new float3(originGO.transform.position.x, originGO.transform.position.y, originGO.transform.position.z);
float3 offsetPosition2 = new float3(testObject2.transform.position.x, testObject2.transform.position.y, testObject2.transform.position.z);

float degreesToRad = math.PI / 180f;

float3 newOriginDir = math.normalize(normal1);

offsetPosition = offsetPosition - newOriginDir;
offsetPosition2 = offsetPosition2 - newOriginDir;

float3 position1 = newOriginDir;
float3 position2 = position1 - (math.cross(position1, math.forward(rotation1)));
float3 position3 = position2 - (math.cross(position1, math.forward(rotation2)));
float3 position4 = position1 - (math.cross(position1, math.forward(rotation2)));

position1 = position1 + offsetPosition;
position2 = position2 + offsetPosition;
position3 = position3 + offsetPosition;
position4 = position4 + offsetPosition;

float3 positionSecound1 = newOriginDir;
float3 positionSecound2 = positionSecound1 - (math.cross(positionSecound1, math.forward(rotation1)));
float3 positionSecound3 = positionSecound2 - (math.cross(positionSecound1, math.forward(rotation2)));
float3 positionSecound4 = positionSecound1 - (math.cross(positionSecound1, math.forward(rotation2)));

positionSecound1 = positionSecound1 + offsetPosition2;
positionSecound2 = positionSecound2 + offsetPosition2;
positionSecound3 = positionSecound3 + offsetPosition2;
positionSecound4 = positionSecound4 + offsetPosition2;

CrossPositionDebug(positionSecound1);
}
``````

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`)

``````float3 v = point2 - point1;
float distance = math.abs(math.dot(v, normal) / math.dot(normal, normal));
``````
2 Likes

Thanks that solved it.

1 Like