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.angle and Vector2.angle_rad properties #3195

Open
aatle opened this issue Oct 27, 2024 · 5 comments
Open

Add Vector2.angle and Vector2.angle_rad properties #3195

aatle opened this issue Oct 27, 2024 · 5 comments

Comments

@aatle
Copy link
Contributor

aatle commented Oct 27, 2024

Proposal:
Add properties angle and angle_rad to pygame.Vector2 with getter and possibly a setter.

vec.angle_rad == math.atan2(vec.y, vec.x) == vec.as_polar()[1]  # note: no vec.angle_rad_to() method
vec.angle == -vec.angle_to([1, 0]) == degrees(vec.angle_rad)

(These angles are normalized: degrees within interval (-180, 180], even if set to an angle outside this range.)
The zero vector has an angle of 0.

Reasons:
In math, the angle of a vector is often relative to the positive x-axis (increasing angle from 0 goes towards positive y-axis).
For example, polar coordinates and trigonometry functions are connected to vectors through their angle to positive x-axis.
vec == Vector2(r * cos(theta), r * sin(theta))

The current ways to get these angles are indirect and less readable, for such an (IMO) important aspect of vectors. See above. In as_polar() result, the length is available via vec.length, but the angle is not directly available.

In a codebase, it's convenient to only have to store one float (angle to +x axis) to mark the direction of something, rather than another vector (especially when the distance between two points is unnecessary/inapplicable). Many physics libraries or engines use angles like this heavily, usually using radians. Even if angles are technically only relative, to store them conveniently there has to be 'absolute' angles.

Example uses:

Calculate the angle of one position to another position, in degrees.

# Before
self.angle = (vec2 - vec1).angle_to([1, 0])
# After
self.angle = (vec2 - vec1).angle

Find the angle between two vectors, in radians.

# Before
self.angle_rad = math.radians(vec1.angle_to(vec2))
self.angle_rad = vec2.as_polar()[1] - vec1.as_polar()[1]
# After
self.angle_rad = vec2.angle_rad - vec1.angle_rad

Rotate a vector in-place at the direction of a given angle (from +x axis), in degrees.

# Before
vec.rotate_ip(angle - vec.angle_to([1, 0]))
# After
vec.angle = angle
@AntoineMamou
Copy link
Contributor

AntoineMamou commented Nov 6, 2024

Hi @aatle,
Can I work on this issue ? I understand that these two functions : angle_rad and angle shoul be add in pygame.Vector2.
angle_rad: Returns the vector’s angle in radians relative to the positive x-axis.
angle: Returns the angle in degrees, normalized to [180,180].

Optional setters: Allow setting angle (in degrees) or angle_rad (in radians) to change the vector's direction, normalizing values outside their intervals.

Tell me if I don't understand the issue.

@aatle
Copy link
Contributor Author

aatle commented Nov 7, 2024

@AntoineMamou, sure you can work on this feature request, anyone can work on it.
Since this issue is a feature request, it isn't yet certain whether it will be added.

@AntoineMamou
Copy link
Contributor

@aatle, I began to add the two functions angle_rad and angle and I also added the tests needed. Since it's a feature, should I make a PR ?

@damusss
Copy link
Member

damusss commented Nov 7, 2024

I began to add the two functions angle_rad and angle and I also added the tests needed. Since it's a feature, should I make a PR ?

Yeah you usually make a pull request for that. Also, the issue was requesting properties (Vector2.angle), not functions, but at the same time it's your PR so you're free.

@aatle
Copy link
Contributor Author

aatle commented Nov 8, 2024

About property vs methods:
I think pygame should start using more properties rather than get [set] methods.
Intuitively, the angle and length of a vector are 'attributes' of it, similar to its x and y attributes, so it is more pythonic. Methods should be for operations. (Though, vector length and magnitude are methods.)

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

No branches or pull requests

3 participants