Started by Sep 23 2012 05:24 AM

,
10 replies to this topic

Posted 23 September 2012 - 05:24 AM

The title says it all. How do i get the number of solid tiles between two points (or two tiles)? Ive been trying to think as to how to implement this algorithm, but all attempts so far have failed.

Here is a image showing what I want to do...

www.xanather.com/lighting.png

(the red tiles and the line are drawn in through paint, not part of the rendering).

The reason I want to implement this is to allow for dynamic 2D lighting between all tiles. A wall of tiles will block some lighting (it similar to Terraria's lighting).

After I implement this I will be able to obtain how many solid tiles are between two points and reduce the lighting depending on the amount of solid tiles that are in between these two points.

Anyway, I hope someone can help me

All replies are appreciated. Thanks,

Xanather.

Here is a image showing what I want to do...

www.xanather.com/lighting.png

(the red tiles and the line are drawn in through paint, not part of the rendering).

The reason I want to implement this is to allow for dynamic 2D lighting between all tiles. A wall of tiles will block some lighting (it similar to Terraria's lighting).

After I implement this I will be able to obtain how many solid tiles are between two points and reduce the lighting depending on the amount of solid tiles that are in between these two points.

Anyway, I hope someone can help me

All replies are appreciated. Thanks,

Xanather.

Posted 23 September 2012 - 05:33 AM

EDIT: Sorry, just realized my below algorithm is incorrect, ignore it for now.

Loop start from one point, move in dx=TileWidth-1, dy=TileHeight-1 toward to the end point, until reach or beyond the end point.

In each move, get the tile at that point, check if it's a solid tile.

This will be sure you loop all tiles covered by the line and won't miss any tile.

Loop start from one point, move in dx=TileWidth-1, dy=TileHeight-1 toward to the end point, until reach or beyond the end point.

In each move, get the tile at that point, check if it's a solid tile.

This will be sure you loop all tiles covered by the line and won't miss any tile.

**Edited by wqking, 23 September 2012 - 05:37 AM.**

http://www.cpgf.org/

cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

v1.5.5 was released. Now supports tween and timeline for ease animation.

Posted 23 September 2012 - 05:56 AM

The number of tiles is the maximum of the distance in the X or Y direction. If you have two points (x1,y1) and (x2,y2), then the number of tiles is max(abs(x2-x1), abs(y2-y1)).

There are a few variants to how to treat the values you have to be aware of though. Depending on if you want to, say, count both the start and end tiles, or only count the start or end tile but not he other, or if you want the number of tiles bwteen the two points, and how you want to handle fractional coordinates, you may have to round the values differently and at different places and add or subtract one at some places.

Combinations includes round(), floor(), ceil() of either the individual coordinates, the difference between them, of absolute values or of the maximum value.

There are a few variants to how to treat the values you have to be aware of though. Depending on if you want to, say, count both the start and end tiles, or only count the start or end tile but not he other, or if you want the number of tiles bwteen the two points, and how you want to handle fractional coordinates, you may have to round the values differently and at different places and add or subtract one at some places.

Combinations includes round(), floor(), ceil() of either the individual coordinates, the difference between them, of absolute values or of the maximum value.

**Edited by Brother Bob, 23 September 2012 - 05:58 AM.**

Posted 23 September 2012 - 07:11 AM

the number of tiles is max(abs(x2-x1), abs(y2-y1)).

How does that work? Wont that just get the number of tiles in one of the two directions? (only the change in X or only the change in Y)?

Xanather.

Posted 23 September 2012 - 07:35 AM

You can try to use Bresenham's line algorithm. Go from the source to the destination you can then count the number of solid tiles along the way.

Posted 23 September 2012 - 08:00 AM

Look at the picture you posted and count the pixels, and think of how moving one of the points one or two steps to the left or right affects the outcome. There are 10 red squares, and there are 10 pixels difference in the Y-direction. Moving, say, the top point one or two points to the left or to the right will just change how the 10 points are distrubuted along the X-axis. It's only when the end point is 11 pixels or more away from the start point in the X-direction that you have to add more points in the X-direction, but then the X-direction has become dominant and that is taken care of by the max() function.the number of tiles is max(abs(x2-x1), abs(y2-y1)).

How does that work? Wont that just get the number of tiles in one of the two directions? (only the change in X or only the change in Y)?

Xanather.

Bresenham's algorithm as TrickyLogic linked to is a perfect example demonstrating this. Bresenham's works by looping over each discrete step in

**Edited by Brother Bob, 23 September 2012 - 08:01 AM.**

Posted 23 September 2012 - 11:14 AM

The picture actually looks like it's using Bresenham's algorithm. Notice how it favors one axis (Y) over the other (X)? Even though the actual line covers some tiles by almost half, they aren't in red, while others are barely covered and are in red. It's iterating over Y with error accumulation in X.u

Basically forget the pixel level of it all. Your creating a line between two tiles. Using Bresenhams algorithm over the tiles is the same as using it over pixels.

Now, as others have said, if your really just wanting 'the number of solid tiles between two points' and your counting the tiles in red in the photo, then it is just the greater of the absolute of the difference in Y or X. (As defined by the outer loop in the algorithm.)

Basically forget the pixel level of it all. Your creating a line between two tiles. Using Bresenhams algorithm over the tiles is the same as using it over pixels.

Now, as others have said, if your really just wanting 'the number of solid tiles between two points' and your counting the tiles in red in the photo, then it is just the greater of the absolute of the difference in Y or X. (As defined by the outer loop in the algorithm.)

Posted 23 September 2012 - 11:55 AM

Now, as others have said, if your really just wanting 'the number of solid tiles between two points' and your counting the tiles in red in the photo, then it is just the greater of the absolute of the difference in Y or X. (As defined by the outer loop in the algorithm.)

This algorithm wouldn't account for any missing tiles. Although it would speed it up greatly. Perhaps a switch can be implemented for people using slower computers.

Posted 24 September 2012 - 03:10 AM

Looking into Bresenham's algorithm now, looks like its what I need. Thanks

Posted 24 September 2012 - 06:09 AM

Ok well, I managed to implement Bresenham's line algorithm. Its creating some nice effects too...

http://www.xanather.com/hmm.png

And here is the code:

[source lang="csharp"] static byte GetTileReduction(int x0, int y0, int x1, int y1) { int reduction = 0; if (x0 > x1) { int temp1 = x1; int temp2 = y1; x1 = x0; y1 = y0; x0 = temp1; y0 = temp2; } int deltax = x1 - x0; if (deltax == 0) { deltax = 1; } int deltay = y1 - y0; float error = 0; float deltaerror = (deltay / deltax); int y = y0; for (int x = x0; x < x1; x++) { if (Engine.tiles[x, y].type != 0) { reduction += 20; if (reduction > 255) { reduction = 255; } } error += deltaerror; if (error >= 0.5f) { y = y + 1; error = error - 1f; } } return (byte)reduction; }[/source]

The problem is that this occurs when the light source is completely underground...

http://www.xanather.com/hmm2.png

I am lost, it seems as when the "imaginary" line in the algorithm gets steeper the solid tiles found reduce, resulting in the brighter-than-it-should-be colored tile (brighter) along the vertical parts of the lighting... It should be just a smaller sphere (circle).

I hope you understand what I mean, Ive been trying to tackle this for a while now.

I feel like the actual problem is right under my nose xD

Xanather.

EDIT:

The

[source lang="csharp"]if (deltax == 0) { deltax = 1; }[/source]

is temporary obviously...

http://www.xanather.com/hmm.png

And here is the code:

[source lang="csharp"] static byte GetTileReduction(int x0, int y0, int x1, int y1) { int reduction = 0; if (x0 > x1) { int temp1 = x1; int temp2 = y1; x1 = x0; y1 = y0; x0 = temp1; y0 = temp2; } int deltax = x1 - x0; if (deltax == 0) { deltax = 1; } int deltay = y1 - y0; float error = 0; float deltaerror = (deltay / deltax); int y = y0; for (int x = x0; x < x1; x++) { if (Engine.tiles[x, y].type != 0) { reduction += 20; if (reduction > 255) { reduction = 255; } } error += deltaerror; if (error >= 0.5f) { y = y + 1; error = error - 1f; } } return (byte)reduction; }[/source]

The problem is that this occurs when the light source is completely underground...

http://www.xanather.com/hmm2.png

I am lost, it seems as when the "imaginary" line in the algorithm gets steeper the solid tiles found reduce, resulting in the brighter-than-it-should-be colored tile (brighter) along the vertical parts of the lighting... It should be just a smaller sphere (circle).

I hope you understand what I mean, Ive been trying to tackle this for a while now.

I feel like the actual problem is right under my nose xD

Xanather.

EDIT:

The

[source lang="csharp"]if (deltax == 0) { deltax = 1; }[/source]

is temporary obviously...

**Edited by Xanather, 24 September 2012 - 06:14 AM.**

Posted 24 September 2012 - 06:57 AM

OK, I solved it. I instead implemented the optimization algorithm found here:

http://en.wikipedia...._line_algorithm

I realized that I wasn't following the rules of the first basic algorithm. The second algorithm on that site takes care of that.

The lighting looks awesome now!

I have no idea how it works, but I will be sure to make sense of it later on...

Thanks everyone : D

http://en.wikipedia...._line_algorithm

I realized that I wasn't following the rules of the first basic algorithm. The second algorithm on that site takes care of that.

The lighting looks awesome now!

I have no idea how it works, but I will be sure to make sense of it later on...

Thanks everyone : D

**Edited by Xanather, 24 September 2012 - 07:00 AM.**