I want to get distance between an entity and camera in meters. To achieve that I'm passing cartesian3 positions of camera and entity in Cesium.Cartesian3.distanceSquared. It seems distance is calculating properly when the entity is not being tracked. But Tracked entity distance seems higher even though the camera is nearly moving along with the tracked entity.
I'm not getting how to get correct distance between any entity (tracked/without tracked) and camera.
Entity can be anywhere on earth or in air and distance should not be the travelling distance.
viewer.clock.onTick.addEventListener(function (clock) {
let epos = entity.position.getValue(clock.currentTime);
let campos = viewer.camera.position;
let distancebetween = Cesium.Cartesian3.distanceSquared(epos, campos);
if (viewer.trackedEntity){
console.log("Tracked:" + distancebetween);
}else{
console.log("NotTracked:" + distancebetween);
}
});
Here is the Sandcastle link of what I'm trying to do.