-
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Method to normalize zero vectors #2269
Comments
Mathematically speaking, it's impossible to normalize a zero vector, so returning a zero vector would be weird. That's why, I don't agree with you. |
Well, I don't agree in the sense that just because something doesn't "make sense mathematically" means that a more convenient method shouldn't exist. But also, that's why the method is called |
I would be in support of having this method added. In nearly ever instance of me using normalize() I use a zero vector if I can't normalize. |
I like the idea of having a way to conveniently get the zero vector back when normalizing, the proposed method's name is quite clear in what it would do and thus I don't see how it wouldn't make sense mathematically either, you either can normalize or you return a zero vector. An alternative idea would be to add an optional argument to |
Perhaps |
I'm not sure if pygame needs this to be honest, especially if there is already a super simple solution to this problem of getting a potential Another example: >>> from pygame import Vector2
>>>
>>> vec_a = Vector2(5, 0)
>>> vec_a.normalize()
Vector2(1, 0)
>>>
>>> vec_b = Vector2(0, 0)
>>> vec_b.normalize()
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
vec_b.normalize()
ValueError: Can't normalize Vector of length Zero
>>>
>>> vec_a_norm_or_zero = vec_a and vec_a.normalize()
>>> vec_a_norm_or_zero
Vector2(1, 0)
>>>
>>> vec_b_norm_or_zero = vec_b and vec_b.normalize()
>>> vec_b_norm_or_zero
Vector2(0, 0) |
I think however we should have prebuilt Vectors (Like in Unity I think), like Vector2.zero |
There's no need for such method, you can already do |
Aside from vectors supporting boolean checks, you can get a zero vector by simply instantiating a vector: |
These methods work, however they are either cumbersome to write when |
To clarify, |
Yes the short-circuiting solution is a good solution as of now, but I think it can be made clearer with the implementation of a |
Just want to point out, if we did implement this, would we also want to do something similar with |
At the beginning, I was against this idea because it's scientifically wrong. So I checked how it was done on other game engine like Unity and godot, and I found that they both return a zero vector, which joins the idea of the original message. So if anyone is looking at this issue and wants to implement this feature, this is the way to go imo. |
I'd love to take this issue :). It'd be my first contribution to pygame-ce (been wanting to contribute to it for a while) and second contribution overall. Might take a while, though. |
Hi @Doublestuf , Sure you can do it ! The code you need to modify should be in |
I'm still not convinced that this is necessary. It may be what bona-fide game engines do, but pygame-ce is not a game engine. There are ways I can think of that being able to "normalize" the zero vector could go pretty wrong because you're expecting the result to be nonzero in your calculations. IMO implementing this just moves the problem from normalizing to wherever else you use the resulting normalized vector. You still need to be cognizant of the case you have the zero vector, so I don't see any benefit to this in general |
Note that the behavior of |
I still think a separate method is the way to go here. That way the original functionality is unchanged, while providing a more convenient option for situations like normalising input, or maybe getting an offset direction for AI might end up in the player. |
Description
When normalizing a vector, it is necessary to first check it is not of length zero, otherwise the program will crash.
The game engine bevy has a method for its
Vec
s that isnormalize_or_zero()
that allows you to normalize a vector that is potentially of length zero, where it returns a zero vector if that is the case, otherwise normalizing it.I think this would be useful to have in pygame, as it saves having to type an extra line pretty much every time you want to normalize a vector.
The text was updated successfully, but these errors were encountered: