0
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\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, 2018 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, 2018 at 2:23

2 Answers 2

1
\$\begingroup\$

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

\$\endgroup\$
-1
\$\begingroup\$

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

https://github.com/oxygine/oxygine-framework/commit/3d02d81e0b3851bab705aaea72d5a0375c9ea432

\$\endgroup\$
7
  • 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, 2018 at 14:25
  • 1
    \$\begingroup\$ I add example about what i mean \$\endgroup\$
    – Yahor10
    Jan 3, 2018 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\$
    – user35344
    Jan 3, 2018 at 21:59
  • \$\begingroup\$ How links can die for official documentation ? \$\endgroup\$
    – Yahor10
    Jan 4, 2018 at 15:34
  • \$\begingroup\$ that library may not exist forever. It's also not specific to his problem \$\endgroup\$
    – CobaltHex
    Jan 5, 2018 at 1:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .