Created
November 14, 2025 23:27
-
-
Save twobob/d64a53a540bf3d7f6490f9f484b234a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extends Node | |
| const DIR_N = 1 << 0 | |
| const DIR_NE = 1 << 1 | |
| const DIR_E = 1 << 2 | |
| const DIR_SE = 1 << 3 | |
| const DIR_S = 1 << 4 | |
| const DIR_SW = 1 << 5 | |
| const DIR_W = 1 << 6 | |
| const DIR_NW = 1 << 7 | |
| func direction_bitfield(vec: Vector2) -> int: | |
| if vec.length() == 0.0: | |
| return 0 | |
| # Godot measures angles starting from the right-hand side, | |
| # which is a bit awkward when all you want is north at the top. | |
| # So we grab the angle as Godot gives it… | |
| var ang = atan2(vec.y, vec.x) | |
| # …then we shift it so that pointing upwards becomes the “start” | |
| # of the circle instead of pointing right. | |
| ang -= PI / 2.0 | |
| # If the angle dips below zero, we just wrap it back around | |
| # so everything sits nicely between 0 and a full turn. | |
| if ang < 0.0: | |
| ang += PI * 2.0 | |
| # The full circle is chopped into eight equal pieces. | |
| # Each piece is 45 degrees, so this works out neatly. | |
| var slice = PI / 4.0 | |
| var sector = int(floor(ang / slice)) | |
| # Whatever slice we landed in, we convert that number into a single bit. | |
| return 1 << sector | |
| func _ready(): | |
| var v = Vector2(1, -1) # roughly north-east | |
| var d = direction_bitfield(v) | |
| print("bit:", d) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
totally untested