Positioning the camera to point to the north

We have code that is supposed to point the camera to the north when the user presses the 'N' key and to point north and straight down when they press the 'R' key. After upgrading from Cesium 1.7.1 to 1.49.0 the code that was working now crashes with the following message:

An error occurred while rendering. Rendering has stopped.
undefined
TypeError: Cannot create property 'x' on number '-0.6110096825637408'
TypeError: Cannot create property 'x' on number '-0.6110096825637408'
    at Function.l.fromAxisAngle (http://xxxxxxxx.com/js/cesium/Build/Cesium/Cesium.js:455:12149)
    at Function.l.fromHeadingPitchRoll (http://xxxxxxxx.com/js/cesium/Build/Cesium/Cesium.js:455:13051)
    at transformCartesianHeadingPitchRangeToCartesian (http://xxxxxxxx.com/js/js_map_ce.js:1101:38)

The code fragments that reposition the camera are shown below. They (apparently) came from a block of code that we got from a Cesium sample (i.e., which moves the camera based on keypresses).

if (flags.lookNorth || flags.centerDown ) {
  if (Cesium.defined(positionCanvasCenter )) {
  
    var newPitch = flags.centerDown ? Cesium.Math.toRadians( -90 ) : camera.pitch;
    var result = Cesium.Cartesian3.fromDegrees(0,0);
    var newCameraPosition = transformCartesianHeadingPitchRangeToCartesian(positionCanvasCenter, 0, newPitch, distanceToGlobe, result);
    
    camera.flyTo({
      destination : newCameraPosition,
      duration: 1,
      orientation : {
        heading : 0,
        pitch : newPitch,
        roll : 0
      }
    });
  }
}

function transformCartesianHeadingPitchRangeToCartesian(positionCartesian, heading, pitch, range, result) {
  var position = Cesium.Cartesian3.clone(positionCartesian, result);
  var ellipsoid = viewer.scene.mapProjection.ellipsoid;
  
  // Find a point from which we can see the target with specified pitch and roll
  var quaternion = Cesium.Quaternion.fromHeadingPitchRoll(heading - Math.PI/2, pitch, 0.0, new Cesium.Quaternion());
  var pitchRollMatrix3 = Cesium.Matrix3.fromQuaternion(quaternion, new Cesium.Matrix3());
  
  var localViewDir = Cesium.Matrix3.multiplyByVector(pitchRollMatrix3, Cesium.Cartesian3.fromElements(1.0, 0.0, 0.0), new Cesium.Cartesian3());
  var orientationMatrix3 = Cesium.Matrix4.getRotation(Cesium.Transforms.eastNorthUpToFixedFrame(position, ellipsoid), new Cesium.Matrix3());
  
  // Transform from local to global cartesian
  var direction = Cesium.Matrix3.multiplyByVector(orientationMatrix3, localViewDir, localViewDir);
  Cesium.Cartesian3.multiplyByScalar(direction, -range, direction);
  Cesium.Cartesian3.add(position, direction, position);
  
  return position;
}

In the above code fragments, the error occurs in the call to Cesium.Quaternion.fromHeadingPitchRoll(). Heading is 0 and pitch is -1.57.

We know barely enough about Cesium to get in trouble (i.e., almost nothing) and not enough to resolve this!

Thanks for your help!

I found the problem. I was calling the deprecated fromHeadingPitchRoll() which accepted individual heading, pitch, and roll arguments.

The fix was to create a HeadingPitchRoll object:

var hpr = new Cesium.HeadingPitchRoll(heading - Math.PI/2, pitch, 0.0);

And pass that to fromHeadingPitchRoll:

var quaternion = Cesium.Quaternion.fromHeadingPitchRoll(hpr, new Cesium.Quaternion());

Awesome, glad you figured it out, and thanks for posting your solution!