How do I get the 3D coordinates of all vertices of a rotated, scaled and position cube

So, I’m trying to convert the following code from LWJGL & JOML to MonoGame, as I’m trying to calculate the corners of a rotated, scaled and positioned cube, so that I can build a Mesh, but I’m currently failing.

Here is the thread, I’m converting the answer from: https://stackoverflow.com/questions/51483021/get-3d-coordinates-of-vertices-of-rotated-and-scaled-cuboid-with-scale-center-p

Just use the Corners in Local-Space and apply the same Matrix to them as you do for the cuboid? You’d have a matrix for the cuboid for rendering I guess

Vector3.Transform(Matrix)

How do I obtain said Matrix? Do I create a Rotation Matrix, Translation Matrix ans Scale Matrix and multiply them together? Then I calculate the x,y,z coordinates as it’s done from the loop?

I don’t really understand your problem I think, because in order to render that cube (in 3D) you’d need a Transform-Matrix anyway to pass to the Renderer or do you use some other type of displaying?

You can always recreate the code in the sample like so:

Matrix.CreateTranslation(pos) * Matrix.CreateScale(scale) * Matrix.CreateRotationX(rad) … … … etc

passing this to Vector3.Transform(corner, matrix) will get you the transformed positions

Ok, that’s all I wanted. And do the rotations have to be in radians?

yes - you can use MathHelper.ToRadians to convert from degrees

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}