Using Georeferencing in Unreal Engine

Unreal Engine has a Georeferencing plugin that lets you use real-world coordinates in Unreal Engine. How to use it is explained here, but the article doesn’t give a lot of detail, and shows no C++ code. The Georeferencing plugin is used for transforming between a real-world coordinate reference system (CRS) and the internal Engine coordinate system.

Basic setup

You need to enable the Georeferencing plugin for your project and add a Georeferencing System Actor to a level for which you want to use it. You can then setup your coordinate system. You can use either a flat or a round planet. A round planet makes sense if you want to do something at a global scale, like visualizing air traffic on a globe. But most of the times you’ll probably want to stick to the area covered by a projected coordinate system and hence stick to a flat planet, which is what I’m going to assume from here on.

You then need to choose the reference system that you want to use for cartesian (X/Y/Z) and geographic (latitude/longitude/height) coordinates. The Georeferencing plugin uses the PROJ library in the background and works with EPSG codes. Make sure to use true 3D coordinate systems. As an example, EPSG:28992 is the Dutch RD coordinate system, but this is a 2D CRS. EPSG:7415 is the correct compound CRS combining 2D planar coordinates with Dutch NAP height.

You also need to provide the location of the center of the Engine coordinate system in the projected CRS. This defines the translation from World coordinates to the Engine coordinates. It makes sense to set this to a value close to the location of where your data is provided to prevent accuracy issues with large coordinates.

Enabling the Status Bar Widget

The Georeferencing plugin provides a widget that shows coordinates on-screen. This widget can be added by hooking it up to the BeginPlay Event inside a Blueprint:

This widget then shows projected, geographic, and geocentric coordinates:

Coordinate transformations with C++

The API documentation can be found here. You need to add the GeoReferencing module to your Projectname.Build.cs file, then generate updated Visual Studio files by right-clicking the Projectname.uproject file in the Windows Explorer and choosing Generate Visual Studio project files.

Transforming coordinates is then straightforward, the respective functions expect FVector data types:

void World2Engine(const FVector &world, FVector &engine)
{
    UWorld* World = GetWorld();
    if (!World) return;

    AGeoReferencingSystem *georef = AGeoReferencingSystem::GetGeoReferencingSystem(World);

    georef->ProjectedToEngine(world, engine);
}

The reverse transformation is done with the EngineToProjected method. There are of course also functions to transform to/from geographic coordinates or earth-centered cartesian coordinates.

In most cases you’ll probably want to transform an entire array of coordinates, which can be achieved with this function:

void World2Engine(TArray<FVector>& Points)
{
    for (int i = 0; i < Points.Num(); i++)
    {
        FVector eng;
        World2Engine(Points[i], eng);
        Points[i] = eng;
    }
}

I would add both as methods to a Character that contains your other logic. I have yet to test if the transformation is setup once a level is started or when getting the pointer to AGeoReferencingSystem. In the latter case, this should not be done for each coordinate but only once, otherwise runtimes can be impacted severely. This could easily be done in the Character’s constructor.

 

Een reactie plaatsen

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *