Page 1 of 1
Line of sight - The Best Way
Posted: Thu May 13, 2010 8:27 am
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?
Re: Line of sight - The Best Way
Posted: Thu May 13, 2010 8:47 am
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:
- Find the nearest object
- Mark it as visible (if in range)
- Check out the min-max angle past the object
- Mark every object behind it as invisible
- 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.
Re: Line of sight - The Best Way
Posted: Thu May 13, 2010 8:51 am
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.)
Re: Line of sight - The Best Way
Posted: Wed May 19, 2010 5:53 pm
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.
Re: Line of sight - The Best Way
Posted: Wed May 19, 2010 9:57 pm
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).
Re: Line of sight - The Best Way
Posted: Thu May 20, 2010 5:07 pm
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.
Re: Line of sight - The Best Way
Posted: Thu May 20, 2010 5:30 pm
by bartbes
Thanks for misspelling my name
Re: Line of sight - The Best Way
Posted: Fri May 21, 2010 7:29 pm
by Luiji
He forgot the "t" and that simple mistake turned your name into the same of a girl's doll!
Re: Line of sight - The Best Way
Posted: Fri May 21, 2010 7:55 pm
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" :-)
Re: Line of sight - The Best Way
Posted: Fri May 21, 2010 8:55 pm
by Robin
mufty wrote:To make things right I'll worship your name.
No! It will make things worse and give bartbes ideas!