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

EdgeDirection::unit_vector is Y flipped? #186

Closed
MatrixDev opened this issue Dec 19, 2024 · 6 comments · Fixed by #187
Closed

EdgeDirection::unit_vector is Y flipped? #186

MatrixDev opened this issue Dec 19, 2024 · 6 comments · Fixed by #187
Labels
bug Something isn't working
Milestone

Comments

@MatrixDev
Copy link

Describe the bug
I'm using unit_vector to rotate objects in the hex. But it seams that Y component of this vector is always flipped.

To Reproduce
Here is a simple test, that calculates direction manually vs unit_vector:

let layout = HexLayout::default();
for direction in EdgeDirection::ALL_DIRECTIONS {
    let h0 = hex(0, 0);
    let h1 = h0.neighbor(direction);

    let h0pos = layout.hex_to_world_pos(h0);
    let h1pos = layout.hex_to_world_pos(h1);

    let look_to_test = (h1pos - h0pos).normalize();
    let look_to = direction.unit_vector(layout.orientation);

    println!("{look_to_test} vs {look_to}");
}

And here are results:

[0.8660254,   -0.49999997] vs [0.8660254,          0.5]
[0,           -0.99999994] vs [-0.00000004371139,  1]
[-0.8660254,  -0.49999997] vs [-0.8660254,         0.50000006]
[-0.8660254,   0.49999997] vs [-0.8660253,        -0.5000002]
[0,            0.99999994] vs [0.000000011924881, -1]
[0.8660254,    0.49999997] vs [0.86602527,        -0.5000002]

As you can see Y component is always flipped, while X is always the same (excluding rounding errors)

Expected behavior
unit_vector should return a direction to a neighbor hex.

Screenshots Or Code snippet
Provided above.

@ManevilleF
Copy link
Owner

Nice catch, I'll write some tests to reproduce this behaviour and propose a fix asap

@ManevilleF
Copy link
Owner

Okay so this was not a really bug but due to HexLayout having its Y axis inverted by default

@MatrixDev
Copy link
Author

Okay so this was not a really bug but due to HexLayout having its Y axis inverted by default

But what does it represent than? Souldn't hex_to_world_pos also have Y flipped?

@ManevilleF
Copy link
Owner

hex_to_world_pos is a method of HexLayout, so it had the inverted axis. The PR I opened changes this behaviour to have the default layout uninverted, matching methods like angle and unit vector computations outside of the layout.
You may review #187

@MatrixDev
Copy link
Author

MatrixDev commented Dec 20, 2024

@ManevilleF, now I understand. thanks for explanation.

So just to clarify, in case I have non-default HexLayouts, I cannot use this unit_vector. The only proper way is to use the (h1pos - h0pos).normalize() trick above, as this PR fixes only the "defaults mismatch".

@ManevilleF
Copy link
Owner

unit_vector is not in world space. It doesn't know about:

  • The size of the hexagons
  • The axis scales
  • The world origin
  • etc

Therefore to be applied in world space transformations are needed. HexLayout could provide such transformations if you want to avoid doing the normalized world space operations

@ManevilleF ManevilleF added this to the 0.20.0 milestone Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants