Page 1 of 1

How do I check if the player "sees" an object?

Posted: Thu Nov 29, 2012 9:43 pm
by penguin321
I know its hard to understand what I am looking for but its kind of hard to explain...

I have a value that is the players angle of view,
And an Field Of View value.
And an object with x,y values.

How can I check if the object can be 'seen' by the player using the FOV?

If you cant understand I will include more detail and pictures if its necessary.

Re: How do I check if the player "sees" an object?

Posted: Thu Nov 29, 2012 9:44 pm
by Nixola
Do you want to check the distance between the player and the object or to check wether there's something between them that blocks the view?

Re: How do I check if the player "sees" an object?

Posted: Fri Nov 30, 2012 3:57 am
by Knaimhe
So is this like a bird's eye view kind of thing where the player sees a 2d arc in front of him?
Seems like it.

First, you'd want to check if the player is close enough to see the object. Use the Pythagorean theorem with playerBody:getPosition() and objectBody:getPosition().
Then, you'd want to record player angle. If you're using Box2D, you can say playerBody:getAngle(), which returns the angle in radians.
Then, you find the angle between the player and the object. This should be something like math.atan(deltaY/deltaX). That sounds right, but I'm not too sure. deltaX and deltaY represent the difference in position between the player and the object in both the horizontal and vertical axes (rise/run).
Finally, you'd want to check if the angle from the player to the object matches the angle of the player's vision. You'd probably want to give it some leniency in either direction to account for an actual field (arc) of view.

Note that you might end up with the angle from the object to the player instead of the other way around. Simple add math.pi (180 degrees).

If, however, you want to account for opaque obstacles between the player and the object... well I'd recommend raycasting/shadowcasting.