Skip to content

new angle and angle_rad property in vector2 #3222

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

Merged
merged 13 commits into from
May 24, 2025

Conversation

AntoineMamou
Copy link
Contributor

This PR follows this one : #3216

I make the changes needed and add two properties :
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].

@AntoineMamou AntoineMamou requested a review from a team as a code owner November 12, 2024 17:43
Copy link
Member

@oddbookworm oddbookworm left a comment

Choose a reason for hiding this comment

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

Needs documentation, and there are a few issues I see offhand. But thanks for submitting! 🎉

src_c/math.c Outdated
pgVector *vec = (pgVector *)self;

if (vec->coords[0] == 0.0 && vec->coords[1] == 0.0) {
return PyFloat_FromDouble(0.0);
Copy link
Member

Choose a reason for hiding this comment

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

I’d rather an exception get raised here. The zero vector is special and getting it’s angle makes no sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I thought it should be 0 because of the initial issue #3195, but I can change and raise an exception

Copy link
Contributor

Choose a reason for hiding this comment

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

@oddbookworm
I strongly think the angle properties should be consistent with the existing behavior of .as_polar(), .angle_to(), .rotate() and more importantly python math.atan2() (which is based off the a C standard) and polar coordinates.
These functions don't raise errors and (for not nan args) don't return nan, and most actually do not document this special case.

Copy link
Member

@oddbookworm oddbookworm Nov 14, 2024

Choose a reason for hiding this comment

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

I still don't agree that we should silently just say the angle of the zero vector is zero. I don't like those functions treating that special case in the way they do either. And actually, the C standard does not guarantee that atan2(0, 0) returns 0. Here's a snippet from the C standard
image

link to standard I used

Copy link
Member

Choose a reason for hiding this comment

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

One reason I hate that behavior is because it can lead to very confusing behavior. Consider the case where you're just continuously scaling down a vector without changing it's direction. At some point, you'll go from a constant angle to 0, regardless of what the starting angle is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @aatle and @oddbookworm,
I saw on discord that there is many vision for what should be done for the zero vector. And I wanted to know if people agree on one of these in order to maybe change my code

Copy link
Contributor

@aatle aatle Nov 17, 2024

Choose a reason for hiding this comment

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

Hi @AntoineMamou, it has been settled that we will imitate python's math.atan2() function behavior. (Returns certain value depending on sign of zeroes, for zero vector.)
See https://github.com/python/cpython/blob/ed81971e6b26c34445f06850192b34458b029337/Lib/test/test_math.py#L327 for the relevant test cases.
Here's the old CPython 3.7.2 implementation for special cases: https://github.com/python/cpython/blob/v3.7.2/Modules/mathmodule.c#L566-L600
Recent CPython relies on a library, with atan2 implemented here:
https://github.com/rust-lang/libm/blob/master/src/math/atan2.rs#L20-L31

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @aatle, so if we want to imitate math.atan2(), for the case where x=y=0, it should return 0, right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And I'm using atan2 to find the angle, so basically I can skip the if statement ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe I'm missing something here

@yunline yunline added New API This pull request may need extra debate as it adds a new class or function to pygame math pygame.math labels Nov 13, 2024
@AntoineMamou
Copy link
Contributor Author

I see that two checks were not successful but I don't understand why. Can someone help me ?

Copy link
Contributor

@aatle aatle left a comment

Choose a reason for hiding this comment

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

LGTM.
Thanks for implementing my feature request.

Copy link

@LittleBread69 LittleBread69 left a comment

Choose a reason for hiding this comment

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

Legitimate.
Had no issues running it on MacOS 15.1 with python3.12 and python3.10 .

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

Hi, sorry for taking a while to get to this proposal.

Personally I really like the overall idea. Definitely +1 for keeping it as a property. A couple of things to discuss

  • Do we really need both angle and angle_rad? Yes, I can understand why it was done (consistency with other vector methods), but because this is a new property, we can consider dropping one of these. I'm just putting this idea up for discussion, personally have no opinion here
  • Why restrict it to read only property? I can imagine that setting an angle without changing magnitude would be a useful feature as well. Though that could be a separate follow up PR too.

Again, thanks for taking up this proposal and contributing to pygame-ce

@aatle
Copy link
Contributor

aatle commented Apr 24, 2025

To answer those questions,
Yes, we need both angle and angle_rad. Degrees is not droppable. Radians is very useful with many mathematical properties and a relation to trigonometric functions. Because of this, radians is often used instead of degrees in many places: physics engines, math/science, etc.
It has been implemented as a read-only property at @oddbookworm's request, the reasoning given on Discord as:

If you want to change the vector, you do it component-wise or use the other methods available.

I guess since it is a property, it may be appropriate to allow setting; however, there should be a non-inplace equivalent. This functionality can be always be added in a later PR though.

Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR 😎

@ankith26
Copy link
Member

Requesting a re-review from @oddbookworm here as they had previously reviewed it.

Update version to 2.5.5
Copy link
Member

@oddbookworm oddbookworm left a comment

Choose a reason for hiding this comment

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

LGTM! 🥳
Sorry it took me so long to get back to this, I've been super busy irl 😢

@oddbookworm
Copy link
Member

As soon as everything passes, I'll merge it

@oddbookworm oddbookworm merged commit b0fa4ab into pygame-community:main May 24, 2025
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
math pygame.math New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants