Image Rotates In The Opposite Direction Of a pymunk Body

68 views Asked by At

I am trying to make a simple fighter game that has a fish as your character I'm Relatively new to pymunk and have little to no experience on rotating sprites

this is my code :


__docformat__ = "reStructuredText"

import random
import math
import pygame

import pymunk
import pymunk.pygame_util
from pymunk import Vec2d


def main():
    pygame.init()
    screen = pygame.display.set_mode((1067, 600))
    clock = pygame.time.Clock()
    running = True
    direct = 0
    ### Physics stuff
    space = pymunk.Space()
    space.gravity = (0.0, 900.0)
    draw_options = pymunk.pygame_util.DrawOptions(screen)

    fish = pygame.image.load('fish.png')
    ### walls and ground
    static_lines = [
        pymunk.Segment(space.static_body, (0, 550), (1067, 550), 10),
        pymunk.Segment(space.static_body, (0, 0), (0, 550), 10),
        pymunk.Segment(space.static_body, (1067, 0), (1067, 550), 10)
    ]
    for line in static_lines:
        line.elasticity = 0.7
        line.group = 1
        line.friction = 1000
    space.add(*static_lines)

    fp = [(7, -10), (-7, -10), (-30, -7),(-40, 0), (-30, 7), (-7, 10), (7, 10), (40, 0)]
    mass = 100
    moment = pymunk.moment_for_poly(mass, fp)

    playerbody = pymunk.Body(mass, moment)
    playerbody.position = 533, 400
    playershape = pymunk.Poly(playerbody, fp)
    space.add(playerbody, playershape)

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                running = False

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_a:
                direct -= 1

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_d:
                direct += 1


            elif event.type == pygame.KEYUP and event.key == pygame.K_a:
                direct = 0

            elif event.type == pygame.KEYUP and event.key == pygame.K_d:
                direct = 0


            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                playerbody.apply_impulse_at_world_point( (0,-40000), (playerbody.position[0], playerbody.position[1]) )

        if direct > 1:
            direct = 1
        if direct < -1:
            direct = -1
        if direct == 1:
            playerbody.apply_force_at_local_point((200000, 0), (0, 0))
            playerbody.apply_force_at_local_point((0, -2900), (-30, 0))
        if direct == -1:
            playerbody.apply_force_at_local_point((-200000, 0), (0, 0))
            playerbody.apply_force_at_local_point((0, -2900), (30, 0))
        ### Clear screen
        screen.fill(pygame.Color("white"))

        ### Draw stuff
        space.debug_draw(draw_options)

        ### Update physics
        dt = 1.0 / 60.0 / 5.0
        for x in range(5):
            space.step(dt)


        angle_degrees = math.degrees(playerbody.angle)
        rotated_logo_img = pygame.transform.rotate(fish, angle_degrees)

        offset = Vec2d(*rotated_logo_img.get_size()) / 2
        #playershape = playershape - offset << commented because when I uncomment it gives an error

        screen.blit(rotated_logo_img, (round(playerbody.position[0] - 35), round(playerbody.position[1] -9)))


        ### Flip screen
        pygame.display.flip()
        clock.tick(50)
        pygame.display.set_caption("fps: " + str(clock.get_fps()))


if __name__ == "__main__":
    main()

yes I know its not the best but I have only only 2 years of python experience. I tried flipping the sprite both through code and just flipping the image source but none seemed to work.

1

There are 1 answers

2
Rabbid76 On

The y-axis of the coordinate system points downwards, however, pygame.transform.rotate() rotates counter clockwise. Therefore you have to invert the rotation angle:

rotated_logo_img = pygame.transform.rotate(fish, angle_degrees)

rotated_logo_img = pygame.transform.rotate(fish, -angle_degrees)

To rotate an image around the center you must preserve the center of the image (See also How do I rotate an image around its center using Pygame?):

angle_degrees = math.degrees(playerbody.angle)
rotated_logo_img = pygame.transform.rotate(fish, -angle_degrees)
fis_rect = fish.get_rect(center = playerbody.position)
rotated_loge_rect = rotated_logo_img.get_rect(center = fis_rect.center) 
screen.blit(rotated_logo_img, rotated_loge_rect)