I am working on a personal project and am trying to make a 2d tile map render that needs to handle rotation.

I am using raylib to handle the drawing stuff onto the screen and am using this function to draw a tile:

void DrawTexturePro(Texture2D texture, Rectangle sourceRec, Rectangle destRec, Vector2 origin, float rotation, Color tint);

How do I calculate the new coordinates of the tiles after the rotation and the individual rotation of each tile if I want to rotate the tile map?

I also need to figure how to do mouse click detection for tiles when they are rotated with their new coordinates and how to do ‘collision detection with cursor’ with rotated squares/tiles.

And due to the way the project is set up I cannot rotate the camera.

Extra info/diagram:

Extra info

  • \$\begingroup\$ When you say you have no means to rotate the camera, do you mean the entire rendering code from that point onwards is out of your control? Or do you have any way to alter the model-view-projection matrices for calculating the vertex positions? \$\endgroup\$ – user111144 Jan 3 '18 at 14:47
  • \$\begingroup\$ No I need other items being rendered to not turn with the grid so I do not want to alter the camera. \$\endgroup\$ – Mclog21 Jan 4 '18 at 2:23

You will need to transform the point by a rotation/transformation matrix.

Wikipedia has good information about using rotation matricies https://en.wikipedia.org/wiki/Rotation_matrix

θ is your angle
X,Y are your coordinates of your object (represented as a column vector/1D matrix)
X',Y' are your new, rotated coordinates

[X'] = [cosθ -sinθ][X]
[Y'] = [sinθ  cosθ][Y]

This is a small amount of matrix math equating to

X' = [cosθ * X] + [-sinθ * Y]
Y' = [sinθ * X] + [ cosθ * Y]

You can use transformation matrices for lots of operations such as translations, scaling, rotation, skewing, etc. They are widely used in 3D graphics


get rotation flag from tile,parse it and swap coordinates from your vertexes

Here good engine and good example where you can find how to do it


  • 2
    \$\begingroup\$ Please elaborate as to why you suggest this is a solution to the problem, and properly format your answer. \$\endgroup\$ – Stephan Jan 3 '18 at 14:25
  • 1
    \$\begingroup\$ I add example about what i mean \$\endgroup\$ – Yahor10 Jan 3 '18 at 14:59
  • 3
    \$\begingroup\$ Adding a link to an external site is not the way to go, as links die. This answer is not very useful in its current state. \$\endgroup\$ – Tyyppi_77 Jan 3 '18 at 21:59
  • \$\begingroup\$ How links can die for official documentation ? \$\endgroup\$ – Yahor10 Jan 4 '18 at 15:34
  • \$\begingroup\$ that library may not exist forever. It's also not specific to his problem \$\endgroup\$ – CobaltHex Jan 5 '18 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.