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

Add a distance_to() method to Vector2i #7983

Closed
TheKassaK opened this issue Oct 4, 2023 · 3 comments · Fixed by godotengine/godot#83163
Closed

Add a distance_to() method to Vector2i #7983

TheKassaK opened this issue Oct 4, 2023 · 3 comments · Fixed by godotengine/godot#83163
Milestone

Comments

@TheKassaK
Copy link

Describe the project you are working on

A 2D colony simulation game, with a tilemap and a lots of calculation everywhere

Describe the problem or limitation you are having in your project

There is no equivalent of Vector2.DistanceTo for Vector2I

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add a Vector2I.DistanceTo() function, this function return a float => number of tiles between each position

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

The code I use :

        public static float DistanceTo(Vector2I from, Vector2I to)
        {
            float dx = to.X - from.X;
            float dy = to.Y - from.Y;

            float distance = Mathf.Sqrt(dx * dx + dy * dy);

            return distance;
        }
        
        GD.Print(Utils.DistanceTo(new Vector2I(1, 1), new Vector2I(5, 5))); // = 5,656854
        GD.Print(Utils.DistanceTo(new Vector2I(5,5), new Vector2I(1,1))); // = 5,656854

If this enhancement will not be used often, can it be worked around with a few lines of script?

Yes, see my code above

Is there a reason why this should be core and not an add-on in the asset library?

Because its a basic function when we use 2D grid (I guess)

@dalexeev
Copy link
Member

dalexeev commented Oct 4, 2023

Perhaps other metrics than Euclidean may be useful for Vector2i (see AStarGrid2D)?

@TheKassaK
Copy link
Author

Yes, for my project I dont need more than Euclidean, but it seems to me a good idea to let the possibility of choosing the calculation method

@kleonc
Copy link
Member

kleonc commented Oct 5, 2023

This proposal is a subset of #2297.

Perhaps other metrics than Euclidean may be useful for Vector2i (see AStarGrid2D)?

I think other metrics are too use case specific for them to be included in vectors (whether as separate method(s) or as argument of distance_to/distance_squared_to). Note that such metrics are not limited to integers, they can be used with reals just fine. However, currently in Vector2/3/4 there are only distance_to/distance_squared_to methods calculating the Euclidean distance. So if support for other metrics were to be added then it would make sense to do so for all vectors (both real and integer). But again, I'm against as this doesn't seem like a common/general enough use case.
And if that's needed/desired specifically for AStarGrid2D then probably some method could be added to AStarGrid2D.


Regarding the proposal, adding only Vector2i.distance_to would make things inconsistent. But adding both distance_to and distance_squared_to to all integer vectors (Vector2i/3i/4i) does make sense, it's a part of #2297 which shouldn't be anyhow controversial (like the length/length_squared case).

@Calinou Calinou changed the title DistanceTo function for Vector2I Add a distance_to() method to Vector2i Oct 5, 2023
@akien-mga akien-mga added this to the 4.3 milestone Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants