Distance in tiles
BlitzMax Forums/BlitzMax Beginners Area/Distance in tiles
| ||
I am trying to make some kind of light system to my tile engine. So i need to check distance (in tiles) between 2 tiles. Here is picture: So how do i check which tiles are under 10 tiles away from tile where cursor is? I need distance to "map[x,y].distance". Also if you have better way to do it, tell me. Thanks. ;) |
| ||
simple but wasteful way (maybe a non issue depending on your needs) is loop through the tiles and check their distances from your focal point. I had C code handy, but should be easy enough to understand to put it into blitz float Distance(float x1, float y1, float x2, float y2) { return sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) ); } A more optimized way would be check the tiles that are within x +/- 5 and y +/- 5 for their distance, so you're not checking everything, just things that are within a 10x10 box around your point... then you need some grid math to calculate the points in that box. |
| ||
Here's a quick test proggy doing what ima747 advised... It looks in a box around the cursor and tests if the centre of the tile is within a pre-set distance to the mouse cursor. |
| ||
Thanks ima747 and matibee. :) |
| ||
@Matibee A brute force 'circle fill' routine. Nice :D Don't see those nowadays :P hehe |