0

I am making a game where there are two players and they can shoot each other. Their movement will be defined by a rotation around a fixed point, the point will be(600, 300), which is the center of our screen. The player will keep rotating around the point as long as they are pressing a certain button(which is keep providing force to our player) else they will fall(due to gravity). I think it would help to think of it as a ball attached to a point using a string. The string is attached as long as a button is pressed and gets unattached as soon as the button is released and the ball flies off. Here is my player class

class Player:
    def __init__(self):
        self.pos = [500, 200]
        self.width = 30
        self.height = 30
        self.player = pygame.image.load("player.png").convert_alpha()
        self.player = pygame.transform.scale(self.player, (self.width, self.height))
        self.rect = self.player.get_rect()
        self.rotated_player = None
        self.anguler_vel = 0
        self.Fg = 0.05
        self.Fp = 0
        self.arm_length = 0

Fp is the force perpendicular to the force of gravityFg. Fg is the force which is pulling it down on our player. Fp is defined by math.sin(theta) * Fg. I am keeping track of Fp because i want the player to keep moving in the direction of rotation after its unattatched from the string. arm_length is the length of the string.

I have a Point class, which is the point about which our player will rotate. Here's the point class.

class Point:
    def __init__(self,x, y):
        self.pos = [x, y]
        dx = self.pos[0] - player.pos[0]
        dy = self.pos[1] - player.pos[1]
        self.angle = math.atan2(dy, dx)

Now, i need help with the actual rotation itself. I am aware that adding a certain value to the angle every single frame would make it go around. But how would i make it go around a certain point that i specify and how would the arm length tie into this?. I find that it is really difficult to implement all of this because the y-axis is flipped and all the positional values have to be scaled down when using them in calculations because of the FPS rate. Any help on how this is done would be appreciated as well. Thanks

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 1
    Don't use FPS, use the actual milliseconds real-time from `pygame.time.get_ticks()`. – Kingsley Feb 25 '20 at 21:43
  • See [How do I rotate an image around its center using PyGame?](https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame/54714144#54714144) and [How to rotate an image(player) to the mouse direction?](https://stackoverflow.com/questions/58603835/how-to-rotate-an-imageplayer-to-the-mouse-direction/58604116#58604116) – Rabbid76 Jan 05 '21 at 18:21

1 Answers1

0

When you use pygame.transform.rotate the size of the new rotated image is increased compared to the size of the original image. You must make sure that the rotated image is placed so that its center remains in the center of the non-rotated image. To do this, get the rectangle of the original image and set the position. Get the rectangle of the rotated image and set the center position through the center of the original rectangle. e.g.:

def rotate_center(image, rect, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = rect.center)
    return rotated_image, new_rect
screen.blit(*rotate_center(image, image_rect, angle))

Alos see How do I rotate an image around its center using PyGame? and How to rotate an image(player) to the mouse direction?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174