#### Archived

This topic is now archived and is closed to further replies.

# camera to mesh distance

This topic is 5905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can I get the distance between camera and a mesh. I know mesh position, camera position, camera lookat, matrices... Thanks

##### Share on other sites
All I have is an old visual basic example, but it should be very easy to convert. Note that the square operation is one of the more processor-intensive mathematical calculations, and I know there is another function that doesnt use it, unless you find that one you should consider only using this function every 5 frames or something like that to reduce the use of this function.

Private Function GetDistance(X1 As Single, Y1 As Single, X2 As Single, Y2 As Single) As Single
GetDistance = Sqr((X1 - X2) ^ 2 + (Y1 - Y2) ^ 2)
End Function

##### Share on other sites
Doesn''t anybody know about the second one??

##### Share on other sites

If you are working in 3D space (as it would appear) you also need to include the z-components in the above formula...

  float xSquared = (meshX - cameraX) * (meshX - cameraX);float ySquared = (meshY - cameraY) * (meshY - cameraY);float zSquared = (meshZ - cameraZ) * (meshZ - cameraZ);float distance = sqrt(xSquared + ySquared + zSquared);

As for ''other'' methods of calculating the square root you will find many documents and source code snippets outlining various methods simply by performing a search for ''fast square root programming'' (or similar) with your favourite search engine.

Regards,
Sharky