- Notifications
You must be signed in to change notification settings - Fork3.8k
Add .move_towards() for Vector2 and Vector3#2929
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
I noticed that |
andrewhong04 commentedDec 29, 2021 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Alright, I'm done with the importpygameimporttimestart=time.perf_counter()defvector2_move_towards(origin,target,speed:float):"""Moves towards the target Vector2 by the movement speed. Must be put in a loop until its reached its target. Parameters: origin: The original position target: The target position. speed: The movement speed. """delta=target-origindist=delta.magnitude()if (dist<=speedordist==0):returntargetreturnorigin+delta/dist*speedvec1=pygame.Vector2(10,20)vec2=pygame.Vector2(30,30)speed=1vec3=vector2_move_towards(vec1,vec2,speed)print(vec3)end=time.perf_counter()print(end-start) Here are the results: C:0.00005339999Python:0.00011449999874457717 Time to work on Edit: It looks like after I got |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This also needs unittests and type stubs
Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Lots of changes 😅
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Whoops, looks like I rushed things lol. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This PR is almost looking good to go! Before merge, you must runpython3 setup.py format and commit the autoformat changes, and all these commits can be squashed and merged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
🎉 👍 thanks :)
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There's a problem with input parsing >>>a=pygame.math.Vector2((0,0))>>>a.move_towards((10,10),2)<Vector2(1.41421,1.41421)>>>>b=pygame.math.Vector3(0)>>>b<Vector3(0,0,0)>>>>b.move_towards(a,2)<Vector3(0,0,1.42421e-306)>>>>a.move_towards((1,2,3,4,5,6,7,8),10)Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>TypeError:ExpectedVectorasargument1 If the dimensions aren't equal, it should fail, presumably And if the input is rejected, it shouldn't say it expected a Vector, since it can take vector-like. Pygame rect functions would call this "Rect-style," so that could be used here as "Vector2-style" or "Vector3-style" Also I'm putting in a commit that fixes a couple things with the example. |
Co-Authored-By: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
…alriptide/pygame into novial-move_towards"This reverts commit771df5e, reversingchanges made toe6e5992.
…com/novialriptide/pygame into novial-move_towards""This reverts commit704a1b2.
Formatting
This reverts commitd702922.
This reverts commit3ea6a3f.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
| target=Vector2(12.30,2021.0) | ||
| change_ip=Vector2(7.22,2004.0) | ||
| change=origin.move_towards(target,-3) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
feels like contradicting: move_towards but actually it moves away with the negative
resolve at will
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It's a pretty good point. Even though we've gone over naming this method more than anything else in the story of pygame, and perhaps any method named anywhere ever...
Did we consider "to"?v.to(v2, -1.1).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I left some suggestions. No need to change them in this PR or at all. Approving anyway.
Thanks! I'll be looking to resolving these later today. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Finally approving this PR 🥳
There is a CI fail but it's unrelated.
Also, side note to the person merging the PR, squash and merge, and remember to remove co-author tags while doing so, so that me and starbuck don't come as PR authors here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
👍 thanks for this, and for being such a good sport about all the naming conversations.
VegaPac commentedNov 20, 2023
As of 2023 it works perfectly and never gives any problem. Good job guys. |
Uh oh!
There was an error while loading.Please reload this page.
Fix#2905