经过多次研究我没有找到答案,当我试图用 pytmx 在 pygame 中显示对象时,结果完全被破坏了,因为 x,y 随旋转而变化。我尝试使用矩阵旋转,但为此,我需要知道原始中心。我不知道怎么找到它,因为 Tiled 在旋转后发送给我,x,y...
所以我的目标只是用 pytmx 在 pygame 中显示对象图块。
import numpy
import math
angle = math.radians(-117.57) #rotation get with tiled, set - for cancel rotation
center_x = 148 #how to get this ?
center_y = 747 #and this
x = 126.82 #get with tiled
y = 679.54 #get with tiled
id_rotation = [ [math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)] ]
R = numpy.matrix(id_rotation)
id_position = [ [x - center_x],
[y - center_y] ]
B = numpy.matrix(id_position)
id_center = [ [center_x],
[center_y] ]
C = numpy.matrix(id_center)
print(numpy.dot(R, B) + C) #return original position before rotation
如果我只使用 pygame.transform.rotate:
if isinstance(layer, pytmx.TiledObjectGroup):
for object in layer:
if (object.image):
assets_surface = pygame.Surface((object.width, object.height), pygame.SRCALPHA)
assets_surface.blit(object.image, (0, 0))
assets_surface_rotate = pygame.transform.rotate(assets_surface, -object.rotation)
rdc.blit(assets_surface_rotate, (object.x, object.y))
对于瓷砖对象,我得到了错误的 x,y 位置: