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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
penguin321
Prole
Posts: 7
Joined: Sun Oct 21, 2012 6:14 pm

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

Post 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.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

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

Post 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?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Knaimhe
Prole
Posts: 34
Joined: Mon Jan 23, 2012 7:23 pm

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Nikki and 4 guests