NWSE mouse click

Godot Version

4.4

Question

How define this:

i have click mouse vector2 which is normalized, how to get values as described on the screenshot?

Thanks a lot for any help!

You could get the absolute maximum of each axis. I would use absf to compare absolute values, then signf to convert the relative mouse position to a negative or positive 1.0

var mouse_click: Vector2 = your_relative_get_mouse_click()
var cardinal_click: Vector2
if absf(mouse_click.x) > absf(mouse_click.y):
    # X axis is highest value, only use mouse's x value
    cardinal_click = Vector2(signf(mouse_click.x), 0.0)
else:
    # Y axis is highest value
    cardinal_click = Vector2(0.0, signf(mouse_click.y))
1 Like

The sign of the x component tells you left (negative) or right (positive).
The sign of the y component tells you up (negative) or down (positive).
The relative magnitude of the components tells you whether you should be looking at x or y.

enum Direction { none, up, down, left, right }

func quantify_vec(v: Vector2) -> Direction:
    if is_zero_approx(x) && is_zero_approx(y):
        return Direction.none
    if absf(x) >= absf(y):
        if x < 0.0:
            return Direction.left
        else:
            return Direction.right
    else: # y is larger
        if y < 0.0:
            return Direction.up
        else:
            return Direction.down
1 Like

Is the mouse vector2 local to the player, or in global coordinates? Since the above solutions will not work in global coordinates.

2 Likes

This my
mouseClickPoint: Vector2 = Vector2(get_global_mouse_position() - global_position).normalized()

1 Like

Either @gertkeno 's or my solution should work, then. As @paintsimmon says, if you weren’t dealing with a screen-aligned vector the answer gets more complicated, but you’ve got a screen-aligned vector.

2 Likes

Thank you so much. Thank to all of you gyus! It’s worked!




All solutions are working, I checked both. But I chose for myself the more cumbersome but understandable to me personally from @hexgrid

3 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.