Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use grid size based arithmetics for offsets in hex tile layout #463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ulfhermann
Copy link

By refraining from using sqrt(3) based arithmetics we get the tiles aligned to the provided textures and grid size. This is not mathematically exact, but arguably what the user wants when providing tiles of specific size.

The central realization here is that neighboring tiles on the "diagonal" axis are always offset by a half tile size in one direction and a 3/4 tile size in the other. So, by providing tiles with sizes divisible by 4, you can get pixel perfect alignment of tiles.

The calculations are also quite a bit simpler this way.

See #456 for some further comments on this.

Copy link
Owner

@StarArawn StarArawn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This math has changed quite a bit from when I first wrote it. Arguably for the better. I can't say I know enough about the math specifically or have the time to dive in to see if this is correct in all user cases. If someone else is willing to review this PR we can merge it assuming it looks good to go.

@ulfhermann
Copy link
Author

I've updated the patch and resolved the conflict.

@ulfhermann ulfhermann force-pushed the hextilefix branch 2 times, most recently from 6b15399 to 6ed627d Compare January 28, 2024 08:45
By refraining from using sqrt(3) based arithmetics we get the tiles
aligned to the provided textures and grid size. This is not
mathematically exact, but arguably what the user wants when providing
tiles of specific size.

The central realization here is that neighboring tiles on the "diagonal"
axis are always offset by a half tile size in one direction and a 3/4
tile size in the other. So, by providing tiles with sizes divisible by
4, you can get pixel perfect alignment of tiles.

The calculations are also quite a bit simpler this way.

See StarArawn#456 for some further comments on this.
@@ -5,13 +5,7 @@

// Gets the screen space coordinates of the bottom left of an isometric tile position.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to this PR, but all the comments seem to be copy and paste... Haha.


let unscaled_pos = pos.x * ROW_BASIS_X + pos.y * ROW_BASIS_Y;
return vec2<f32>(grid_width * unscaled_pos.x, ROW_BASIS_Y.y * grid_height * unscaled_pos.y);
return vec2<f32>(grid_width * (pos.x + pos.y / 2.0), grid_height * pos.y * 0.75);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think naming these in variables as before would help, e.g.

Suggested change
return vec2<f32>(grid_width * (pos.x + pos.y / 2.0), grid_height * pos.y * 0.75);
let ROW_BASIS_X: f32 = 0.5;
let ROW_BASIS_Y: f32 = 0.75;
return vec2<f32>(grid_width * (pos.x + pos.y * ROW_BASIS_X), grid_height * pos.y * ROW_BASIS_Y);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants