It is time for another installment of Godot (previous entries: introduction, 2D). This time, I have dived into the world of 3D. The goal is to recreate parts of an old time favorite: Kosmonaut. Something I remember playing a lot on my dad’s 286 with amazing EGA graphics.
The state of the game when writing can be seen in the short screen capture below. This is more of a tech demo status than a full game at the moment, but I hope you will still find it interesting. You can also get the complete source code.
Of course every scene can be created manually from individual elements. However, in 2D games it is common to use tiles to create large worlds from common elements. Godot’s analog in the 3D world is the GridMap.
A grid map is more or less a 3D tile map. Inside each tile (or cell, or whatever you call them in 3D) you can place an item. The way you create these items in Godot is to place each 3D element in a scene and then convert it to a mesh library. Then you can draw your 3D scene much like you can draw a 2D scene from tiles.
Once we have a world with a track (the grid map), we add a player to the scene (the yellow blob in the image above – I need to learn Blender to create a proper ship). The player scene contains the ship – and the camera. This means that the camera follows the player automatically – very convenient.
The player script is responsible for this ship’s movements based on user input. Inputs can either be pressed for a long time, used for sideways movement, or just tapped (i.e. the release is ignored), used for jumping. Each of the inputs are mapped to a keyboard key (or other input device) in the Project Settings dialog, under the Input Map tab. This feels a bit awkward to me and makes me lose the feeling of flow – but I don’t know how to do it better.
Getting gravity, jumping and bouncing to work was fun. It reminded me of coding games on my Atari ST. The character controller code is really basic, but can become super complex if one does not think through the order of the things you do. I ended up exporting some tunables so that they can be adjusted in the editor, just to figure out how high to jump and how fast to run the ship.
Figuring out crashes was easier than expected. Through the KinematicCollision objects retrieved using get_slide_collision, the normal of the surface collided with can be retrieved. From there on, it is easy to determine if the ship is sliding along a wall, or just hit it head on. The latter is deadly and the crash signal is emitted.
The crash signal leads to the world script. Here we handle the second death scenario – falling. This is a handled by the world, rather than the player, as the world knows what the lowest possible y value is.
The world also refers to two invisible scene elements used to mark the starting position ($StartPosition) and goal position ($GoalPosition). The first is used when resetting the player, while the latter is used to determine when the goal has been reached (duh).
When starting a run – or restarting for the millionth time, and when completing a track, a short sequence is shown. These sequences are realized using a CanvasLayer, Sprites and an AnimationPlayer. The canvas layer allows us to add 2D contents on top of (and behind) a 3D (or 2D) scene. Each such layer is broken into a scene of its own that encapsulates one sequence. Each scene have a small script interface with a play function and a finished signal.
When showing a sequence from the main world script, the script is started and then the calling function yields until the finished signal is emitted, as exemplified here. Very convenient!
One more word about the sequences: the animation player is a really convenient way to control all properties of a scene using key frames. It can even be used to call functions. I really like this.
On a side note, I found my first Godot docs bug when writing this post. The pull request is right here ;-)