# Learn Orbital Mechanics in Unity Game Engine for Augmented Reality

Common Topics: offset, data, scale, unity, scaling

In this Insight, I’ll go over implementing basic orbital mechanics simulations in the Unity game engine as well as an approach to scaling the simulation for Augmented Reality applications. Unity is a great tool for prototyping games but also for animating physics models. The physics gets more interesting when you can watch all the interactions over time and see your formulas doing their work.

## Key Points

1.  Unity is a great tool for prototyping games and animating physics models
2. Gravity simulations are a great game design mechanic
3. Unity has builtin physics modules but not for orbital mechanics
4. Gravity and Orbits Solar System package can be used as a starting point
5.  Generate forces using algorithm (directionForce, forceValue, force) and apply to Rigidbody component
6. caling the entire 3D world can be done with auniverse scaler GameObject
7. Script to scale size and mass of each Unity game object
8. Initial speed and position offset also need to be scaled
9. Recommended to use Unity Game Engine for physics projects

I’ve been developing games for over 30 years and as a game developer, playing with gravity simulations has been a go-to game design mechanic. I’ve always been fascinated with gravity. Whether it’s playing with scaling gravity in a game, riding mountain bikes, or shredding at the skatepark, gravity is fascinating and fun to play with. I recently got the opportunity to work on an Augmented Reality, or AR, game prototype that uses orbital mechanics as a gameplay mechanism. One of the issues with AR applications is that you need to be able to let the user scale the models so they look the appropriate size for the location they are placed in the real world, relative to the user. For example, there’s room to place your virtual object in a far corner of the room, but it may appear too small from where the user is located. If the open space in the room is too close to the user, then it will appear way too large and spill over the edges of the display. In most applications, you can just set the scale, keep it constant, and move the camera in your simulation order to render the scene at the appropriate size. In AR the user is the camera, they physically move closer or further from the object. So it’s not something you can set in your code and be done with. However, with our solar system model it not only has to be scaled in size visually but the physics model needs to be scaled as well.

Unity has some awesome built-in physics modules that include gravity simulations, but they are set up for simulating gravity for a small object on a single enormous planet and not for orbital mechanics. So you need to add some of your own algorithms for simulating the interaction and motion of multiple planets. For my prototype, I found a great package for \$6.99 on the Unity Asset store, Gravity and Orbits Solar System. It’s a great starting point. Although there are a lot of ways this code could be improved with regards to design and style it’s a great functional example of an animating solar system. However, it does not support scaling the solar system, which is required for AR applications.

The approach used for the orbital mechanics motion is this:

1) Generate forces applied to your objects (planets in this case) using this algorithm:

a) directionForce = (SunPosition – PlanetPosition) / distance;
b) forceValue = (SunMass * SatelliteMass) / Math.Pow(distance, 2);
c) force = GFactor * forceValue * directionForce;

2) Then apply the force to the RigidBody component of your Unity GameObject using the RigidBody.AddForce() method.
3) So let the Unity RigidBody component determine the movement and position updates from the force calculated above.

The next problem is to be able to scale your solar system. The size of your planets, their distance apart, initial forces, and mass all need to be scaled appropriately.

In Unity, you might approach scaling your entire 3D world by making every GameObject (one planet in this case) a child of a single “universe scaler” GameObject where you set the scale in one place and all the children in the hierarchy inherit that scale, thus appearing the right size in relation to one another. The issues with this approach are that it doesn’t support scaling the physics simulations, (mass, initial force, etc.) and it also doesn’t maintain any scaling you’ve done in the Unity editor in order to differentiate your models (e.g. the different sizes of the planets) as is done in the Gravity and Orbits Solar System package. The pre-scaling done in the editor is just overwritten by the inheritance operation so all your planets based on the same Unity prefab (base model) will be the same size when scaled at runtime. The solution to this is to add a scaling script to each Unity game object that handles the scaling of the size of the model (transform.localScale) as well as scaling the mass of the Rigidbody component. It’s important that you scale the mass by the scale factor cubed.

newMass = originalMass * scaleFactor3

In C# code it would look like this:

```// scale the model’s transform (its appearance)
{
transform.localScale = originalScale * scaleFactor;
// scale the mass
Rigidbody rb = this.GetComponent<Rigidbody>();
if (rb)
{
// set mass
GetComponent<Rigidbody>().mass *=
(float)Mathf.Pow(scaleFactor, 3);
}
}```

The initial speed and position offset need to be scaled as well.

```satellite.GetComponent<InitialSpeed> ().speed =
InitialSpeedConstant*scaleFactor; Vector3 position =
centerPlanet.transform.position + positionOffset*scaleFactor;```

Also if you’re using the Gravity and Orbits Solar System package don’t scale the initial direction as it’s unnecessary and will scale the initial force inappropriately.

There are lots of ways to achieve the same outcome but that’s the approach that worked for me.

Unity is a great prototyping tool and adding an animated simulation for your orbital mechanic’s project, that works on almost any platform (PC, Mac, iOS, Android, etc.) will make it much more exciting and interesting to view. I would recommend the Unity Game Engine for use in modeling and animating any physics project. It’s free to download and really very easy to use.

## FAQ

### Whatis the Unity Game Engine?

The Unity Game Engine is a crossplatform game engine developed by Unity Technologies. It is used to create video games for web plugins, desktop platforms, console gaming, and mobile devices.

### What platforms does the Unity Game Engine support?

The Unity Game Engine supports a wide range of platforms, including PC (Windows and Mac), iOS, Android, Xbox, PlayStation, and Nintendo Switch.

### How much does the Unity Game Engine cost?

The Unity Game Engine is available in two versions: the Personal Edition and the Pro Edition. The Personal Edition is free for individuals and small businesses, while the Pro Edition requires a subscription fee.

### What programming languages does the Unity Game Engine use?

The Unity Game Engine uses C#, JavaScript, and Boo.

### What type of games can I create with the Unity Game Engine?

The Unity Game Engine is capable of creating a wide variety of 2D and 3D games, including RPGs, FPSs, platformers, and more.

Tags:
2 replies
1. jedishrfu says:

Great insight! I especially liked the description of scaling factors and how they a codependent on one another requiring a more nuanced program design.

Unity is awesome, we did a unity simulation of Challenger Deep where the user could fly through the canyon using an Oculus Rift for one implementation and similar code for an Android tablet implementation.