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 ?


2 Answers 2


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.

  • \$\begingroup\$ 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. \$\endgroup\$ Oct 4, 2016 at 15:34
  • \$\begingroup\$ See also Wikipedia - Pythagorean theorem \$\endgroup\$ Apr 26, 2019 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.

  • \$\begingroup\$ I have already told you it's not giving me a correct answer \$\endgroup\$
    – Marvel
    Oct 4, 2016 at 15:18
  • \$\begingroup\$ @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. \$\endgroup\$
    – PaulD
    Oct 4, 2016 at 15:25
  • \$\begingroup\$ 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? \$\endgroup\$
    – PaulD
    Oct 4, 2016 at 15:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Oct 4, 2016 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .