Skip to main content
GameDev.net gamedev.net
🔒 Locked

Objects behind camera?

Started by MindWipe Apr 22, 2003 at 2:30 PM 5 replies 1.1k views
Original Post
MindWipe
MindWipe
Is there a fast way to find out if an object is behind the player? Behind like this: the X:s are behind the camera, and I want to be able to check that(like if the FOV was 180 degrees). I know I can do this by checking the angle to each object, but is there a faster way? I''m just using X and Y, so no 3D thinking needed. /MindWipe
"To some its a six-pack, to me it's a support group."
Jackbunny
Jackbunny
Do a search for viewing frustrum. It has to do with collision detection etc.
"I am Galstaff sorceror of light!""Then how come you had to cast magic missile?"
sanjo
sanjo
better do a search on vector-dot-product.
MindWipe
MindWipe
Ehem... thanks for the ideas. But in that case I do think it''ll be easier to count the degree to the objects and decide from that.

if ( AngleToObject < PlayerAngle - 90'' && AngleToObject > PlayerAngle + 90)
{
ObjectBehindPlayer
}
else
{
OBjectInfrontOfPlayer
}

Or what!?

"To some its a six-pack, to me it's a support group."
S1CA
S1CA
quote:
Original post by MindWipe
Ehem... thanks for the ideas. But in that case I do think it''ll be easier to count the degree to the objects and decide from that.


That''s what a dot product will get you, but without any of the trig you''d end up using to get the angles.


1) Get a normalised vector for the direction the camera is looking in. With most lookAt matrix creation functions this value is usually shoved directly into the matrix so you can extract it from there or calculate once per frame. Call this "view_direction".

2) Get the point in the world where the camera is located in world space, call this "camera_pos".

3) Get the point where the object is located in world space, call this "object_pos".



And the code:

// get vector from camera to object
vec3 u = object_pos - camera_pos;
//
// get the projection of the object onto the view direction
float t = vec3DotProduct( u, view_direction );
//
// if t>0, the object is in front of the camera
// if t<0, the object is behind the camera



vec3 is a simple 3d vector data type. The "operator-" for vec3 does a componentwise subtract (va.x - vb.x, va.y - vb.y, va.z - vb.z). The vec3DotProduct is res=va.x*vb.x+va.y*vb.y+va.z*vb.z.

If you need to test a sphere instead of a single point against the plane, you can do:

if ((t-radius) > 0): all sphere is in front
if ((t+radius) > 0): some or all of sphere is in front
if ((t+radius) < 0): all sphere is behind





--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com
Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site
LordDeath
LordDeath
Dot-product is the way to go. You don''t need to normalize the vectors if you only care about the sign of the result. (Meaning if you only care about being in front of or behind, and not how far in front of or behind.)

// Stuff you already know
vector camera_forward_dir;
vector some_obj_pos;
vector camera_pos;

// Stuff you need to calculate
vector camera_to_object_dir = some_obj_pos - camera_pos;
float dot_product = DotProduct(camera_to_object_dir, camera_forward_dir);

// > 0.0f means object in front of the camera
// < 0.0f means object behind the camera
// 0.0f means object planar with the camera
MindWipe
MindWipe
S1CA> Thanks! I''ll do that. Allways nice getting advice from an expert like you.

LordDeath> Thx to you too!

/MindWipe

"To some its a six-pack, to me it's a support group."

Topic Locked

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

Sign in to reply to this topic.