<p>to generate the disired orientation matrix you can do the following:</p>
<p>lets say <code>v</code> is the vector from the cubes origin towards the camera. <code>u</code> is the up vector of your camera in worldspace.</p>
<pre><code>//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 &quot;0&quot;. Only the diagonal cells from left up to right down is filled with &quot;1&quot;
    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;
}
</code></pre>
<p>The order of matrix multiplications is important because it is not commutative. So the result matrix from the last method applies the <code>calculateOrientationTowardsCamera</code> rotation on top of the 45/295 rotation.</p>

CC BY-SA 4.0
2022-07-21T18:51:53.917Z
2022-07-22T09:45:15.780Z
2022-07-22T09:45:15.780Z
Thomas
<p>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.</p>
<p>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- &amp; 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)</p>
<p>The problem now is that rotation of the voxel would need to be modified based on the position and fov of the camera:</p>
<p><a href="https://i.sstatic.net/U43rr.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/U43rr.png" alt="3D Space with a Cube and a Camera" /></a></p>
<p>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.</p>
<p><a href="https://i.sstatic.net/Kd39K.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Kd39K.png" alt="enter image description here" /></a></p>
<p>The correct parameters would be x: 45° and y: 295°
<a href="https://i.sstatic.net/ovj5t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ovj5t.png" alt="enter image description here" /></a></p>
<p>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:</p>
<p><a href="https://i.sstatic.net/0RB4M.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/0RB4M.png" alt="enter image description here" /></a>
<a href="https://i.sstatic.net/BOUlo.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BOUlo.png" alt="enter image description here" /></a></p>
<p>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.</p>
<p>Thanks in advance!</p>

CC BY-SA 4.0
2022-07-21T17:16:23.960Z
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?
Thomas
CC BY-SA 4.0
2022-07-21T17:55:35.480Z
@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
kaelundeen
CC BY-SA 4.0
2022-07-21T18:17:34.307Z
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?
Thomas
<3d><transformations><mathematics>
View rotation matrix
kaelundeen
# View rotation matrix

(Asked by: username_0 on 2022-07-21)

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:

[![3D Space with a Cube and a Camera](https://i.sstatic.net/U43rr.png)](https://i.sstatic.net/U43rr.png)

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.

[![enter image description here](https://i.sstatic.net/Kd39K.png)](https://i.sstatic.net/Kd39K.png)

The correct parameters would be x: 45° and y: 295°
[![enter image description here](https://i.sstatic.net/ovj5t.png)](https://i.sstatic.net/ovj5t.png)

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:

[![enter image description here](https://i.sstatic.net/0RB4M.png)](https://i.sstatic.net/0RB4M.png)
[![enter image description here](https://i.sstatic.net/BOUlo.png)](https://i.sstatic.net/BOUlo.png)

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.

Thanks in advance!

**Tags:** 3d, transformations, mathematics

> **Comments:**
> **username_1**: 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?  
> **username_0**: @username_1 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  
> **username_1**: 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?  

---

## 1 answers

### Answer by username_1 on 2022-07-21. Score: 0

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.


---

