Mapping bezeir curve to tiles

Started by
13 comments, last by Zakwayda 13 years, 3 months ago
Quote:Original post by codeman_nz
Thanks for that, it is very useful. How would I modify this to handle a tilemap with 10x10 pixel tiles?


First, read the comments in my code. ;) Those comments do not exist outside of this thread and were made for you. Follow them.

As LorenzoGatti mentioned, the size of your tiles is completely irrelevant. The important thing is the dimensions of your tile map. Think of your tile map as a bitmap (it is, actually). If you are using an array of chars, you have an 8-bit bitmap, an array of longs would give you a 32-bit bitmap. I have successfully used a tile map as the image pointer to a Win32 GDI bitmap and used GDI calls to set tiles.

Anyway, the point is that no matter what you use to set the tile IDs in your tile map, make sure you stay within the array bounds.

Quote:Original post by superpig
As a general theory about this - similar to MarkS's solution but without the Bresenham approximation


Of course there are other ways and I know that the Bresenham algorithm is looked down on, but it really is fitting here. Since a tile map is a 2D array of integers, there are no "half tiles", so an integer approximation is the best.

Anyway, I never could get adjacency to work, so I caved and just drew lines to fill the gaps. There is very little to no overdraw and the lines drawn are typically either very short of near vertical/horizontal, so the curve is nearly pixel perfect and far smoother than the typical polyline approximation.

No, I am not a professional programmer. I'm just a hobbyist having fun...

Advertisement
OK thanks. I looked into the Bresenham algorithm and looks like exactly what I need. Now how would I apply it to a road which is several tiles wide?
Quote:Original post by codeman_nz
OK thanks. I looked into the Bresenham algorithm and looks like exactly what I need. Now how would I apply it to a road which is several tiles wide?


This is all VERY basic stuff. I want you to think it through, but I'll give you a hint:

You have two vertical Bezier curves defining the sides of the road with horizontal spans of tiles between them. How do you think you ca accomplish this?

No, I am not a professional programmer. I'm just a hobbyist having fun...

Thanks for the hint. I should make myself clear about what I'm trying to do.

I have a curved road which is created by a bezier curve going up through the middle. I create 100 points and calculate the parallel points either side of the centre points. The parallel points define the edge of the road.

I am now trying to map the entire road to a tile map. Currently I have Bresenham's line algorithm for the bezier curve going up through the middle of the road and that's working.

I am having trouble with the rest of the road. I tried calculating parallel points from each of the Bresenham's line algorithm points and then using another Bresenham's line algorithm between those parallel points but that didn't work.

I then tried an extension of Bresenham's line algorithm which deals with thick lines but that didn't work.

So now I'm stuck. I can map the centre of the road to the tilemap but can't do it for the rest of the road.
I can't really comment on the Bresenham stuff you're doing, but here's an alternate solution you could try. For each tile, calculate the closest point on the curve to the tile center (the easiest way to do this would probably be to subdivide the curve into line segments, and then find the closest point on the line segments making up the curve). Then, if the (squared) distance from the tile center to the curve is below some threshold (e.g. the width of the road plus the half-width of a tile), consider the tile to be part of the road.

This topic is closed to new replies.

Advertisement