Hi, my question is really simple, how to import object with rotation in python ?
Because the x,y change with rotation, i don’t know how to do ?
Thanks
Hi, my question is really simple, how to import object with rotation in python ?
Because the x,y change with rotation, i don’t know how to do ?
Thanks
I found the solution :
elif isinstance(layer, pytmx.TiledObjectGroup):
for Object in layer:
if (Object.image):
if Object.rotation != 0:
angle = math.radians(-Object.rotation)
center_x = Object.centerX
center_y = Object.centerY
Object_y = Object.y + Object.height
id_rotation = [ [math.cos(angle), -math.sin(angle)],
[math.sin(angle), math.cos(angle)] ]
R = numpy.matrix(id_rotation)
id_position = [ [Object.x - center_x],
[Object_y - center_y] ]
P = numpy.matrix(id_position)
id_center = [ [center_x],
[center_y] ]
C = numpy.matrix(id_center)
position_without_rotation = numpy.dot(R, P) + C
no_rotation_x = position_without_rotation[0]
no_rotation_y = position_without_rotation[1] - Object.height #Repere Tiled pas le meme que Pygame
Object_surface = pygame.Surface((Object.image.get_rect()[2], Object.image.get_rect()[3]), pygame.SRCALPHA)
Object_surface.blit(Object.image, (0, 0))
Object_surface_scale = pygame.transform.scale(Object_surface, (round(Object.width), round(Object.height)))
Object_surface_rotate = pygame.transform.rotate(Object_surface_scale, -Object.rotation) #Pygame va en anti horaire
extra_x = (Object_surface_rotate.get_rect()[2] - Object.width) / 2
extra_y = (Object_surface_rotate.get_rect()[3] - Object.height) / 2
rdc.blit(Object_surface_rotate, (no_rotation_x - extra_x, no_rotation_y - extra_y))
else:
Object_surface = pygame.Surface((Object.image.get_rect()[2], Object.image.get_rect()[3]), pygame.SRCALPHA)
Object_surface.blit(Object.image, (0, 0))
Object_surface_scale = pygame.transform.scale(Object_surface, (round(Object.width), round(Object.height)))
rdc.blit(Object_surface_scale, (Object.x, Object.y))
The rotation of tile objects in Tiled is applied with bottom-left origin for historical reasons. This means that when rotating objects around their center (like with the selection tool), indeed their position needs to change.
I’m glad you already found a solution to render your objects correctly, and thanks for sharing it with us!
I hope to get around to supporting configurable tile object origin in the near future, in which case you should be able to set the origin to “Center” to simplify your math.