So I am making a chess-esque game. The game was initially planned to be built left to right but recent review with friends revealed that they want an ability to rotate at will.
So initially I thought I could just look at the relationship of positions on the board and simply math out what the rotation would look like. It simply looks like it would be the difference from the center and then inverting X and Y positions.
For instance, on an 8 by 8 a unit that is on 4,4 would rotate to 5,4 then 5,5 and finally 5,4.
Similarly, a unit on 3,4 is going to rotate to 4,6 then 6,5 and finally 5,3.
In an 8 by 8 board the top-left of the dead center is 5,5. So we can say:
CenterX or Cx = 5
CenterY or Cy = 5
Distance of unit’s origin (top-left X) from CenterX or Dx = x - Cx
Distance of unit’s origin (top-left Y) from CenterY or Dy = y - Cx
RotatedX or NewX or Nx= Cx + Dy
RotatedY or NewY or Ny = Cy - Dx - 1
So let’s look at the first example:
(4,4) → (4,5) → (5,5) → (5,4)
When X = 4 and Y = 4, Dx = 4 - 5 = -1 and Dy = 4 - 5 = -1
Nx = Cx + Dy = 5 - 1 = 4
Ny = Cy - Dx - 1 = 5 + 1 - 1 = 5
(4,5)
When X = 4 and Y = 5, Dx = 4 - 5 = -1 and Dy = 5 - 5 = 0
Nx = Cx + Dy = 5 - 0 = 5
Ny = Cy - Dx - 1 = 5 + 1 - 1 = 5
(5,5)
When X = 5 and Y = 5, Dx = 5 - 5 = 0 and Dy = 5 - 5 = 0
Nx = Cx + Dy = 5 - 0 = 5
Ny = Cy - Dx - 1 = 5 + 0 - 1 = 4
(5,4)
When X = 5 and Y = 4, Dx = 5 - 5 = 0 and Dy = 4 - 5 = -1
Nx = Cx + Dy = 5 - 1 = 4
Ny = Cy - Dx - 1 = 5 + 0 - 1 = 4
(4,4)
Similarly, second example would fit the rule as well:
(3,4) → (4,6) → (6,5) → (5,3)
When X = 3 and Y = 4, Dx = 3 - 5 = -2 and Dy = 4 - 5 = -1
Nx = Cx + Dy = 5 - 1 = 4
Ny = Cy - Dx - 1 = 5 + 2 - 1 = 6
(4,6)
When X = 4 and Y = 6, Dx = 4 - 5 = -1 and Dy = 6 - 5 = 1
Nx = Cx + Dy = 5 + 1 = 6
Ny = Cy - Dx - 1 = 5 + 1 - 1 = 5
(6,5)
When X = 6 and Y = 5, Dx = 6 - 5 = 1 and Dy = 5 - 5 = 0
Nx = Cx + Dy = 5 + 0 = 5
Ny = Cy - Dx - 1 = 5 - 1 - 1 = 3
(5,3)
When X = 5 and Y = 3, Dx = 5 - 5 = 0 and Dy = 3 - 5 = -2
Nx = Cx + Dy = 5 - 2 = 3
Ny = Cy - Dx - 1 = 5 + 0 - 1 = 4
(3,4)
OK now that I have proven that this can work - I am wondering if there is a more elegant way of doing this. The reason why I say this is because:
- The game will have online play and two people can be in different orientation
- Currently only pawn is angle dependent (all other units would have same movement irrespective of angle). Pawn can only move right or left or up or down depending on orientation. So I would need to start doing movements based on angle