Assistance with Tracking Distance, Altitude, and Speed in Cesium for Unity

Hello Cesium team,

I’m currently working on a project in Cesium for Unity, and I need assistance with the following:

  1. Calculating Distance to a Building:
    How can I calculate the distance from a moving object (e.g., a vehicle or drone) to a specific building or structure within the Cesium 3D Tiles? I’m looking to measure this in meters and dynamically update it as the object moves.

  2. Tracking Altitude:
    What’s the best approach to retrieve and display the altitude of the object above the terrain or a specific point of interest in meters?

  3. Tracking Speed and Distance:
    How can I compute the speed of the object in real-time based on its movement over the terrain, as well as the total distance it has traveled?

I’d appreciate any code snippets, API references, or workflows you could share to help implement these features effectively.

need answers immediately, please help us

Thank you for your guidance!

Best regards,
[Payal zariya]

Hi,

This reads a bit like a list of requirements, and, if so, the source of those requirements might have specific expectations about how those things are computed. I can suggest a way to do them, but you will have to evaluate if my suggestions are appropriate for your needs.

Calculating Distance to a Building

Do you know the location of the building? Perhaps as a longitude/latitude/height? If so, you can convert the longitude/latitude/height to a 3D ECEF position by calling CesiumWgs84Ellipsoid.LongitudeLatitudeHeightToEarthCenteredEarthFixed. You can do the same for your moving object; if your object has a CesiumGlobeAnchor component attached, then the positionGlobeFixed property will give it to you the ECEF position directly. To compute the distance, subtract one position from the other, and compute the magnitude of the resulting vector using math.length in Unity.Mathematics.

If you know the longitude/latitude of the building, but not its height, you can sample the height at a particular location in a tileset using the SampleHeightMostDetailed method on Cesium3DTileset.

If you don’t know either, but instead need to find the location of a building, it might be tricky, and the details will vary depending on what tileset you’re using.

Tracking Altitude

An object with a CesiumGlobeAnchor has a longitudeLatitudeHeight property. The height (Z component) is the height of the object above the WGS84 ellipsoid. To turn that into a height above the terrain, you need to compute the terrain height at that longitude/latitude, which can do with the SampleHeightMostDetailed method mentioned above. To find the height above some other object, compute that other object’s height and subtract it from the first object’s height.

Tracking Speed and Distance

I think the most common way to compute the velocity of an object is to compute the distance between two successive positions (for example, over two render frames), subtract them, and then divide by the time that elapsed between those samples. This will give you as velocity vector in whatever reference frame your positions were originally expressed in. You can use Earth-Centered, Earth-Fixed (ECEF) as mentioned above, or you can use Unity coordinates obtained directly from the objects (but watch out for an origin rebase between the frames!).

This will give you the straight-line velocity between the two positions, which is not necessarily representative of the path the object actually followed. For example, an aircraft moving at a constant altitude will follow a slightly longer path between two points because it will follow the curvature of the Earth. The shorter straight-line path would result in a slight decrease in altitude. Therefore, this method would compute a velocity very slightly lower than the aircraft’s actual velocity.