bresenham like LOS algorithm that works with corners?

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.
User avatar
veethree
Inner party member
Posts: 876
Joined: Sat Dec 10, 2011 7:18 pm

bresenham like LOS algorithm that works with corners?

Post by veethree »

So i'm playing around with an idea for a puzzle platformer where you can place a certain amount of tiles on each level to complete it (you have to get from point a to point b and maybe collect some shit before doing that). And i dont want the player to be able to place tiles when there are tiles between the player and wherever he is trying to place the tiles. My first idea was bresenham.lua by kikito. And it kinda works, But shortly after implementing it i was reminded that if you check for a line of sight with it, it sees right through something like this:
(Picture cause i forgot how to use words)
Image
green square = player
red outline = players grid position
gray outlines = What bresenham sees
yellow square = tile the player placed

And that's not what i want. So i'm wondering if there's perhaps a way to modify bresenham to not do that, Or a different algorhithm perhaps.

.love controls:
WASD or arrow keys to move and jump
LMB = Place tile
RMB = Remove tile (only works on tiles you placed, and that is intended.)

Note that this is an early prototype. Also for some reason the .love crashed löve for me, Works fine when i just drag the folder to love.exe. Let me know if the same happens to you and i'll just post a .zip or something if necessary.
EDIT: Just tested the .love on my mac, Crashed there as well. You may have to extract the source to run it. Not sure what i did wrong here...
Attachments
Possibly platforms.love
(21.33 KiB) Downloaded 73 times
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: bresenham like LOS algorithm that works with corners?

Post by markgo »

First find out which quadrant your LOS is. Let's say that you're in the first quadrant with a counterclockwise orientation:

Code: Select all

2|1
----
3|4
The first quadrant has this case where # is a solid:

Code: Select all

.#
.?#
@..
You (@) want your LOS to stop at ? and go no further. To do this, add an additional check for each cell if the top and right neighbors are solid. If they are, then you can't see inbetween them, and quit the LOS. For the other quadrants, transform the coordinates in the first quadrant to the respective one. This way, you can make a loop which handles all case with a transformation.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bresenham like LOS algorithm that works with corners?

Post by kikito »

The typical bresenham algorithm is good for drawing pretty lines, but it's not "exhaustive"; definitively it's not a good choce for line of sight.

There's a bresenham supercover algorithm made by Roland Yonaba right here: https://github.com/Yonaba/Algorithm-Imp ... rcover.lua

I have not used it yet, but definitively this kind of algorithm is more appropiate for line-of-sight and path finding stuff.
Last edited by kikito on Fri Jan 24, 2014 1:10 pm, edited 1 time in total.
When I write def I mean function.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: bresenham like LOS algorithm that works with corners?

Post by micha »

I thought about the super-cover idea, too.

But what does it do on perfectly diagonal lines between cell centers? Does it give the same result as the original Bresenham algorithm or does it make a "buffer" around the line?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bresenham like LOS algorithm that works with corners?

Post by kikito »

I have not tried the algorithm, but in my mind, for a "perfectly diagonal" line the supercover should cover "the diagonal + 1 tile-border around the cells in the diagonal". So here's bresenham vs bresenham-supercover on a perfect diagonal:

Code: Select all

                     x                        xx
                    x                        xxx
                   x      +---------->      xxx
                  x                        xxx
                 x                         xx
When I write def I mean function.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: bresenham like LOS algorithm that works with corners?

Post by micha »

I'd count both solution as possibly correct, depending on the problem formulation. The question is, if cells are considered as closed intervals or open intervals. Veethree, you seem to consider them as closed intervals, because two diagonally adjecent cells touch each other. I will try and see, what the super-cover version of the algorithm really does. Now I am curious.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: bresenham like LOS algorithm that works with corners?

Post by Roland_Yonaba »

kikito wrote:I have not tried the algorithm, but in my mind, for a "perfectly diagonal" line the supercover should cover "the diagonal + 1 tile-border around the cells in the diagonal"
Exactly.

@Kikito: By the way, would you be okay for a pull request to bresenham.lua providing support for supercover LOS ? :D
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: bresenham like LOS algorithm that works with corners?

Post by micha »

Thank you, guys.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: bresenham like LOS algorithm that works with corners?

Post by kikito »

Roland_Yonaba wrote:@Kikito: By the way, would you be okay for a pull request to bresenham.lua providing support for supercover LOS ? :D
By all means. Do you think you can remove some code duplication? In particular, this conditional? I know the original algorithm has the code duplicated, but that's because they pursue the total maximum efficiency possible in C (I actually asked the guy).
When I write def I mean function.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: bresenham like LOS algorithm that works with corners?

Post by Roland_Yonaba »

kikito wrote:By all means. Do you think you can remove some code duplication? In particular, this conditional? I know the original algorithm has the code duplicated, but that's because they pursue the total maximum efficiency possible in C (I actually asked the guy).
Hmm. Not sure, but I'll take a closer look to that. ;)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], DTmg and 3 guests