# Finding the Distance between 2 Objects

I have 2 objects One Having a Transformation Matrix T1 and other Having Transformation Matrix T2. And We are having a View Matrix V and Projection Matrix P. Basically 2 Object are render using this function

Render(vec3 position,X Rotation , Y Rotation, Z Rotation, Scaling);


So i am having 2 different T1 and T2 transformation matrix.

How should i find the distance between these 2 objects ? Please Explain ? I have tried by simple formula using position vector but it is giving me incorrect distance ?

The distance formula is as follows. v1 and v2 are your two vectors, type vec3. Each one has an x, y, and z property, type double.

vec3 delta = v2 - v1
double distance = math.sqrt(delta.x^2 + delta.y^2 + delta.z^2)


Vector subtraction, as used in the distance formula, can be calculated as follows:

double subX = v2.x - v1.x
double subY = v2.y - v1.y
double subZ = v2.z - v1.z
vec3 result = new vec3(subX, subY, subZ)


You should try to find a library that does vector math and matrix math for you. Most languages have a good one it'll save you lots of time rewrite needless things, that is unless you're doing it to learn the math.

• Absolutely the correct answer for finding the Euclidean distance between two vectors. I could potentially suggest some good math libraries if OP tells us which language they're using. – DisturbedNeo Oct 4 '16 at 15:34
• See also Wikipedia - Pythagorean theorem – TomTsagk Apr 26 '19 at 10:32

I'm no expert in programming, but I know mathematics. From what I understand the distance formula is p1 = x2 - x1 ; p2 = y2 - y1; d= math.sqrt(p1+P2)

I'm not sure where to begin on making z move however. . . .I think that would go into the realm of circumference which is my worst subject currently.

• I have already told you it's not giving me a correct answer – Marvel Oct 4 '16 at 15:18
• @Marvel, to calculate distance betwwen two objects, you need their coordinates in space. They're typically represented by 2 or 3 dimensional vectors. You can't get object coordinates from transformation matrix, because transformation matrix only can tell you how far to move the object, not where it's placed. Final object coordinates received by multiplying current object position by this transformation matrix. – PaulD Oct 4 '16 at 15:25
• Also, provide some additional info. Show the the full code snippet, and what kind of engine or technology do you use? Why do you think distance is wrong? – PaulD Oct 4 '16 at 15:27
• This is not correct. You've omitted the squares inside the expression (ie. it should be d = Math.sqrt(p1*p1 + p2*p2). As for continuing to higher dimensions, you just keep adding more ps (eg. in 3D, p3 = z2 - z1 d = Math.sqrt(p1*p1 + p2*p2 + p3*p3), in 4D p4 = w2 - w1...) - this has nothing to do with circumference, which is the perimeter of a circle. – DMGregory Oct 4 '16 at 17:27