Build a Flight Tracker with Cesium for Unreal
This tutorial will walk you through how to visualize a flight from Sanfrancisco to Copenhagen.
Flying over San Francisco, CA, USA.
You’ll learn how to:
- Import real-world flight data into Unreal Engine
- Create a flight path using the data and
- Import a model of an airplane and have the plane follow the flight path
- Switch between different camera views in-flight
- An installed version of Unreal Engine (at least 4.26 or later). For instructions on how to install Unreal Engine, visit the Unreal Engine download page and refer to the Installing Unreal Engine guide for detailed instructions.
- Visual Studio 2019 with Desktop Development with C++ workload
- Know how to set up a basic Cesium for Unreal application. Check out our Cesium for Unreal Quickstart guide for instructions on starting with the Cesium for Unreal plugin
Step 1. Create an Unreal level
You can either start with the 01_CesiumWorld level from the Cesium for Unreal Samples on the Unreal Engine Marketplace or create a new level. If starting a new level, make sure to populate the level with at least Cesium World Terrain and some lighting, either with CesiumSunSky or your own custom lighting. Check out the Quickstart guide to learn how to set up a new level with Cesium for Unreal.
For this tutorial, the Origin Longitude, Origin Latitude, and Origin Height of the CesiumGeoreference actor are set to -122.390533, 37.61779, and 0.0 respectively, which are the coordinates of for San Francisco International Airpoty (SFO), where the flight originates.
Step 2. Add the
PlaneTrack class will contain logic to process the flight data and generate position points for the spline which represents the flight path.
- Add a new C++ class by going to File —> New C++ Class… at the top left corner of the Unreal Editor. Select Actor as the parent class. Open the project in Visual Studio by going to File —> Open Visual Studio.
New C++ files can be found in the
Source folder of the Visual Studio project.
- Add the following code snippet to the project
.Build.csfile, which can also be found in the
Let's add some member variables to the
PlaneTrackclass to store the flight data, spline, and convert the data to the appropriate coordinate system. Import the necessary libraries and add the following public variables to
PlaneTrack.cppand initialize the
SplineTrackvariable in the
PlaneTrackconstructor as follow:
UDataTable variables will be set inside the Unreal Engine Editor.
Before moving on to the next step, compile the code. In the Unreal Engine Editor, click on Compile at the top tool panel:
If the code is well-formatted and correct, you will see the “Compile Complete” message at the bottom right corner of the Unreal Engine main editor. In this tutorial, compiling the code refers to this step.
Step 3. Bring in real-world flight data
Next, you are going to use real flight data from San Francisco to Copenhagen. This data is collected by FlightRadar24. The height in Cesium for Unreal is in meters relative to the WGS84 ellipsoid. The data is pre-processed to convert the heights from feet relative to mean sea level to meters relative to the ellipsoid. You can download the converted data here.
The flight data in Microsoft Excel.
In order for the
PlaneTrack class to access the data to perform coordinate conversions, you will use the Unreal Engine
DataTable to store the data inside of the project. In this step, you will create a data structure to represent the structure of the flight data.
PlaneTrack.h, insert this snippet of code below the imports to define the flight database structure:
The structure contains three member variables:
Height. These variables correspond to the column names in the raw datatable above. Also notice that the structure inherits from
Compile the code.
.csvdata file into the Unreal Engine Content Browser. Select
AircraftRawDatain the Choose DataTable Row Type dropdown:
Click Apply and double-click on the new
UDataTable object in the Content Browser to open the data table:
Troubleshooting: if you receive a an error saying that Unreal Engine is unable to import, check to see if the data is saved within the the project folder. This error often occurs if the data file is saved elsewhere.
Step 4. Add positions to the flight track
In this step, you will add some more code to the
PlaneTrack class to complete the rest of the functionality needed to create the spline path.
- Add the following imports and function to
Fill out the body of
LoadSplineTrackPointswith the code snippet below. This is where the majority of the computation will be done.
Compile the code.
- Navigate back to the Unreal Engine and add the flight track to the scene. In the Place Actors panel, search for "Plane Track" and drag and drop it into the viewport.
- Select the PlaneTrack actor and in the Details panel, look for the Flight Tracker category. Set the Cesium Georeference variable to be the Cesium Georeference variable in your scene and set the Aircrafts Raw Data Table variable to the data table added in step 3.
- Navigate to the Level Blueprint. Here you're going to add some Blueprint nodes to populate the spline path.
- Look for the Event BeginPlay node. This node is called at the very beginning in Play mode. Drag-and-drop the PlaneTrack actor from the World Outliner, drag a connection from this node, search and add the Clear Spline Points function node. When the spline is first added to the scene, it has two points by default. These two points are arbitrary and not needed in this tutorial, so they can be cleared with Clear Spline Points.
From the PlaneTrack object node, drag another connection and search for "Load Spline Track Points". Connect the Clear Spline Points and the Load Spline Track Points nodes, and connect the Event BeginPlay node to the Clear Splint Points node. The final Blueprint network looks like this:
Click Compile at the top left corner of the Blueprint Editor.
Since spline visualization is off by default, you can turn it on by selecting the viewport, pressing the ` key (usually under the Esc key) on your keyboard and entering in the
ShowFlag.Splines 1command. To check if everything every is set up correctly, click the Play button in the top panel of the main editor. You should be able to see the data points connected by a spline curve that starts at a terminal building in the San Francisco International Airport like this:
Step 5. Add the aircraft
The final step to complete the flight tracker is adding an aircraft that follows the spline path. For the aircraft mesh, you can use any model you would like. Here is a Boeing 787 aircraft model from TurboSquid that you can download for free.
The aircraft model in this tutorial is a Boeing 737 aircraft model from Sketchfab.
In the Content Browser, import the aircraft model into the content browser by selecting Add/Import —> Import to Game/[Path]/. You can inspect the model in the Static Mesh Editor:
Right-click in a blank area of the Content Browser and select Blueprint Class. When prompted to pick the Parent Class, select Actor. Name the class “BP_Aircraft” (BP stands for Blueprint). This class will contain the logic to move the aircraft along the spline path.
Double-click on the new Blueprint class to access the Blueprint Editor. Click on the green Add Component button at the top left corner and search for “Static Mesh”. Add it to the component list and name it “AircraftMesh”.
With this new component selected, locate the Details panel to the right side of the Blueprint Editor. Find the Static Mesh variable and in the dropdown menu, find and select the aircraft mesh you imported earlier.
Head to the Event Graph by clicking the Event Graph tab at the top of the Blueprint Editor. Right-click anywhere in the Event Graph and search for “Custom Event”. Name this event “MoveAircraft”.
Right-click again in the Event Graph and search for “Add Timeline”. The Timeline node will serve as a timeline to move the aircraft over time.
Double-click on the Timeline node to open the Timeline Editor. Create a float curve by clicking on Add Float Track button at the top left corner of the editor and give it a name. In this tutorial, the Float Track is called “Alpha”. Add keyframes to the timeline by right-clicking on the curve and select Add key to Curve. The final curve looks like this:
The first keypoint is at Time = 0, Value = 0 while the second keypoint is at Time = 1, Value = 1. To assign more precise values to a keypoint, select it and type in the values at the top left corner:
Check the checkbox next to Use Last Keyframe. If you would like the flight to loop once it ends, you can check the Loop checkbox as well.
Head back to the Event Graph and promote the Alpha output pin of the Timeline node to a variable by right-clicking on it and select Promote to variable. This will add a new variable to the left panel under Components:
Add a variable called Duration to the BP_Aircraft Blueprint class using the green “Add New” button in the My Blueprint panel. This variable will be used to determine how long the aircraft takes to fly along the path. Give it a type of float and click on the eye symbol next to it to make it public and editable in the main Unreal Engine editor.
Similarly, add the following remaining variables to the
- AircraftStartOffset: type
float; public visibility; used to determine where on the timeline the flight begins. The Slider Range and Value Range should be between 0 and 1, since the timeline goes between 0 and 1. These variables can be edited in the Details panel in the Blueprint Editor.
- PlaneTrack: type
PlaneTrackwith Object Reference; public visibility; used to store a reference to PlaneTrack object to retrieve the positions on the spline.
The final Components list looks like this:
- AircraftStartOffset: type
Complete the rest of the Move Aircraft custom event as follow:
To use a variable, drag-and-drop it into the Event Graph or right-click anywhere in the graph and search for the variable’s name. To call a function that corresponds to a variable, drag a new connection from the variable node and search for the function’s name.
Compile the Blueprint.
Next, you will add logic to connect the PlaneTrack spline to the Move Aircraft function and interpolate the position of the aircraft using the spline. In the same Event Graph, create the following node network below the Move Aircraft function:
Lastly, connect the two Blueprint networks together with the Set Actor Transform node:
Compile the Blueprint.
Navigate back to the main editor. Add your aircraft object to the scene either by drag-and-droppping the BP_Aircraft Blueprint into the scene from the Content Browser or search for “Aircraft” in the Place Actor panel.
There are several ways to trigger the Move Aircraft event. In this tutorial, you will trigger it on a key press. Head back to the Level Blueprint. In the Event Graph, add a Keyboard node (M is used here). Connect this Keyboard node to the Move Aircraft event as follow:
Compile the Blueprint.
With the BP_Aircraft actor selected, head to the Details panel and initialize the public variables Duration, PlaneTrack, and AirplaneOffset. Note that with Duration, the higher the value, the longer it will take the aircraft to travel the path. For Duration, a value of 100,000 works well. Modify the AirplaneOffset variable to start the flight at another point on the path. For example, since 0.0 is the start and 1.0 is the end of the flight, set AirplaneOffset to 0.9 to begin closer to the end of the flight.
With the viewport focused on the aircraft, click the Play button and press M (or the key that you chose to connect the Move Aircraft event to) to start the flight.
Switch between different camera views (optional)
You’re going to implement some camera switching to look at the flight from different points of view. This step is optional but it can add a nice effect to your project.
Using the Place Actor panel, add two new Camera Actors to the scene. In the Details panel on the right, drag-and-drop the Camera Actors onto the BP_Aircraft actor so that they become its children:
When the aircraft moves, the cameras will also move along with it. Adjust the cameras so that one is looking at the aircraft directly from the top and the other from a side view:
Once the cameras are positioned, navigate to the Level Blueprint. Add custom keyboard events for the side and top camera views:
Compile the Blueprint and head back to the main editor to view the result. In Play mode, you can press the keys you specified in the Level Blueprint to test the new camera switching feature.
Complete Source Code
Here is the complete source code for this tutorial:
Currently, the aircraft’s velocity is constant throughout the path. If your dataset has velocity or speed variable, you can use it to adjust the velocity of the aircraft depending on its position on the spline track. Similarly, you can adjust the heading, yaw, pitch of the aircraft with real-world data.
Now that you have completed the Cesium for Unreal tutorials, please visit the Community Forum to share your feedback on Cesium for Unreal and the tutorials, and to show the world what you’ve built.