<p>I don't know the specifics of what looks to be blender. But I would grab the view matrix of the camera. The rows of which should be orthonormal and give you forward, up, and right. Or columns depending on row or col major (technically its the inverse of the view, but that's just the transpose). Then just do some axes angle rotations around that. Something like this:</p>
<pre><code>Vec3 dx = scale * (currPosX - prevPosX)
Vec3 dy = scale * (currPosY - prevPosY)
Matrix4x4 m1 = axesAngleRot(dx, camera up)
Matrix4x4 m2 = axesAngleRot(dy, camera right)

Matrix4x4 deltaRot = m1 * m2
</code></pre>
<p>Where scale is defined by you, and dx and dy give the delta coords of the mouse.</p>
<p>Then apply that to the object. You may have to tack on an extra transform to move back to origin (however you have that transform defined) before doing rotations. The cube you showed is at origin though.</p>
<pre><code>Matrix4x4 finalTransform = inverseFinalTransform * deltaRot * finalTransform
</code></pre>

CC BY-SA 4.0
2021-01-28T01:22:55.150Z
2021-01-28T01:22:55.150Z
Andrew Wilson
<p>I know how to rotate objects around the center and there are rotation matrices for that as well, but, how do we rotate the object like a trackball, below is the example:</p>
<p><a href="https://i.sstatic.net/7Toy2.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7Toy2.gif" alt="enter image description here" /></a></p>
<p>firstly I rotate around the y axis, and then the x axis, then I rotate it like a trackball, how is the third rotation achieved?</p>

<transformations>
rotate objects like a trackball
juztcode
# rotate objects like a trackball

(Asked by: username_0 on 2021-01-26)

I know how to rotate objects around the center and there are rotation matrices for that as well, but, how do we rotate the object like a trackball, below is the example:

[![enter image description here](https://i.sstatic.net/7Toy2.gif)](https://i.sstatic.net/7Toy2.gif)

firstly I rotate around the y axis, and then the x axis, then I rotate it like a trackball, how is the third rotation achieved?

**Tags:** transformations


---

## 1 answers

### Answer by username_1 on 2021-01-28. Score: 1 | Accepted answer

I don't know the specifics of what looks to be blender. But I would grab the view matrix of the camera. The rows of which should be orthonormal and give you forward, up, and right. Or columns depending on row or col major (technically its the inverse of the view, but that's just the transpose). Then just do some axes angle rotations around that. Something like this:

```
Vec3 dx = scale * (currPosX - prevPosX)
Vec3 dy = scale * (currPosY - prevPosY)
Matrix4x4 m1 = axesAngleRot(dx, camera up)
Matrix4x4 m2 = axesAngleRot(dy, camera right)

Matrix4x4 deltaRot = m1 * m2

```

Where scale is defined by you, and dx and dy give the delta coords of the mouse.

Then apply that to the object. You may have to tack on an extra transform to move back to origin (however you have that transform defined) before doing rotations. The cube you showed is at origin though.

```
Matrix4x4 finalTransform = inverseFinalTransform * deltaRot * finalTransform

```


---

