Line of sight - The Best Way

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
mufty
Prole
Posts: 10
Joined: Fri Apr 09, 2010 5:11 pm

Line of sight - The Best Way

Post by mufty »

Hi all

I'm trying to make a perfect simple and non memory consuming Line of sight in a 2D space without affecting by any other object only A point and B point.

I've been thinking about drawing a line between two points and if this line is longer than the view range then it can't see it. Something like this:

A------------------------- B - A can't see B
A--------------------B---- - A can see B

The question is... Wouldn't it be too slow if A should iterate through every object on the screen to see if it can see it? Or worse every non static object should iterate every other object because they can have different view range.

Do someone have some experience with this? Is there any other way around?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Line of sight - The Best Way

Post by bartbes »

If it's only range then it's just Pythagoras, but if you have a----b---c and want c to be hidden as well there's a technique you could use:
  1. Find the nearest object
  2. Mark it as visible (if in range)
  3. Check out the min-max angle past the object
  4. Mark every object behind it as invisible
  5. Loop until there are no more unmarked objects
However if you only want to not draw things off-screen (or outside of a certain rectangle) use love.graphics.setScissor.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Line of sight - The Best Way

Post by Robin »

It depends on how many objects you have on screen. If you don't have too many, it shouldn't be a problem.

Otherwise, a way to reduce calculations would be to put every object on a grid, where each box is just over the largest LOS distance. Then you only have to compare for objects in the same box and in the 8 boxes around them. (Unfortunately, you can't assume objects in the same box are within LOS.)
Help us help you: attach a .love.
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Line of sight - The Best Way

Post by pekka »

Also, if you are willing to learn how to use the Physics module, you can make use of sensors.

http://love2d.org/wiki/Shape:setSensor

The basic idea is that you create a shape that represents the viewing range and set is as a sensor, and then place it wherever you want the viewing range to be. You will get callbacks of all the collisions with that shape during the next physics update, but these collisions don't affect the other bodies in the physics world physically, as it says on the linked page.

You can expect the Physics module to perform this checking efficiently, as it is built for things like this.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Line of sight - The Best Way

Post by kikito »

When I have this kind of questions, I usually go to gamedev.net

The articles there are valid, abheit a bit old.

I found this one doing a quick search: http://www.gamedev.net/reference/articl ... cle729.asp

Two ideas are presented there, and both assume a tileable world. The first one is: precalculate the "shadow" of all objects on meaningful degrees (eg 32 different angles) and then line of sight is just adding shadows and seeing if an object is contained on them or not.

The other idea is: walls behave a little bit differently than "regular objects" (such as enemies) when calculating line of sight (LOS).
When I write def I mean function.
mufty
Prole
Posts: 10
Joined: Fri Apr 09, 2010 5:11 pm

Re: Line of sight - The Best Way

Post by mufty »

thanks a lot guys

first i've been trying pythagoras but as long as i use physics and i didn't noticed sensor functionality there this is the right and easiest way for me to go. sensors works like charm with the oportunity to use callbacks it's just fine. So if anyone is intrested i recomend use sensors if you're using physics. If you're in a tile based world (which i'm not) i would go with the theory from kikito. In other case use barbes method.

There will always be a solution for your situation.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Line of sight - The Best Way

Post by bartbes »

Thanks for misspelling my name :cry:
User avatar
Luiji
Party member
Posts: 396
Joined: Mon May 17, 2010 6:59 pm

Re: Line of sight - The Best Way

Post by Luiji »

:ultrahappy: He forgot the "t" and that simple mistake turned your name into the same of a girl's doll!
Good bye.
mufty
Prole
Posts: 10
Joined: Fri Apr 09, 2010 5:11 pm

Re: Line of sight - The Best Way

Post by mufty »

BARTBES I'm rly sorry about that. I'm too lazy to copy paste it:-). To make things right I'll worship your name.

Btw it doesn't sound like Malibu "doll" :-)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Line of sight - The Best Way

Post by Robin »

mufty wrote:To make things right I'll worship your name.
No! It will make things worse and give bartbes ideas!
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 52 guests