1
\$\begingroup\$

I have an image that I'm rotating and moving Original position

And when the rotation angle is negative the image moves as expected. Negative angle

But when the angle is positive it moves it's origing point to the left Positive angle

Can anybody explain this behavior, and how to solve it?

Here is the code

import pygame, sys
import math

screen = pygame.display.set_mode((700, 400))
wrench = pygame.image.load('prueba.bmp')
white = (255,255,255)


ox = 20
oy = 32
running = True
posX = 100 - ox
posY = 100 - oy


pygame.init()
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running=0
        screen.blit(wrench, (posX, posY))
    if(pygame.time.get_ticks() <= 20000):
        screen.fill(white)
        t = pygame.time.get_ticks() / 1000
        ang = math.degrees(math.pi / 2 * math.cos(math.pi * t))
        #ang = math.pi / 2 * math.cos(math.pi * t)
        print(ang)
        posX = 100 + (50 * math.cos(math.pi * t)) - ox 
        posY = 100 + (20 * math.sin(math.pi * t)) - oy
        rotated_wrench = pygame.transform.rotate(wrench, ang)
        screen.blit(rotated_wrench, (posX, posY))
    pygame.display.flip()
\$\endgroup\$
6
  • \$\begingroup\$ What is the expected behavior? Should it move like a real wrench? \$\endgroup\$
    – skrx
    Commented Mar 7, 2018 at 3:54
  • \$\begingroup\$ No, what I dont understand is why its origin point moves so much when the angle is positive \$\endgroup\$
    – Mntfr
    Commented Mar 7, 2018 at 3:56
  • \$\begingroup\$ And what behavior do you expect? \$\endgroup\$
    – skrx
    Commented Mar 7, 2018 at 4:08
  • \$\begingroup\$ If you want to rotate around an anchor point other than the center, I can show you an example. \$\endgroup\$
    – skrx
    Commented Mar 7, 2018 at 8:10
  • \$\begingroup\$ Did you manage to fix the issue? \$\endgroup\$
    – Leo
    Commented Mar 8, 2018 at 21:49

1 Answer 1

1
\$\begingroup\$

This line is setting the X coordinate of the bliting:

posX = 100 + (50 * math.cos(math.pi * t)) - ox

The cosine is positive from 0 to 90 degrees and 270 to 0, which is the right side of a circle; and negative otherwise.

This is making the value to oscillate between 100 + 50 - ox and 100 - 50 - ox

If you want to keep the horizontal position to not oscillate in both directions, you should use the absolute value:

posX = 100 + (50 * math.fabs(math.cos(math.pi * t))) - ox
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .