What is the proper way to reset the orientation of the camera?

I’ve created my own widget and keyboard shortcut to reset the orientation of the camera. However I see strange artifacts.

For example if I use the middle mouse button to rotate the camera let’s say 90 degrees, then I reset the view as given below. The camera does snap back to a zero orientation. However, the next time I begin to start rotating the camera its like the middle mouse button remembered my last time I let go of the rotation and snaps back to the 90 degrees then starts rotating from there.

viewer.camera.setView({
			orientation: {
				heading: 0,
				pitch: Cesium.Math.toRadians(-90),
				roll: 0
			}
		});

Any thoughts?

1 Like

In my application, I display a “compass” using the camera heading relative to north, and when the user clicks it, I reset the camera heading (but do not change the pitch). This is the method I use:

    public resetOrientation(): void {
        const v = this.viewer;
        if (!v || v.scene.mode === SceneMode.SCENE2D) { return; }
        const cartesian = Cartographic.toCartesian(v.camera.positionCartographic);

        v.camera.flyTo({
            destination: cartesian,
            orientation: {
                heading: CesiumMath.toRadians(0),
                pitch: v.camera.pitch,
                roll: 0.0
            }
        });
    }

I chose flyTo because it makes a smooth transition, rather than “snapping” the camera. You could also reset the pitch if you prefer.

1 Like

Thank you James. I also tried the flyTo and it does work but you can’t stop the flyTo transition with your mouse until it fully completes. Have you found this to be a problem?

However my biggest issue is that Cessium seems to remember your last mouse position when you were rotating, so then you start rotating again and it immediately snaps back to the last known angle making a huge jump in your view.

I realized I actually didn’t set up a keyboard shortcut for that function. I went ahead and added one, just to see. I don’t have a mouse attached to this computer, but I am able to rotate using CTRL+pointer movement, or using my touchscreen. I can rotate, then call the above reset function, then rotate again, and I don’t see any unusual jumps. If you’re having a problem, maybe try to put together a reproduction using Sandcastle?

The camera controls are basically locked out for movement during the flyTo transition, but I don’t think anybody has ever complained to me about it – the time is usually around 3 seconds, I think, so it doesn’t seem like much of an imposition. ETA: The computed transition did feel a little slow, so in response to your comment, I tried adding a duration to the arguments in my code, and it looks and feels pretty nice around 0.5, maybe 0.75. Worth playing with, certainly.

1 Like

@gimp3695 welcome to the community!
@James_B thank you very much for your input here :grin:

1 Like