I am developing a card game in HTML that shows real-time perspectives and card movements to all players.
All players are connected via socket.io.
BACKGROUND:
If there are 4 players, table is square and table HTML body is rotated based on player position on their screen.
The structure is like this:
<gameTable style=”position: relative;”>
<card style=”position: absolute; top: 1%, left: 1%;”></card>
</gameTable >
Now in order to move cards, player picks a card with mouse and it
1: Saves that location
2: Based on movement, it converts movement of card (in px), into relatively equal percentage and then moves the card on table that much.
This is so that whether someone is using big screen or small screen, the card movement will be same for all people by using % as top, left coordinates.
-- SQUARE TABLE:
When player is in 0 degree table position, the mouse and card movements are directly linked.
Card top: --mouse y
, card left: --mouse x
When player is in 180 degree table position, the mouse movements are reversed:
Card top: ++mouse y
, card left: ++mouse x
For 90 degree:
Card top: ++mouse x
, card left: --mouse y
Similar translation with small change of coordinates for 270 degree rotated table.
This works perfectly as with this translation, it perfectly translates mouse over all directions in code.
THE PROBLEM:
For a 6 player game, I want to use Hexagonal table. Now in this table, the players table rotate to:
0, 60, 120, 180, 240 and 300 degrees.
0 and 180 degree are fine but I am unable to figure out a proper formula to translate mouse coordinates to card coordinates based on rotation.
-- CURRENT SOLUTION (Not good)
Currently, I did some brute force programming and did it like this: (60 degree table)
If (mouse moving up / down) {
card top: --mouse y/1.7, card left: --mouse y
}
If (mouse moving left / right) {
card top: ++mouse x, card left: --mouse x/1.7
}
Similar approaches for other positions on hexa table with small changes.
This is brute force approach and it blocks me from transitioning from up/down movement to left/right state as I have to leave the mouse and then start again.
In short, it is not working and I would like to know if it is possible to have a formula that can correctly translate mouse coordinates of mouse to cards based on rotation.
Please view following images for visual representation of what I mean