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