Quaternion to camera rotation

1. A concise explanation of the problem you’re experiencing.

I have quaternion an want to apply it to camera orientation. To set camera orientation I can use heading, pitch, roll or direction vectors. Heading, pitch, roll doesn’t work for me since gimbal lock preventing from changing heading too much. With vectors problem is that their value changing depending on location and i can’t just apply vectors from rotation matrix that I get from my quanterion, not messing up starting camera orientation. So, my question is, how to apply quanterion rotation to camera direction vectors. This sounds like something simple. but I couldn’t find the answer.

2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.

Right now my working code looks like this:

    let dirForward = new Cesium.Cartesian3();
   dirForward.x = 2 * (quat.x * quat.z + quat.w * quat.y);
   dirForward.y = 2 * (quat.y * quat.z - quat.w * quat.x);
   dirForward.z = 1 - 2 * (quat.x * quat.x + quat.y * quat.y);
    let dirUp = new Cesium.Cartesian3();
   dirUp.x =  2 * (quat.x * quat.y - quat.w * quat.z);
    dirUp.y =  1 - 2 * (quat.x * quat.x + quat.z * quat.z);
   dirUp.z =  2 * (quat.y * quat.z + quat.w * quat.x);
        camera.setView({
         orientation: {
             direction: dirForward,
             up: dirUp
         }
        });

``

But when stepping into it, camera goes somewhere, where it is not supposed to be, I want camera to stay stil, when quanterion is 0,0,0,1.

3. Context. Why do you need to do this? We might know a better way to accomplish your goal.

I’m building for VR and I’m only able to get quaternion as headset orientation, but not able to directly apply is to camera.

4. The Cesium version you’re using, your operating system and browser.

Cesium 1.59, Windows 10, Chrome 76

P.S. What’s up with tags? I couldn’t add any.

So, I figured out how to correclty apply quanterion to camera (or at least it works good enough for me). At first frame in render loop I save current camera rotation matrix (it’s three direction vectors), then at every next frame I just multiply it by direction vectors from my quanterion and assing results to corresponding direction vectors of camera.

Thanks for posting your solution! For the forum tags, I think only moderators are able to use those?

This sounds like a cool project - what kind of VR headset are you using? Will this project be publicly accessible when it’s done? It’d be really cool to showcase - while we have some Sandcastle VR demos, I don’t think I’ve seen a recent application using it.

And in case you find it helpful, this is the VR code example I was referring to:

https://cesiumjs.org/Cesium/Apps/Sandcastle/index.html?src=Cardboard.html

It sets the camera’s reference frame to be where you want the viewer to be so it’s easy to apply any transformations to the camera from that vantage point.

I use HTC Vive for testing. Project will be publicly accessible, when it will be released, but I’m not so sure about VR support. I guess, it depends on how far will I go. Can’t share any more details, since it isn’t my personal project. Speaking about cardboard example, I use Cesium scene’s property useWebVR to split screen for stereoview, it still has some problems, mainly with alignment of two pictures in headset (wikipedia suggests diplopia as correct term for similar medical condition).

I’d definitely love to see it when it’s out. If you run into issues like that that you can re-create in a code example you can share, it would be great to open a bug report to document it so we or others in the community can test and improve it.

Hi, could you please post the code snippet that worked for you. I am finding difficulty in applying the quaternion to the camera.