# How to import object with rotation?

**URL:** <https://discourse.mapeditor.org/t/how-to-import-object-with-rotation/3980>\
**Category:** Questions\
**Created:** [June 29, 2019, 5:06pm UTC](https://discourse.mapeditor.org/t/how-to-import-object-with-rotation/3980 "2019-06-29T17:06:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![laochun](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/laochun/32/2364_2.png) [@laochun](https://discourse.mapeditor.org/u/laochun)\
**Post date:** [June 29, 2019, 5:06pm UTC](https://discourse.mapeditor.org/t/how-to-import-object-with-rotation/3980/1 "2019-06-29T17:06:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![laochun](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/laochun/32/2364_2.png) [@laochun](https://discourse.mapeditor.org/u/laochun)\
**Post date:** [June 29, 2019, 7:43pm UTC](https://discourse.mapeditor.org/t/how-to-import-object-with-rotation/3980/2 "2019-06-29T19:43:13Z")

</div>

I found the solution :

```python
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))

```

---

<div class="post-metadata">

**Author:** ![bjorn](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.mapeditor.org/bjorn/32/1599_2.png) [@bjorn](https://discourse.mapeditor.org/u/bjorn)\
**Post date:** [June 29, 2019, 8:21pm UTC](https://discourse.mapeditor.org/t/how-to-import-object-with-rotation/3980/3 "2019-06-29T20:21:13Z")

</div>

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.
