Godot: Proposal: Adding cartesian to polar conversion functions

Created on 7 Dec 2016  路  7Comments  路  Source: godotengine/godot

Bugsquad edit: Repurposing this issue based on https://github.com/godotengine/godot/pull/7285#issuecomment-272994312

@tagcup wrote:

Conversion between degrees and a general 2D vector doesn't make any sense to me, in mathematical terms: the norm of a 2D vector isn't necessarily equal to 1. You can't convert between two degrees of freedom (Vector2) and one degree of freedom (an angle) in a meaningful way.

I would rather propose adding functions that converts between polar and cartesian coordinates in 2D: (x,y) <-> (r,胃) where 胃 is in radians. The API could be something like cartesian_to_polar(x,y) and polar_to_cartesian(r,胃), each returning a vector.

Or alternatively, simply a polar constructor for Vector2.


Original proposal:

I find that a lot of users (me included) have the need to convert angles (in radian or degrees) into a Vector2 direction.

The current way to get a Vector2 direction from an angle;

  var radian_value = PI # 180 degrees
  var my_vec2 = **Vector2(cos(radian_value), sin(radian_value))**

I propose that we add new functions such as rad2vec(rad_value) and deg2vec(deg_value) with the other functions found at the core.

This will make the process much cleaner for everyone, and we wont have to know/remember this formula. :)

feature proposal junior job gdscript

Most helpful comment

@viibr Or maybe it isn't? IDK I always try a few times until it fits the expected response... :laughing:
Anyway, I agree that this would be a nice helper to have in @GDScript.

Actually, that's a pretty-much "junior job", as it is pretty simple to implement (though it should be done in vscript as well). The main changes for GDScript would be in gd_functions.cpp/.h, and for VScript, they would be in all places and switches the TEXT_CHAR enum (for example) is found.

All 7 comments

Note that usually using Vector2(1,0).rotated(rad) is pretty clean, as long as you get the x and y of the "up" vector correct...

Isn't it Vector2(0,1).rotated() ?

Not as clear and efficient as rad2vec (which I ended up writing myself as a helper too).

@viibr Or maybe it isn't? IDK I always try a few times until it fits the expected response... :laughing:
Anyway, I agree that this would be a nice helper to have in @GDScript.

Actually, that's a pretty-much "junior job", as it is pretty simple to implement (though it should be done in vscript as well). The main changes for GDScript would be in gd_functions.cpp/.h, and for VScript, they would be in all places and switches the TEXT_CHAR enum (for example) is found.

I'd like to try my hand at this one. New to the project (and github in general), but it's in line with a hobby I've adopted recently (game design). Seems like as good a place as any to start!

@jxfaith Good luck -- even if you forget to do some part of it, we would discuss it in the PR :wink:

Solved some time ago by 054a2ac579c4893f46b680b1cb0d2460aa3d9140.

Was this page helpful?
0 / 5 - 0 ratings