What is this?
AstroFlight is a 2D puzzle game based on classical mechanics, astronomy and further principles of physics, taking on a new approach to how gravity works in video games.
Developed within the scope of a beginner's practical, it is designed as a pure OpenGL project that doesn't use any external ressources such as textures, except for the GUI font.
A singular approach to gravity
Video games tend to use a very simplified gravity model: While the player is in the air, he gets "pulled" towards the ground with constant acceleration.
This is exactly how gravity feels to us.
However, on a bigger scale – for example in planetary systems – this looks a bit different: Objects with mass move towards each other and their accelerations are based on their masses and on the distance between them. This leads to the N-body problem, where N bodies accelerate each other, or in other words: Each of the N bodies has N‑1 forces acting upon them.
AstroFlight chooses a path inbetween: In a crammed system with N astronomical objects (planets, moons,...), N forces are acting upon the player spaceship and impacting its movement. The N bodies themselves are fixed to ensure a rich variety in different levels and, to some extent, predictable behavior for the player.
To control or not to control: How does the game work?
The goal of AstroFlight is simple: The player launches from one of the planets, and if he lands on a different planet – the one marked with a flag – he wins the level.
If he collides with a different planet or stays outside the screen for 5 seconds, he loses.
What makes this more interesting is that the player himself has no control over the movement of his spaceship. All he can do is modify its launch angle and its launch velocity, and gravity – the forces acting upon the spaceship – will do the rest.
In other words: In theory, each level can be described with a set of differential equations, and the player's goal is to find initial conditions for one of the objects – the spaceship – such that its trajectory intersects the radius of one other specific object, the goal planet.
This is the core idea of the game: Even though the player is controlled by forces he has no control over, he has to learn how to use them to his advantage in order to win.
Exploiting the existence of multiple solutions
The player can actually win the level using different initial conditions. Depending on the level structure, this can mean that the possible trajectories are close to each other,
but it could also mean taking entirely different paths around the obstacles. We can use this to encourage the player to play the same level multiples times.
In the current state of the game, for example, the player can drop up to 3 "terraforming" boxes after launch. These boxes are, just like the player, accelerated by gravity, and supposed to land on planets. Once a terraforming box lands on an obstacle planet, it will mark the planet with a blue shine (the atmosphere) and reward the player with additional points. This means that, in addition to the goal "navigate to the goal planet", we can define the additional goal "find a path such that you can terraform 3 planets".
There's room for more bonus goals that have not yet been implemented:
- Land directly on the flag marking the goal planet, for example on its backside
- Collect a satellite placed somewhere in the level, i.e., fly through exactly this spot
- Find the shortest (or a very short) path
- Maximize your score
In addition to the gravitational force, there is another very important force in classical mechanics: The centrifugal force. This force acts upon a mass moving on a circular orbit, depending on the distance to the center and acting in the opposite direction. If in the center of this circle there's another mass, both gravitational force and centrifugal force act upon the moving mass, and by setting these forces equal, we can find the angular velocity needed for the moving mass to stay in the orbit without increasing or decreasing its distance to the center.
The result is a moon rotating around a planet – a new moving obstacle for the player.
Two objects of equal size don't necessarily have equal mass – you could for example consider a solid planet and a gas planet, which have entirely different densities.
The same concept applies to AstroFlight: Even if obstacles are equally large, they aren't equally dangerous, and the player can't judge by simply looking at their sizes.
Furthermore, between planets the forces applied by them cancel each other out.
Therefore, a helping feature has been implemented: By pressing a key, the magnitude of the force applied in each spot of the level is visualized, with green meaning "weak" and red meaning "strong".
This has been realized by putting a grid over the screen and computing the magnitude of the force applied in each vertex.
AstroFlight has been developed without any pre-existing graphics/physics engine. In order to realize this project, I learned the basics of OpenGL 3.
This turned out to be a challenge, as learning materials for OpenGL 3 are still rare: Google search results are usually based on OpenGL 2 and therefore use deprecated functions,
OpenGL 3 textbooks are usually supplemental and not aiming at beginners. The result is a rather messy code and a rough long start to even get the main functionalities of the game running.
Therefore, for getting into OpenGL for the first time, even in 2D, I clearly recommend starting with simpler projects so you can actually focus on one thing after another.
Even though I don't sound too satisfied at this point, the practical was a huge success for me. I feel confident with the basics of OpenGL 3 and GLSL, and the concept of the game turned out to be working and to be a fun foundation opening up lots of possibilities.