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 Vector2.from_angle() method #48237

Merged
merged 1 commit into from
Sep 1, 2021
Merged

Conversation

KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Apr 27, 2021

Resolves 2D part of godotengine/godot-proposals#1719 (someone wanted Vector3 equivalent, but it probably requires some more math so meh)

With this you can do

Vector2.from_angle(PI/2)

and it will return Vector2(0, 1)

(or at least it would if static functions weren't bugged, see #48238)

@KoBeWi KoBeWi added this to the 4.0 milestone Apr 27, 2021
@KoBeWi KoBeWi requested review from a team as code owners April 27, 2021 13:56
doc/classes/Vector2.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from af24c90 to 075fb36 Compare April 27, 2021 15:53
core/math/vector2.cpp Outdated Show resolved Hide resolved
doc/classes/Vector2.xml Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 075fb36 to 7eeb450 Compare April 27, 2021 18:17
doc/classes/Vector2.xml Outdated Show resolved Hide resolved
core/math/vector2.h Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 7eeb450 to 5c1bcf9 Compare April 27, 2021 20:20
Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

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

Doesn't work in my testing:

Screenshot from 2021-04-27 17-26-57

Missing test cases.

Missing C# version.

@Xrayez
Copy link
Contributor

Xrayez commented Apr 27, 2021

Missing test cases.

Writing tests is not currently required (unlike documentation), but highly recommended. See also godotengine/godot-proposals#1586.

Missing C# version.

Are there any contribution guides in the documentation to make this happen? I understand that C# is officially supported language, but I'm not sure if it's required for contributors to implement those methods currently. This question also applies to VisualScript, GDNative etc...

As for myself, I'm not using C# at all. Does that mean my own contribution to core would be blocked because of lack of C# knowledge?

@aaronfranke
Copy link
Member

aaronfranke commented Apr 27, 2021

@Xrayez It's not necessarily a blocker, but if it's not done here, someone will have to do it later for consistency. It's easier to just do it everywhere at once. If you want to update the documentation, that would be great.

As for VisualScript and GDNative, they get access to methods via variant_call.cpp (C# doesn't do this). I don't know if this includes static methods, but looking at the code, I don't see any special logic for from_hsv...

@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 5c1bcf9 to 5e82996 Compare April 27, 2021 21:48
@KoBeWi KoBeWi requested a review from a team as a code owner April 27, 2021 21:48
@KoBeWi
Copy link
Member Author

KoBeWi commented Apr 27, 2021

Doesn't work in my testing:

This is due to #48238.
You need to call it on a vector for now.

Missing test cases.

This is task for #47202

Missing C# version.

Added, but I never compiled mono version, so I can't test it.

@Xrayez
Copy link
Contributor

Xrayez commented Apr 27, 2021

It's not necessarily a blocker, but if it's not done here, someone will have to do it later for consistency. It's easier to just do it everywhere at once.

Most of the time (if not always), I'd like to test a feature myself before doing changes. If I don't know how to test something, I'd rather avoid doing those changes, unless someone else is willing to test those changes before merging, otherwise this could lead to broken code. So, IMO, it's better that someone else does those changes in a separate PR who know better.

I'd just make it clear that you're willing to test the implementation if you do want to see them in any PR, not just "Missing C# version". I'm just providing my feedback on the review process, sorry. 🙂

@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 5e82996 to 0e9e17e Compare April 27, 2021 22:14
Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

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

The C# code looks good, and the tests can wait. This PR isn't fully functional until #48238 is fixed, so we can either fix that first, or merge this first and use this method as a test for #48238.

@groud
Copy link
Member

groud commented Apr 28, 2021

The idea makes sense, but what you are providing here is simply half of of what would be needed to allow building any vector from polar coordinates.

Consequently, I think it would be more feature-complete to rename the function to from_polar(...) and to allow a second argument (that could be 1.0 by default), that would allowing providing a length too.

@KoBeWi
Copy link
Member Author

KoBeWi commented Apr 28, 2021

The length argument wouldn't be very useful. It's a unit vector, so you can just multiply it.

Also the function was never intended to have anything to do with polar coordinates (which very few people use). from_angle() has similarity to angle() method, which makes it more obvious that the two methods are related, so it's easier to find.

@groud
Copy link
Member

groud commented Apr 28, 2021

The length argument wouldn't be very useful. It's a unit vector, so you can just multiply it.
Also the function was never intended to have anything to do with polar coordinates (which very few people use). from_angle() has similarity to angle() method, which makes it more obvious that the two methods are related, so it's easier to find.

I mean, at least it looks like something that has a mathematical sense. The angle() function itself is basically returning a float out of a vector, without the 3 points usually needed to define an angle. This is simply because it returns the angle part of it's polar coordinates.

I think that, if we implement the possibility to create a vector from an angle, then that's basically equivalent to building a vector from it's polar coordinates (returned by the angle() and lenght() function the other way around). It makes the API more consistent and easier to search.

@aaronfranke
Copy link
Member

without the 3 points usually needed to define an angle.

We don't need 3 points. The origin and X axis are implied.

When it comes to polar coordinates, this is just something that is super easy to do with existing methods (angle & length, and from_angle & multiply), while simultaneously being a very niche use case for games, so it doesn't make sense to have APIs for it.

@groud
Copy link
Member

groud commented Apr 28, 2021

The origin and X axis are implied.

Yep, that's basically the issue here. As explicit naming is better than implicit naming, I think from_polar is better than from_angle() as it is more explicit.

@Xrayez
Copy link
Contributor

Xrayez commented Apr 28, 2021

Compare:

Vector2.from_angle(radians) * length
Vector2.from_polar(radians, length)

I really do think it's not worth being explicit here. I'm for complementing angle() method rather than this.

@groud your opinion on this is basically the complete opposite of what you said in #43103 (comment):

Simply allow generating a random unit vector then let the user multiply it. That's a simple and efficient API.

The from_angle is "simple and efficient" API as well according to your comment. I understand that I'm taking this from other context, but it's very similar to what we have here.

@Xrayez
Copy link
Contributor

Xrayez commented May 4, 2021

Alternative solution could be:

  1. Merge this PR as-is;
  2. Implement new methods such as:
    • Geometry2D.polar_to_vector(theta, length)
    • Geometry3D.spherical_to_vector(theta, phi, length)

if really needed. Due to this, I've even considered to do the same thing in goostengine/goost#65, where using polar terminology would probably make more sense, since Geometry already implies that we deal with something related to space, contrary to Vector2/3 classes which are not always used in a geometry/spatial context.

Consequently, I think it would be more feature-complete to rename the function to from_polar(...) and to allow a second argument (that could be 1.0 by default), that would allowing providing a length too.

Being feature-complete can mean anything to a user. For instance, are we going to implement other conversion methods such cylindrical coordinate system as well? That would certainly make those features complete, but I'm afraid those methods could bloat Vector2/3 classes with features that nobody is going to use in their game projects.

That's why I'm suggesting that we implement coordinate system conversion methods in classes such as Geometry2D/3D (again, if needed), and merge this PR for the reasons already outlined above by @KoBeWi and @aaronfranke.

@Xrayez
Copy link
Contributor

Xrayez commented May 5, 2021

Surprise! Looks like I've found existing methods which do polar to cartesian conversion and vice versa, I had no idea we have those functions already, and I'm not sure whether you were aware of them either... 054a2ac.

The only problem is that polar2cartesian accepts length as first parameter, so we can't omit it to default 1.0.

That said, since we already have those functions, I humbly think that adding from_angle() still makes perfect sense to have as a Vector2 method to complement angle() and there's no need to rename it to from_polar(), because we already have polar2cartesian() for that as a GDScript method.

Again, quoting KoBeWi in this PR:

Also the function was never intended to have anything to do with polar coordinates (which very few people use). from_angle() has similarity to angle() method, which makes it more obvious that the two methods are related, so it's easier to find.

Cross-referencing those methods in the documentation should be worth it, still.

@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 0e9e17e to 4ed79ad Compare August 30, 2021 23:24
@reduz
Copy link
Member

reduz commented Aug 30, 2021

ready to merge when passes CI. Do not forget to remove polar/cartesian functions from variant_utility in a subsequent PR, as they are redundant and a confusing API for Godot users compared to this one.

Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

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

Works in my testing and looks good except for one style issue.

modules/mono/glue/GodotSharp/GodotSharp/Core/Vector2.cs Outdated Show resolved Hide resolved
@KoBeWi KoBeWi force-pushed the they_came_from_angle branch from 4ed79ad to 3f3739c Compare August 30, 2021 23:54
Copy link
Member

@aaronfranke aaronfranke left a comment

Choose a reason for hiding this comment

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

Looks good and works in my testing. I haven't tested the C# version but it looks good.

@vnen vnen merged commit cf59028 into godotengine:master Sep 1, 2021
@vnen
Copy link
Member

vnen commented Sep 1, 2021

Thanks!

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

Successfully merging this pull request may close these issues.

8 participants