Advertisement

World->Map Coords with diamond map

Started by January 26, 2000 07:03 AM
4 comments, last by Asdas 24 years, 10 months ago
I am having some problems converting world coords (pixel coords) to map coords (tile coords). To convert screen coords to world coords i found the tile in the very top left of the screen, and calculated its world coords, then added that to the screen coords - there must be a better way!, but thats not what im stuck on. Im really too embarrased to show the code i used, it was probably the most inefficient method of doing it (it involved calculating the world coords of every tile on the map and comparing it to the screens world cord). Anyway, im using the diamond map method described a few days ago to me by TANSTAAFL, and I was wondering if anyone has a good method or code they dont mind sharing?
You could try using a trigonometric function I think...

If you have a constant angle at which the tiles are slanted, you just normalize your coordinates to the world system...looks quite challenging...

- Sleepwalker
- Sleepwalker
Advertisement
You could try using a trigonometric function I think...

If you have a constant angle at which the tiles are slanted, you just normalize your coordinates to the world system...looks quite challenging...

- Sleepwalker
Oooops...sorry
- Had a network problem...that''s what you get using IE. darn it...
- Sleepwalker
Okay, I had this exact same problem, but in exact reverse. I had the center tile''s map coords, but I couldn''t find the top left tile''s coordinates. I finally figured it out. You''ll be really SURPRISED. Trick math. Given the variables CenterTile_X, CenterTile_Y (map coords), SCREEN_WIDTH, SCREEN_HEIGHT, TILE_WIDTH, and TILE_HEIGHT, I can find out what the top left coordinate is. Maybe this isn''t what you''re looking for, but hey, it could help. Here''s the math:



//Run this code in a different function
//It''s really long, so it''s use many cpu cycles unless
//You run it only once
double TileConstant =
(SCREEN_HEIGHT / 2.0) -
sqrt(
(SCREEN_WIDTH * SCREEN_WIDTH / 4.0) +
(SCREEN_WIDTH * SCREEN_WIDTH * TILE_HEIGHT * TILE_HEIGHT /
(4.0 * TILE_WIDTH * TILE_WIDTH)) -
(SCREEN_WIDTH * SCREEN_WIDTH / 4.0)
);
TileConstant /= (double)TILE_HEIGHT;

int XOffset = (SCREEN_WIDTH / (double)TILE_WIDTH) - TileConstant;
int YOffset = TileConstant;

//Okay, now put this in your calculations to be run every
//frame
int TopLeftXCoord = CenterTile_X - XOffset;
int TopLeftYCoord = CenterTile_Y - YOffset;


Once you get the first calculations done, then it''s really a snap. You''re probably running everything from a pixel->world system, but if you want to just start with a center tile coordinate system, then this is how you find the top left tile. Trust me, it works. The topleft tile may be negative in the calculations, but it''s still correct. It''ll tell you what tile WOULD''VE been there, and it''ll still center out the tile that''s supposed to be in the center. Good luck!

ColdfireV
[email=jperegrine@customcall.com]ColdfireV[/email]
This is some convoluted math here. It''s way overcomplicated. Are you sure you didn''t concoct this just to confuse the person? If you were sincerely trying to help then I suggest you sincerely study some basic remedial algebra. I mean look at this:

sqrt( (SCREEN_WIDTH * SCREEN_WIDTH / 4.0) +
+ (SCREEN_WIDTH * SCREEN_WIDTH * TILE_HEIGHT * TILE_HEIGHT / (4.0 * TILE_WIDTH * TILE_WIDTH))
- (SCREEN_WIDTH * SCREEN_WIDTH / 4.0)
);

You''re kidding, right? Aren''t you subtracting the first term at the end? Don''t these cancel out? Doesn''t that leave you with (sw**2)(th**2)/4? Isn''t the square root of that sw*th/2? Why are you going through all that other happy horseshit? There''s more simplification to be done besides this. What you end up with is the exact same values for your Xoffset and your Yoffset which I don''t think is what you want.

This topic is closed to new replies.

Advertisement