Unproject cartesian to cartesian

Hi!

I have a lot of cartesian coordinates (moving points on a plane) that I want to place programatically on the globe. Does the Unreal plugin provide any function for unprojecting coordinates. Preferrably resulting in cartesian coordinates. I’m a newbie so sorry if my question makes poor sense.

Best
Rikard

Just to clarify, the points are placed in reference to a web mercator projected map.

There are functions to transform ECEF or LongitudeLatitude coordinates to Unreal positions and vice versa. Is this what you are looking for?

Thank you arbertrary,

It depends. Do any of the functions unproject projected coordinates? I have points that exist in web mercator projection space. I need to unproject them so that they are correctly placed on the curved globe surface.

Best
Rikard

There seems to exist such functions in the javascript library. But I cannot find the same in the unreal plugin: WebMercatorProjection - Cesium Documentation

Cesium for Unreal doesn’t have anything built-in for working with Web Mercator coordinates. However, if you can use C++, Cesium for Unreal’s underlying library, called cesium-native, has a WebMercatorProjection class that will do what you want. Specifically, it will allow you to transform Web Mercator coordinates to longitude/latitude, at which point you can use the functions @arbertrary mentioned.

If that doesn’t work for you, you can also consider using Unreal’s built-in Georeferencing plugin, which can do arbitrary transformations with PROJ. I believe it’s Blueprint-accessible as well:
https://dev.epicgames.com/documentation/en-us/unreal-engine/georeferencing-a-level-in-unreal-engine

That’s great news. I can use the WebMercatorProjection functions using C++. Thank you Kevin.

I’m using the project/unproject functions in that class but it seems to give incorrect results, or probably I’m using it wrong. I am first using ‘project’ to find the CesiumGeoreference origin in web mercator space (they appear to be correct). But then I am adding my web mercator coordinates to the x and y of that origin and doing unproject. The result are geographic coordinates that are a fair bit off. (10000m = about 7700m).

Can you share your code? Adding your coordinates to something derived from the CesiumGeorefence origin doesn’t sound right, but if I can see your code I’ll know for sure.

Of course. I apologize I didn’t do it to begin with:

CesiumGeospatial::WebMercatorProjection projection(CesiumGeospatial::Ellipsoid::WGS84);
FVector originDegrees = CesiumGeoreference->GetOriginLongitudeLatitudeHeight();
CesiumGeospatial::Cartographic originCartoGraphic(originDegrees.X * (pi / 180.0), originDegrees.Y * (pi / 180.0), originDegrees.Z);
SimLocation = projection.project(originCartoGraphic);
CesiumGeospatial::Cartographic simLocGeo = projection.unproject(glm::dvec3(SimLocation.x, SimLocation.y, SimLocation.z));
CesiumGeospatial::Cartographic testCarto = projection.unproject(glm::dvec3(10000.0 + SimLocation.x, 10000.0 + SimLocation.y, SimLocation.z));

UE_LOG(LogTemp, Warning, TEXT(simLocGeo  is lon: %f . lat: %f . %f), simLocGeo.longitude * (180.0 / pi), simLocGeo.latitude * (180.0 / pi), simLocGeo.height);
UE_LOG(LogTemp, Warning, TEXT(testCarto is lon: %f . lat: %f . %f), testCarto.longitude * (180.0/pi), testCarto.latitude * (180.0 / pi), testCarto.height);

Output:

LogTemp: Warning: simLocGeo  is lon: -105.104505 . lat: 39.409924 . 2250.000000
LogTemp: Warning: testCarto is lon: -105.014673 . lat: 39.479295 . 2250.000000

The simLocGeo latitude and longitude is exactly the same as the GeoReference origin I have set in the editor. I expect (perhaps erroneously) the testCarto latitude and longitude to be located at 10000x10000 meters on the globe in relation to the origin. However, It is at around 7700x7700 meters.

Your code looks right to me, and I can believe that ~7700 meters is correct. Keep in mind that Web Mercator meters are not actually meters. They get larger as you get further from the equator.

Aha. Then I suppose I have some learning to do. I thought that a meter in projected space would amount to a meter on the globe after you unproject it.

Thank you

No, I don’t believe any projection does (or can) have that property. You can instead use a local Cartesian coordinate system centered at a point on the Earth, but that wouldn’t be considered a projection. And if set up your coordinate systemn so that Z=0 is Mean Sea Level at a particular point on Earth, then a little distance away from that point, Mean Sea Level would be below Z=0 due to Earth curvature. Think of Z=0 as a plane touching the Earth at a point, and the curved Earth falls away beneath it.