How to determine if a position is in front of another?
Hello,
Let me explain:
I want a player to be able to pick up items, but only those which are ''visible'' to the player.
By visible I mean in front of the player (in an 180 degree or so slice of area from his left->front->right).
I have the player''s forward vector, but he can obviously be rotated or face any angle.
So, given the player position(x,y,z), forward vector(a,b,c) and an object position (ox,oy,oz), how can you determine whether the object position is in that 180 degree area, and therefore ''visible'' to the player?
Thanks for any help with this!
Mark
This actually isn''t too difficult, since you''re checking a 180 degree span. If you create a vector V which goes from the player''s position to the object''s position (basically Vobject - Vplayer), and take the dot product between this vector and the player''s forward vector, your result will be >= 0 if the object is within the front 180 degrees (what you want), and < 0 if it''s behind the player. 0 is really a boundary value, where the object is right on the edge.
Zipster is right.
My way of seing it:
First you move the objects position to be relative to the player.
Now, the players forward vector is a normal to a plane that goes through the origin (plane = n*{x,y,z} + d, where n is forward vector and d = 0 (origin is in the plane)). Inserting the new object pos in the plane by doing the dot product gets the (shortest) signed distance from the plane to the object. So if the distance < 0, the object is behind the plane and also behind the player. If the distance > 0, the object is in front.
Ah, maybe this is just confusing...
My way of seing it:
First you move the objects position to be relative to the player.
Now, the players forward vector is a normal to a plane that goes through the origin (plane = n*{x,y,z} + d, where n is forward vector and d = 0 (origin is in the plane)). Inserting the new object pos in the plane by doing the dot product gets the (shortest) signed distance from the plane to the object. So if the distance < 0, the object is behind the plane and also behind the player. If the distance > 0, the object is in front.
Ah, maybe this is just confusing...
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement