delicious goat
delicious goat

Reputation: 47

Flappy Bird Rotation Algorithm

I am writing a flappy bird clone with c++ and DirectX. I am basically finished, except for the algorithm for the rotation. I have one now (rotation = ((90 * (yVelocity+10) / 25) - 90)/2;), but it does not act the same way as the original flappy bird. I am trying to replicate the original flappy bird's rotation as closely as possible so any help would be appreciated.

Upvotes: 1

Views: 1036

Answers (4)

while0equals0
while0equals0

Reputation: 1

if ySpeed < smallPositiveValue:
    angle = 15 
else:
    helpAngle = 90 * sin((-ySpeed * (pi/2 * maxYSpeed)))
    if helpAngle < angle: angle += 0.1 * (helpAngle - angle)

This makes it look pretty much like the original Flappy Bird.

What it does:

  • flappy looks up a bit if rising and falling slightly

  • flappy smoothly comes close to a 90 degree rotation with increasing ySpeed

Upvotes: 0

self.temporary is a copy of the current sprite.

""" Bird object """
class Bird(pygame.sprite.Sprite):
    def __init__(self, color):
        super().__init__()

        # sounds
        self.flap = pygame.mixer.Sound('wing.wav')
        self.crash = pygame.mixer.Sound('crash.wav')
        
        # flapping sprites
        self.sprites = []

        for i in [1, 2, 3]:
            temp = pygame.image.load(color + str(i) + ".png").convert_alpha()
            temp = pygame.transform.scale2x(temp)
            self.sprites.append(temp)
            
        # current sprite
        self.image = self.sprites[0]

        # temporary sprite
        self.temporary = self.image

        # current rectangle
        self.rect = self.image.get_rect(center=(BIRD_X, HEIGHT/2))

        # current frame
        self.frame = 0

        # movement amount
        self.movement = 0

Upvotes: 0

    def fall(self):
        # bird falls with accelerated motion
        self.movement += GRAVITY
        self.rect.centery += self.movement

    def rotate(self):
        # factor is positive when falling, therefore rotation is clockwise
        # factor is negative when raising, rotation is counter-clockwise
        factor = -2
        factor *= self.movement

        self.image = pygame.transform.rotozoom(self.temporary, factor, 1)

Upvotes: 0

Oleks G
Oleks G

Reputation: 104

I would make it equal to the original yVelocity but cap it at 2 numbers. Something like

rotation = min(topClamp, max(bottomClamp, yVelocity));

You might want to play around with this for a bit but this will make the rotation rely on the yVelocity but if the player is constantly going up then the rotation will be clamped at some number and the bird will just be looking up like in the original game.

Upvotes: 1

Related Questions