Jump to content
• Advertisement

Public Group

# How To Find All Polys Within A Distance To Camera?

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

Hi, I'm an audio developer currently experimenting with audio algorithms in 3D environments (OpenGL). At the moment my "worlds" are very simple, consisting of around 50 polygons. I'd like to see how my algorithms work in more complex worlds (thousands of polygons or more) however I've run into a problem. What I need is a function that returns all polygons with a certain range (say "x") of the user/camera position. One way I thought of doing this was to use a BSP Tree and test each polygon until a point is reached where all polygons are beyond x. As an experiment, I've implemented a 1D BSP Tree (based on the code found in Brackeen's "Developing Games in Java"). I can do a Front-To-Back sort using this tree. However, the sample code sorts the complete list of polygons. What I'd like to do is return the polygons one-at-a-time in nearest-to-furthest order. Something like this: Poly poly = getNextPoly(Poly previousPoly); I imagine that what I really need is a collision test between a circle (defined by radius "x" and centred at the user position) and each polygon. What I don't want to do is test every polygon in the world. What's the best way to do this? Thanks in advance.

#### Share this post

##### Share on other sites
Advertisement
Sort your polygons into a quadtree/octree. Within each zone in the tree, you can depth-sort the child polygons. Then you can easily write a function that returns all zones within X units of your viewpoint, and you can choose which 'reachable' zones get priority, and only operate on the individual polygons within each prioritized zone.

#### Share this post

##### Share on other sites
Since you want to get the length between the camera and the polygon, you could just calculate the distance between the camera and the polygon's center.

dist = sqrt( (( camera.x - polyCenter.x ) * ( camera.x - polyCenter.x )) +
(( camera.y - polyCenter.y ) * ( camera.y - polyCenter.y )) +
(( camera.z - polyCenter.z ) * ( camera.z - polyCenter.z )) );

If you don't already have it, you'll need to calculate the center point of your polygon.

// Average out each vertex
for( int i = 0; i < vertCount; ++i )
{
polyCenter.x += polyPoint->x;
polyCenter.y += polyPoint->y;
polyCenter.z += polyPoint->z;
}

polyCenter.x /= vertCount;
polyCenter.y /= vertCount;
polyCenter.z /= vertCount;

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5
• Advertisement

• 14
• 10
• 23
• 9
• 48
• ### Forum Statistics

• Total Topics
632637
• Total Posts
3007578
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!