I have a group of soldiers, and each has a vector describing their position in the squad. When the group rotates, I do this math to calculate the desired position of each soldier.
var unitPositions : Array<CGPoint> = []
let cosRadian = cos(rotation)
let sinRadian = sin(rotation)
for var index = 0; index < self.positions.count; ++index {
// Find the vector for this unit, translate it, then store it
let targetVector = self.getVectorAtPosition(index) - translate
let newX = targetVector.dx * cosRadian - targetVector.dy * sinRadian
let newY = targetVector.dx * sinRadian + targetVector.dy * cosRadian
unitPositions.append(CGPoint(x:newX, y:newY))
}
This produces the following result:
The problem with this is that units in an isometric world facing south east should match up with the reference grid. So the next step was to add a multiplier. In the loop above, I changed it to:
let isoX = newX * 1.414213562373095;
let isoY = newY
unitPositions.append(CGPoint(x:isoX, y:isoY))
This produced closer results:
But, it's still not 100% accurate. So, right now I'm going through and trying to get my fundamentals of math, matrices, vectors, etc all figured out in hopes of producing this desired result:
I would love any pointers on what I should be searching for, as right now it is all a bit overwhelming. Thanks much!
UPDATE
Due to the awesome comment by @DMGregory, here are my findings:
If I want to "hack" this and continue to use screen coordinates, I can do so using 1.414213 (the magic number found here) combined with your true isometric projection.
let isoX = newX * 1.414213562373095
let isoY = newY * (1.414213562373095 * 1/sqrt(3))
That will produce:
That approach will work, but won’t solve everything else you mentioned (i.e: units will move faster when going certain directions). To handle that, creating the original formation with isometric vectors, and then just rotating that matrix will produce the same results. So, it looks like I have a lot of work converting my game calculations and coords to default as isometric coordinates. Thanks again!