So I've got a grid of cubes and I'd like to allow the player to swap positions with the cubes that are adjacent to it. Right now I have a 2d array of world matrices available to my game board class and I'm using Matrix.Lerp to animate between the two positions. However, when I originally created these matrices, I used Matrix.CreateTranslation to a vector scaled by the size of my cubes. However, I don't apply this when I swap positions, so the new positions of the cubes don't line up with the grid anymore because their movement hasn't been scaled. Is there any way to do this if I"m calculating the matrices this way? Code below:
My first alignment on the grid:
| 1 | float sphereScale = Math.Max(pieceTransforms[0].M11, pieceTransforms[0].M22); |
| 2 | blockSize = blocks[0].Model.Meshes[0].BoundingSphere.Radius * sphereScale * 1.9f; |
| 3 | |
| 4 | for (int y = 0; y < Height; y++) |
| 5 | { |
| 6 | for (int x = 0; x < Width; x++) |
| 7 | { |
| 8 | Vector3 blockPos = new Vector3( |
| 9 | (Width - 1) * -0.5f, (Height - 1) * -0.5f, 0.0f); |
| 10 | blockPos += new Vector3(x, y, 0.0f); |
| 11 | blockPos *= blockSize; |
| 12 | |
| 13 | Matrix transform = Matrix.CreateTranslation(blockPos); |
| 14 | |
| 15 | boardPositionMatrices[x, y] = transform; |
| 16 | layout[x, y].World = transform; |
| 17 | } |
| 18 | } |
My swap position code:
| 1 | Matrix toTransform, fromTransform; |
| 2 | |
| 3 | // Linearly interpolate between the world matrcies of the to and from blocks |
| 4 | // to eventually end up with them both in the other's original positioning. |
| 5 | Matrix.Lerp(ref boardPositionMatrices[shiftingBlocks[0].XLayoutPosition, |
| 6 | shiftingBlocks[0].YLayoutPosition], |
| 7 | ref boardPositionMatrices[shiftingBlocks[1].XLayoutPosition, |
| 8 | shiftingBlocks[1].YLayoutPosition], |
| 9 | currentShiftTime / shiftDuration, |
| 10 | out fromTransform); |
| 11 | |
| 12 | Matrix.Lerp(ref boardPositionMatrices[shiftingBlocks[1].XLayoutPosition, |
| 13 | shiftingBlocks[1].YLayoutPosition], |
| 14 | ref boardPositionMatrices[shiftingBlocks[0].XLayoutPosition, |
| 15 | shiftingBlocks[0].YLayoutPosition], |
| 16 | currentShiftTime / shiftDuration, |
| 17 | out toTransform); |
| 18 | |
| 19 | |
| 20 | shiftingBlocks[0].World = fromTransform; |
| 21 | shiftingBlocks[1].World = toTransform; |