Set camera to view from entity

I'd like to set the camera view to be looking from the perspective of an entity. For example, if an aircraft is flying, I want the camera to be positioned at the aircraft looking out from the cockpit. When the aircraft rolls, the camera should roll to.

I'm using camera.setView() and am succesfully setting the position, but I can't get the orientation quite right. I'm hoping someone can set me straight. Perhaps there is an easier way than what I am attempting?

Here's my code:

function set_camera(entity_id, time) {
   var entity = viewer.entities.getById(entity_id);
   var position = entity.position.getValue(time, new Cesium.Cartesian3());
   var q_R_ecef_to_body = entity.orientation.getValue(time, new Cesium.Quaternion());
   var q_T_ecef_to_body = Cesium.Quaternion.conjugate(q_R_ecef_to_body, new Cesium.Quaternion());

   var r4_T_ned_to_ecef = Cesium.Transforms.northEastDownToFixedFrame(position);
   var r3_T_ned_to_ecef = Cesium.Matrix4.getRotation(r4_T_ned_to_ecef, new Cesium.Matrix3);
   var q_T_ned_to_ecef = Cesium.Quaternion.normalize(Cesium.Quaternion.fromRotationMatrix(r3_T_ned_to_ecef), new Cesium.Quaternion());
   var q_T_ned_to_body = Cesium.Quaternion.multiply(q_T_ecef_to_body, q_T_ned_to_ecef, new Cesium.Quaternion());

   var hpr = Cesium.HeadingPitchRoll.fromQuaternion(q_T_ned_to_body, new Cesium.HeadingPitchRoll());

   var camera = viewer.camera;
   camera.setView({
      destination: position,
      orientation: {
         heading: hpr.heading,
         pitch: hpr.pitch,
         roll: hpr.roll
      }
   )};
}

Any guidance would be appreciated. Thanks!
-Steven

Hi Steven

This is exactly what I am looking for.

Did you have some success in the meantime?

Thanks for letting me know

Tim

This thread has a code snippet for figuring out an entity’s orientation:

https://groups.google.com/d/msg/cesium-dev/xPOMVgEbids/VlwQuis3CAAJ

Tim, I’m afraid I never did get this working perfectly. I ended up going a different route and didn’t need to do this anymore. I believe the code I posted was close, but just missing something regarding the orientation. Perhaps the code Omar just posted will get you what you need. Best of luck!