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.