# determine if object is in view

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

## Recommended Posts

Greetings, I'm looking for a quick (can be crude) way to determine if an object (a point in space) is in my current view projection. I'm sure there's some nifty process involving the current projection matrix, but I'm not aware of it. In other words, I don't want to render an object if it's behind the camera. Any help is greatly appreciated.

##### Share on other sites
Use a frustum (6 planes surrounding your view frustum) and test them against spheres or axis aligned boxes surrounding your objects.

##### Share on other sites
I wrote a Wolf3D style game years ago that had a 90 degree view (-45 to +45). After translating all position of the objects, I first made sure that their position on the Z-axis was greater than my clipping point. Then, to see if they were on-screen, I simply checked that their X position was less than their Z position and greater than the negative of their Z position. I allowed a little more slack than that so objects could be partially off-screen but that's the jist of it.

Hey, you wanted crude =b

##### Share on other sites
Jan-Lieuwe, is there a simple way of creating the 6 sides of my frustum? Would I have to create the frustum (6 quads), and then apply the current MODELVIEW_MATRIX to these quads, then test against this translated frustum, or is there a simpler way?

##### Share on other sites
Quote:
 Original post by slikrikJan-Lieuwe, is there a simple way of creating the 6 sides of my frustum? Would I have to create the frustum (6 quads), and then apply the current MODELVIEW_MATRIX to these quads, then test against this translated frustum, or is there a simpler way?
This article describes a straightforward method for extracting the frustum planes from the modelview and projection matrices. (It does require a little matrix math though...)

##### Share on other sites
If you just want to know wether an object is behind your camera, just one plane of the frustum will do (the near clipping plane).

##### Share on other sites
If you are really talking about points (or don't mind it being a bit crude), then it's usually enough to know the width/height of your frustum for some z (usually you should know it for the near plane, as that's what you set the projection up with.. unless using some helper function).

Then it comes down to transforming the point to camera space (can be done coordinate by coordinate), compare z to the near/far plane and x (y) to width (height)*z. Or do it with the six planes, as you will probably find countless tutorials about that ,-)

##### Share on other sites
Trienco:

To use your method, wouldn't I need to transform the point in question by the *inverse* of the projection/modelview matrix to obtain the Z coordinate relative to the camera?

##### Share on other sites
If you have front, right and up for your camera, you can dot the vector from camera to object center with those three. If depth is negative (more than radius) your object is behind camera. Else take the absolute of right and up dot products; subtract objet radius; divide each by depth dot product; if this projects less than your field of view, the object is likely visible. The "field of view" is the atan value for your sideways and upways half-viewing-angles.

This is generally quicker than doing the six planes test (assuming you pre-calculate your atan and camera values once each frame), and about as accurate.

##### Share on other sites
hplus0603: could you please clarify the steps you're referring to? By "front, right and up" do you mean "near, right, and top" planes? You then refer to "depth" but I'm not sure how you're calculating depth. Your "else" portion then doesn't make sense-- I'm sorry if I am missing something obvious. I can't tell exactly what you're referring to with the right and top dot products...

Thanks!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634779
• Total Posts
3019241
×