Skip to main content
gamedev.net
🔒 Locked

Distance

Started by Blue*Omega Sep 16, 2000 at 4:57 PM 2 replies 450+ views
Original Post
Blue*Omega
Blue*Omega
Two Questions, one simple, one hard. 1) What''s the equation to find the distance between two 3d points? (Yes, I should know it but don''t, so sue me) 2) Is there and easy way to find if the object is out of vieing range? (Off the side of the screen or behind the camera?) Thnx. ----------------------------- Blue*Omega (Insert Witty Quote Here)
Strylinys
Strylinys
First...

distance = sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)))

Or, the square root of the sum of the squares of the sides. In a 3 dimensional kinda way. It''s pythagorus just with three terms to add under the square root.

I don''t know the answer to the second question tho.

S.
Chazz
Chazz
ah ha!
-Chazz
Strylinys
Strylinys
For the second question... (this might be a little CPU intesive... I haven''t tried it)

Figure out the projection for all objects within the viewport.

To do this, simply find the width, height, and depth of the viewport somewhere in your code... width and height are in the glViewport line and the depth is in gluPerspective.

Then... Take all your objects, run them through a loop to see if they''re in the viewport. This will give you all objects that are on the screen. Not sure if this will accomplish what you want. If it doesn''t, then we can go one step further.

Figure out the scalar projections of your objects.

    
// find the magnitude of a vector

GLfloat FindMag(GLfloat v[])
{
return (GLfloat)sqrt((v[0] * v[0]) + (v[1] * v[1]) + (v[2] * v[2]));
}
// find the dot product of 2 vectors

GLfloat DotProd(GLfloat v1[], GLfloat v2[])
{
return ((v1[0] * v2[0]) + (v1[1] * v2[1]) + (v1[2] * v2[2]));
}
// find the projection of a vector on another

GLfloat VectorProj(GLfloat v1[], GLfloat v2[])
{
return (DotProd(v1, v2) / FindMag[v2]);
}
// x axis and y axis vectors...

GLfloat xAxis[3] = {1, 0, 0};
GLfloat yAxis[3] = {0, 1, 0};
// loop through all of your vectors in the viewport with this inside the loop...

// check the vector against max x and min x

if (VectorProj(CurrentVector, xAxis) < (ViewportWidth / 2 + xTranslation) || VectorProj(CurrentVector, xAxis) > (ViewportWidth / 2 + xTranslation))
{
ignore vector
}
... do the same for that vector with the yAxis, ViewportHeight and yTranslation


I think that should work.

S.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.