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

pygame.transform.rotate_center #3378

Open
ScriptLineStudios opened this issue Mar 10, 2025 · 7 comments
Open

pygame.transform.rotate_center #3378

ScriptLineStudios opened this issue Mar 10, 2025 · 7 comments
Labels
enhancement New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform

Comments

@ScriptLineStudios
Copy link
Member

ScriptLineStudios commented Mar 10, 2025

pygame.transform.rotate_center(surface: Surface, angle: float) -> Surface

Rotating an image around it's center is fairly common, it could be nice to have it as part of the transform API. If we can come to an agreement on whether this should be added or not, I've got a draft PR ready to go.

@damusss
Copy link
Member

damusss commented Mar 10, 2025

Can you show me a result surface rotating normally and a result surface rotating like this? in my mind they would produce the same result, so I'm having a hard time justifying this in my mind

@ScriptLineStudios
Copy link
Member Author

@damusss

Image

@damusss
Copy link
Member

damusss commented Mar 10, 2025

oh wow, damn, wow, I support this, sure

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform labels Mar 10, 2025
@gresm
Copy link
Contributor

gresm commented Mar 10, 2025

Mark the corners of your sprite and you'll see why it behaves like this. @ScriptLineStudios I have no clue how would you like it to work. Cut out the parts that are outside the original rect? Why would it be useful? This works in your case only because the image is closely shaped to a circle...

@damusss
Copy link
Member

damusss commented Mar 10, 2025

@gresm you have a point, and additionally, isn't the sprite on the left being slightly cut off? like when the horizontal parts need to start rotating, or is it just me? when I first saw it I was a bit mesmerized, but I think that to achieve something close to the image of the left without cutting out anything one can use the Mask class and find the bounding rect and then cut, or cut just like you said.

@aatle
Copy link
Contributor

aatle commented Mar 10, 2025

Technically pygame.transform.rotate() and similar already rotate around the center. I think rotate_center would be a bit confusing.

The suggested function would be more like a "cropped" version of pygame.transform.rotate() with the behavior that it always preserves size, which works best on square surfaces. Note that for more rectangular surfaces, much would be cut out for certain angles. Also note that corner padding would be necessary for most angles.

pygame.transform.rotate_cropped(surface: Surface, angle: float) -> Surface

@bigwhoopgames
Copy link

If we are going to expand on rotate I would just make a generic rotate_on_pivot function where the pivot point can be changed as needed.

This is what I use, its a bit different in that it returns a rect as well but that rect can be used to position the image correctly after rotation around any pivot / origin point. Caching can be optional.

rot_cache = {}
def rotate_on_pivot(image: pygame.Surface, angle: float, pivot: Vector2, origin: Vector2) -> tuple[pygame.Surface, pygame.Rect]:
    
    lookup = (image, angle)
    
    if lookup in rot_cache:
        surf = rot_cache[lookup]
    else:
        surf = pygame.transform.rotate(image, angle)
        rot_cache[lookup] = surf
        
    return surf, surf.get_rect(center = ((origin - pivot).rotate(-angle) + pivot))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New API This pull request may need extra debate as it adds a new class or function to pygame transform pygame.transform
Projects
None yet
Development

No branches or pull requests

5 participants