Gravity Engine supports a trajectory prediction feature that allows the future paths of massive and massless objects to be shown. The feature is controlled by inspector elements in the Gravity Engine and operates on objects that have a Trajectory script attached to a child of NBody objects. Support is also provided for creating time markers at fixed intervals along the trajectory and optionally labelling the time markers with the future time.
Trajectory prediction works by cloning the current state of the simulation and then evolving all the objects forward in time for the specified trajectory time. When running, the current and future-most configurations are evolved each frame. If the trajectory changes (due to e.g. user input changing a velocity) then the future path must be re-evaluated. If this occurs often in a scene with a large number of objects this can affect frame rate. Gravity Engine provides a parameter to limit how often re-computations can occur as a simple mechanism to limit this performance issue.
This tutorial will demonstrate:
- How to setup trajectory prediction
- Specification of time markers
- Configuration for time labels
- Run-time changes in velocity causing new trajectories
- Enabling/disabling trajectory prediction during runtime.
The tutorial is available as a video. A summary of the steps is provided below.
Trajectory Prediction Setup
- Add a GravityEngine object to the scene.
- Add a Mission Camera to the scene. (This is a camera that can rotate around the origin using arrow keys as input). The Mission Camera has a Main Camera as a child, so delete the Main Camera that came with the default scene. Set the length of the camera boom (the relative position of the mission camera) to Z=-35.
- From the Prefabs folder add a BinaryStars.
- Create a new empty game object and name it “Spaceship”. Attach an NBody component. Set the mass to 0, the position to (0, -20, 0) and velocity to (0,2,0).
- Drag a SpaceshipModel prefab onto Spaceship, making it a child of Spaceship.
- Note the script “SpaceshipWithTrajectory” is already attached to the SpaceshipModel (NOT the Spaceship).
The script is attached to the Spaceship model because it also handles controls that change the orientation of the spaceship model. (The logic in this script for input controls would more commonly be put in a game controller script – but putting it in with the spaceship simplifies the tutorial).
Setting up the Trajectory on an Object
- Create an Empty Game Object using the Menu Game Object:Create Empty
- Name the game object “TrajectoryGO”
- Drag the TrajectoryGO onto the Spaceship, making it a child of that object.
- Add a Trajectory component to the TrajectoryGO (Add Component button in the inspector). As you do this a LineRenderer component will also be added automatically.
- Select a material from the resources folder and drag it onto the TrajectoryGO
- Set the LineRenderer width to 0.4
Setting up the Trajectory in the Scene
- Select the GravityEngine object in the Inspector.
- Open the Trajectory Prediction foldout
- Enable trajectory prediction
Press PLAY and observe the trajectory in the scene.
The length of the trajectory is controlled by the GravityEngine “Trajectory Time” attribute.
Adding TimeMarkers and Time Labels
- Select the Trajectory component attached to the TrajectoryGO.
- Drag the prefab TimeMarker into the “Time Marker Prefab” slot.
- Set the “Time Mark Interval”. e.g. 2
Press PLAY. Observe that there are now time markers along the trajectory every 2 seconds. These are oriented so that the z-axis of the time marker object is aligned with the trajectory direction.
Time labels can also be added. In order to display UnityEngine.UI text elements, the scene will need a Canvas.
- Add a Canvas element to the scene. Change the name to TimeCanvas and set the Render Mode to “World Space”
- Drag the TimeCanvas into the Gravity Engine Trajectory Prediction “Canvas for Text” field.
- Select the Trajectory element in the SpaceshipTrajectory object. Drag the TrajectoryText prefab into the “Time Text Prefab” box.
Press PLAY and you will see a scene with time labels next to each of the time markers along the scene. By default the text is aligned to the camera. It can be aligned to the trajectory by enabling the “Align Text to Traj.” checkbox in the Trajectory element.
To add trajectories to the binary stars, copy the existing Trajectory object and make it a child of the star. Next, change the material to a different colour.
Dynamically Changing Trajectories
The SpaceshipWithTrajectory script has an Update() method that collects user input and changes the velocity of the Spaceship. <Space> stops the evolution and WASD control the ship orientation. Thrust is controlled by the +/- keys. The ApplyImpulse() code in the GravityEngine will automatically recompute trajectories if they are present in the scene.
Note that when done frequently the re-computation can lower the frame rate. This can be limited by an attribute in the GravityEngine inspector “Frames Between Resets”. This enforces a gap between subsequent reset trajectory requests.
The display of trajectories can be toggled with the ‘T’ key. This is linked to a call to GravityEngine.instance.SetTrajectoryPrediction(bool).