1

I'm adding a plane entity to Cesium as the folowing:

let position = Cesium.Cartesian3.fromDegrees(long, lat, alt)
let planeEntity = this.viewer.entities.add({
  position: position,
  model: {
    uri: './assets/cesium/Cesium_Air.glb',
    minimumPixelSize : 64
  }
});

I get plane locations at realtime, each time location arrived I do:

planeEntity.position = Cesium.Cartesian3.fromDegrees(long, lat, alt);

and move the plane to that location.

I want to rotate the plane head to the right place (if the plane flight up, the head cant stay to the left).

How can I calculate bearing rotation from 2 positions? (current position and the next position)?

Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42
larry ckey
  • 141
  • 2
  • 14

2 Answers2

1

I found solution here: [Calculate bearing between 2 points with javascript

 // Converts from degrees to radians.
   toRadians(degrees) {
    return degrees * Math.PI / 180;
  }

// Converts from radians to degrees.
   toDegrees(radians) {
    return radians * 180 / Math.PI;
  }

   bearing(startLat, startLng, destLat, destLng){
    startLat = this.toRadians(startLat);
    startLng = this.toRadians(startLng);
    destLat = this.toRadians(destLat);
    destLng = this.toRadians(destLng);

    let y = Math.sin(destLng - startLng) * Math.cos(destLat);
    let x = Math.cos(startLat) * Math.sin(destLat) - Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
    let brng = Math.atan2(y, x);
    let brngDgr = this.toDegrees(brng);
    return (brngDgr + 360) % 360;
  }
larry ckey
  • 141
  • 2
  • 14
0

Cesium way (ES6) based on larry ckey's answer:

import * as Cesium from 'cesium';

const calculateBearing = (startPoint, endPoint) => {
  const start = Cesium.Cartographic.fromCartesian(startPoint);
  const end = Cesium.Cartographic.fromCartesian(endPoint);

  const y = Math.sin(end.longitude - start.longitude) * Math.cos(end.latitude);
  const x =
    Math.cos(start.latitude) * Math.sin(end.latitude) -
    Math.sin(start.latitude) * Math.cos(end.latitude) *
    Math.cos(end.longitude - start.longitude);

  const bearing = Math.atan2(y, x);
  return Cesium.Math.toDegrees(bearing);
}
Ugnius Malūkas
  • 2,649
  • 7
  • 29
  • 42