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?
Line of sight - The Best Way
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Line of sight - The Best Way
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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Line of sight - The Best Way
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.)
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.
Re: Line of sight - The Best Way
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.
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.
- 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
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).
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.
Re: Line of sight - The Best Way
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.
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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Line of sight - The Best Way
Thanks for misspelling my name
Re: Line of sight - The Best Way
He forgot the "t" and that simple mistake turned your name into the same of a girl's doll!
Good bye.
Re: Line of sight - The Best Way
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" :-)
Btw it doesn't sound like Malibu "doll" :-)
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Line of sight - The Best Way
No! It will make things worse and give bartbes ideas!mufty wrote:To make things right I'll worship your name.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests