Skip to content
Advertisement

How to rotate an image around its center while its scale is getting larger(in Pygame)

I loaded an image and I want it to rotate around its center, while its scale is getting larger. I know how to rotate an image around its center originally, but it’s hard for me to calculate the position if the scale is getting larger. I tried it, but the image just ‘dances’, not staying in the center.

Advertisement

Answer

Short answer:

Store the center of the source image rectangle and update the center of the rotated and zoomed image rectangle after the rotation and zoom operation, by the stored center position. Rotate and zoom the image by pygame.transform.rotozoom():

JavaScript

Long Answer:

An image (pygame.Surface) can be rotated by pygame.transform.rotate.

If that is done progressively in a loop, then the image gets distorted and rapidly increases:

JavaScript

This is cause, because the bounding rectangle of a rotated image is always greater than the bounding rectangle of the original image (except some rotations by multiples of 90 degrees).
The image gets distort because of the multiply copies. Each rotation generates a small error (inaccuracy). The sum of the errors is growing and the images decays.

That can be fixed by keeping the original image and “blit” an image which was generated by a single rotation operation form the original image.

JavaScript

Now the image seems to arbitrary change its position, because the size of the image changes by the rotation and origin is always the top left of the bounding rectangle of the image.

This can be compensated by comparing the axis aligned bounding box of the image before the rotation and after the rotation.
For the following math pygame.math.Vector2 is used. Note in screen coordinates the y points down the screen, but the mathematical y axis points form the bottom to the top. This causes that the y axis has to be “flipped” during calculations

Set up a list with the 4 corner points of the bounding box:

JavaScript

Rotate the vectors to the corner points by pygame.math.Vector2.rotate:

JavaScript

Get the minimum and the maximum of the rotated points:

JavaScript

Calculate the “compensated” origin of the upper left point of the image by adding the minimum of the rotated box to the position. For the y coordinate max_box[1] is the minimum, because of the “flipping” along the y axis:

JavaScript

To define a pivot on the original image, the “translation” of the pivot in relation to the upper left of the image has to be calculated and the “blit” position of the image has to be displaced by the translation.

Define a pivot e.g. in the center of the image:

JavaScript

Calculate the translation of the rotated pivot:

JavaScript

Finally calculate the origin of the rotated image:

JavaScript

If the image has to be additionally zoomed, then the zoom has to be taken into account when the origin of the image is calculated:

JavaScript

In the following example program, the function blitRotate does all the above steps and “blit” a rotated image to a surface. pos is the position of the image. originPos is the point on the image which is placed on pos and the pivot.


Minimal example: repl.it/@Rabbid76/PyGame-RotateAroundPivotAndZoom

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement