My values are broken. Here’s my code:
Generating the Cube:
Voxel tmp = new Voxel();
tmp.Position = new Vector3(32, 64, 32);
tmp.Rotation = new Vector3(45, 90, 45);
tmp.Scale = new Vector3(5, 2, 1);
tmp.BuildMesh();
The Voxel Class:
public class Voxel
{
public Vector3 Position = new Vector3(1, 1, 1);
public Vector3 Rotation = new Vector3(0, 0, 0);
public Vector3 Scale = new Vector3(1, 1, 1);
public void BuildMesh()
{
List<VertexPositionNormalTexture> vertices = new List<VertexPositionNormalTexture>();
/* Cuboid center position */
float px = 10, py = 0, pz = 0;
/* Euler angles around x, y and z */
float ax = 0, ay = 0, az = (float)Math.PI / 2.0f;
/* Scale factor for x, y und z */
float sx = 1, sy = 3, sz = 1;
/* Build transformation matrix */
Matrix m = new Matrix();
Matrix pos = Matrix.CreateTranslation(Position.X, Position.Y, Position.Z); // <- translate to position
Matrix rot = Matrix.CreateRotationX(MathHelper.ToRadians(Rotation.X)); // <- rotation about x
rot *= Matrix.CreateRotationY(MathHelper.ToRadians(Rotation.Y)); // <- rotation about y
rot *= Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation.Z)); // <- rotation about z
Matrix scl = Matrix.CreateScale(Scale.X, Scale.Y, Scale.Z); // <- scale
/* Compute cube corners and print them */
m = pos * rot * scl;
Vector3[] corners = new Vector3[8];
for (int i = 0; i < corners.Length; i++)
{
int x = i % 2 * 2 - 1;
int y = i / 2 % 2 * 2 - 1;
int z = i / 4 % 2 * 2 - 1;
corners[i] = Vector3.Transform(new Vector3(x, y, z), m);
Console.WriteLine(string.Format(
"Corner ({0}, {1}, {2}) = {3}",
x, y, z, corners[i]));
}
}
}
Console Output:
Corner (-1, -1, -1) = {X:155 Y:126 Z:-31}
Corner (1, -1, -1) = {X:155 Y:126 Z:-33}
Corner (-1, 1, -1) = {X:155 Y:130 Z:-31}
Corner (1, 1, -1) = {X:155 Y:130 Z:-33}
Corner (-1, -1, 1) = {X:165 Y:126 Z:-31}
Corner (1, -1, 1) = {X:165 Y:126 Z:-33}
Corner (-1, 1, 1) = {X:165 Y:130 Z:-31}
Corner (1, 1, 1) = {X:165 Y:130 Z:-33}