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


camera to mesh distance

Recommended Posts

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 this post

Link to post
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.


Share this post

Link to post
Share on other sites