Search code examples
rglrotational-matrices

Rotate camera along "eye" direction in rgl


In rgl, you can set up camera direction with rgl.viewpoint. It accepts theta, phi: polar coordinates. They specify the position of the camera looking at the origin. However, there is yet another one degree of freedom: angle of rotation of the camera along "eye" vector. I.e. one can imagine two vectors associated with camera: "eye" vector and "up" vector; theta and phi allow one to adjust "eye" vector, but I want then to adjust "up" vector after it. Is it possible to do it?

I guess that it can be possible to do it with userMatrix parameter («4x4 matrix specifying user point of view»), but I found no information how to use it.


Solution

  • The ?par3d help topic documents the rendering process in the "Rendering" section. It's often tricky to accomplish what you're asking for, but in this case it's not too hard:

    par3d(userMatrix = rotationMatrix(20*pi/180, 0,0,1) 
                       %*% par3d("userMatrix"))
    

    will rotate by 20 degrees around the user's z-axis, i.e. line of sight.