Suppose that in 2D I have a polygon consists of square cells on a grid, much like a tetronimo but with an arbitrary number of cells that form it. The shape is orthogonally continuous, as in every cell connects to at least one other cell in an orthogonal direction. Here are some examples:
In the project I am working on, these blocks can be moved with the arrow keys in multiple directions around multiple pivot points. For example, this shape below can rotate around any of 4 directions by pressing, in order of rotations shown, the left, down, right and up arrow keys. The shape rotates around the pivot points shown in red.
I would like to add "drag-to-move" support. In other words, you press your finger down onto any point upon the shape, and as you drag and rotate your finger around a pivot point, the shape will rotate with it accordingly. My problem is that I do not know how to go about programatically finding the pivot point to rotate around, or the direction of rotation, from the path of the player's finger alone.
In code, I store a list of pivot points as Vector3s. These Vector3s store the following information:
X-component: x-pos of pivot
Y-component: y-pos of pivot
Z-component: -1 or 1, direction of rotation (1 for clockwise, -1 for anti-clockwise)
For clarity, the Z-component determines which direction the shape can be rotated about a pivot if it is to be rotated. Therefore, the above GIF will might have the following 4 entries:
Entry #1: (0, 0, -1)
Entry #2: (0, 0, 1)
Entry #3: (2, 1, -1)
Entry #4: (2, 1, 1)
Notice 2 entries for each x-y position in this case, as the shape can be rotated in both directions.
I am familiar with Unity's touch system although it is not much help to me here. I plan to use Transform.RotateAround(Vector3 point, Vector3 axis, float angle) to rotate the shape around a pivot and axis incrementally every frame, but I don't know how to calculate, from the player's touch input and touch position, which pivot and by what angle to rotate.
I have seen posts like this and this (using mouse input), and while they would be helpful were I dealing with only one pivot, I am dealing with potentially 2 and even 3 pivot points. Were only one pivot involved, I might try to check the angle difference every frame (the angle between the pivot, current position, and last frame position) and use Transform.RotateAround. Since there are two pivots, I first determine which pivot to rotate around, and then possibly calculate the angle deltas. However I don't know what the best way would be to go about finding which pivot to rotate around. Any ideas?
Apologies for the very long post, thanks for any and all help you can provide! Ask me to clarify anything.
rbjacob