# View rotation matrix

Im unsure about how I could describe what im trying to achieve in few words or a title so ill go on to describe it.

So I have voxel, in 3D space, represented solely by its center. The voxel though, is going to be rendered by using a set of pre-rendered images and rotation parameters. E. G.: A function would take an x- & y-rotation and match it with a particular image, which it will display on the screen (Edit: For clarity, the image the function would match with a particular rotation would be an frontal image of the voxel rotated by that rotation)

The problem now is that rotation of the voxel would need to be modified based on the position and fov of the camera:

This cube has a rotation of 0,0. But when using the above described function with the parameters 0,0 it would display a rectangle.

The correct parameters would be x: 45° and y: 295°

Obviously the position on the screen can be calculated using the projected coordinates of the center and the size can be derived from the Z-coordinate of the center. And In general I have a basic idea of how I would calculate the rotation:

But im unsure if it will work correctly and if there's a more elegant way of calculating the rotation like e. g. with a matrix.

• Are you trying to rotate the cube 'voxel' so that it alway pointing towards a point in 3d space? So when rendering from that point all cubes are looking like squares? Commented Jul 21, 2022 at 17:16
• @Thomas no the exact other way around, so if I would render all cubes with a rotation of 0,0, they would all look like squares. Im trying to make the look like actual cubes in 3D space Commented Jul 21, 2022 at 17:55
• okay, so they should be all face towards the camera plus the additional 45° 295° rotation right? In principle it is the same... So have I understood it correctly? Commented Jul 21, 2022 at 18:17

to generate the disired orientation matrix you can do the following:

lets say v is the vector from the cubes origin towards the camera. u is the up vector of your camera in worldspace.

//This method returns the orientation towards the camera
Matrix3x3 calculateOrientationTowardsCamera(Vector3D v, Vector3D u)
{
Matrix3x3 result;
Vector3D vNormalized = v.normalized();
Vector3D uNormalized = u.normalized();
Vector3D rightDirection = cross(vNormalized, uNormalized); // this vector does not need to be normalized, because vNormalized and uNormalized is normalized already, so the result is normalized as well
Vector3D uCorrectedNormalized = cross(rightDirection, vNormalized);
result.row(0) = rightDirection;
result.row(1) = uCorrectedNormalized;
result.row(2) = vNormalized;
return result;
}

Matrix3x3 get45And295Rotation()
{
Matrix3x3 result = Matrix3x3::Identity(); //matrix filled with "0". Only the diagonal cells from left up to right down is filled with "1"
result *= rotation(45, 0, 0); //rotation should give you a rotation matrix
result *= rotation(0, 295, 0);
return result;
}

Matrix3x3 combineRotations(Vector3D v, Vector3D u)
{
Matrix3x3 result = calculateOrientationTowardsCamera(v, u);
result *= get45And295Rotation();
return result;
}


The order of matrix multiplications is important because it is not commutative. So the result matrix from the last method applies the calculateOrientationTowardsCamera rotation on top of the 45/295 rotation.