I am trying to get my app to start tracking an entity when the user hits a button. I would like the camera’s height to stay put (not jump), when the user starts tracking the entity.
My understanding is that when I set the entity to be a trackedEntity, the camera is moved to the “viewFrom” position, which is relative to the trackedEntity.
To keep the camera at its current height above the surface, I subtract the trackedEntity’s height (in world coordinates) from the camera height (in world coordinates) to get the camera height in the trackedEntity’s coordinates and set that as the viewFrom value. However, the camera still jumps closer to the object. Is my understanding of the frames of reference off? Or are there scales involved that I’m ignoring?
Here is my code:
var camera = viewer.scene.camera;
var entityHeightInWorld = trackedEntity.position._value.z;
var cameraHeightInWorld = viewer.trackedEntity ? camera.position.z + viewer.trackedEntity.position._value.z : camera.position.z;
trackedEntity.viewFrom = new Cesium.Cartesian3( 0, 0, cameraHeightInWorld - entityHeightInWorld );
viewer.trackedEntity = trackedEntity;
Thanks for the help!