0

I've got a little problem that I can't seem to solve. I can't find it on stackoverflow yet.

It's about rotating an image. I've got two 'gamepieces' in the example here, where I can show the problem. If I click on one line, it rotates 90 degrees. But after it rotated 180 degrees (which is precise, because I made sure it ends on exactly 90, 180, 270 etc degrees) the images don't connect well anymore. if I rotate it another 180 degrees, then it's fine again..

Screen 1, nothing bad here:

Screen 2, here you see the two lines don't connect well.

I tried changing the resolution to different pixel sizes (64, 64) and (81, 81) for example. But this does not cure the problem.

And here is part of the code:

class Piece:
def __init__(self, piece, x, y):
    self.image = pygame.image.load(piece)
    self.rotated_image = self.image
    self.rect = self.rotated_image.get_rect()
    self.rect.center = x, y

    self.current_degrees = 0
    self.degrees = 0

    self.values = [0, 1, 0, 1]

def rotate_piece(self):
    self.rotated_image = pygame.transform.rotozoom(self.image, lerp(self.current_degrees, self.degrees, 0.3), 1)
    self.rect = self.rotated_image.get_rect(center=self.rect.center)
    self.current_degrees = lerp(self.current_degrees, self.degrees, 0.3)

def rotate_values(self):
    temp = self.values[0]
    for i in range(len(self.values) - 1):
        self.values[i] = self.values[i+1]
    self.values[3] = temp
    print(self.values)

pieces = []

piece1 = Piece(images['line'], 32, 32)
piece2 = Piece(images['line'], 96, 32)
pieces.append(piece1)
pieces.append(piece2)
PeterH
  • 1
  • btw: `rotate_values()` can be done in one line: `self.values.append(self.values.pop(0))` – furas Dec 11 '16 at 18:40
  • you use float so result can be imprecise - ie. `0.1 + 0.2 == 0.3` gives `False`. `'{:.54f}'.format(0.3)` gives `'0.299999999999999988897769753748434595763683319091796875'` and `'{:.54f}'.format(0.1+0.2)` gives `'0.300000000000000044408920985006261616945266723632812500'` – furas Dec 11 '16 at 18:53
  • Ah.. so, what would be best to use in this case? The 'decimal'-module? I'm not asking this for specifically this case, but for general cases as well :-) – PeterH Dec 12 '16 at 09:05
  • Hmm, tried. But same problem occurs if I rotate the images with 90 degrees directly. Without using interpolation. Thanks for the one-line solution btw. – PeterH Dec 12 '16 at 09:21
  • if there is problem with 90 or 180 directly then I don't have solution. Normally you could use integers multiplied by 10 - `0-1800` instead of `0-180` - and step `3` instead of `0.3`, and divied by 10 only when you need to use degrees in function. People for small numbers of steps create all images in some Graphic Editor and use this images instead function - ie. https://www.spriters-resource.com/resources/sheets/45/47719.png – furas Dec 12 '16 at 11:32
  • Fixed it by using rotate instead of rotozoom... Me happy but not getting it. – PeterH Dec 12 '16 at 14:40
  • @PeterH my guess would be that `rotozoom` introduces floating-point error somehow, because it's zooming _and_ rotating at the same time. By the way, have you tried putting the two lines one on top of the other instead of next to each other? To see if it's actually a rotation issue, because it almost looks like the line on the right in your example is translated upwards. – Sean Bone Dec 14 '16 at 13:33

0 Answers0