The purpose of a scenegraph in a game engine

Scenegraphs are generic trees which provide an efficient way to traverse game entities for rendering.

As the image below shows, a scenegraph node can have as many children as it desires. This property is in complete contrast with a binary tree; which allows at most two children per node.

On each game tick, the scenegraph traverses its lists and sends each game object to an Engine Loop. Where it gets rendered, and its coordinate space gets updated.

Aside from its fast traversal property, a scenegraph provides a clean method to transform a child node space with respect to its parent node space.

For example, in the image below, both the soccer player and the soccer ball are children of the World Entity.

Thus, if the world entity (grid) rotates, both children nodes will be affected by their parent space-transformation, as shown below.

However, a child node does not affect the space of a sibling node, nor its parent. Thus, rotating the player or the ball about their y-axis does not affect either or, nor the World entity, as shown below:

Now, if the ball was a child of the soccer player, as shown below:

then rotating the player causes the ball to rotate as well.

A scenegraph provides a clean method to establish a parent-child relationship and fast traversal of its entities. Such feature is useful when the number of game objects grows, and efficiency is of utmost importance.

Hope this helps.

A sneak peek at the Game Engine's API

I know that I tend to talk a lot about my game engine. I also noticed that haven't given you a sneak peek at the Game Engine's API.

When I started development of the engine, one of my primary goals was to create an engine with an user-friendly API. That is the API needed to be compact, small and easy to remember.

In short, I wanted an engine that helps me focus on developing a game rather than wasting time reading through tons of API documents.

So, let me give you a sneak peek.

Initializing Objects (3D models)

The engine works hand in hand with Blender 3D. The engine includes a Digital Asset Exporter, written in Python, which exports your 3D models done in Blender, into data the engine understands.

The initialization is quite simple. For example, let's say that you have modeled this 3D model in Blender and you want to use it in the engine.

After you run the python script, a file is generated. This file contains all the attribute data of your model, such as vertices, normals, UV maps, etc. This file is copied into your project.

In your initialization routine of the engine, you ask the engine to render this model by just calling the init method:

myModel->init("guardian","guardianfile.u4d");

The method requires the name of the 3D model and the name of the file you imported.

If the engine confirms that all the 3D model data is valid, it loads all the rendering information into the engine.

The only thing left to do is to add the model into the scenegraph. You do so by calling the addChild method:

addChild(myModel);

And that is it. The engine will render your model on your mobile device.

Enabling Lights and Shadows

WIthout light, you can't see. And without shadows, you can't make out visual details. Currently, the engine allows only one Light object per scene. That is, the light object is implemented as a singleton. You need to initialize a Light object, else, the whole scene will appear dark. Luckily, to initialize a light object is as simple as doing this:

//Create a light object
U4DLights *light=U4DLights::sharedInstance();

//Translate the light
light->translateTo(0.0,10.2,1.0); 

The engine gives you the flexibility to choose which model to cast shadows and which one not to. Again, doing so is very simple:

myModel->setEnableShadow(true);

The engine will cast a shadow on the selected objects:

Enabling Physics and Collisions

If you want a model to be affected by external forces, such as gravity, or any other applied forces, the engine gives you the flexibility to do so with one line of code. To enable such feature, simply call the enableKineticsBehavior method on the model.

myModel->enableKineticsBehavior();

And if you want to enable collisions between objects, you simply call the enableCollisionBehavior method:

myModel->enableCollisionBehavior();

The engine will enable gravity and collision properties on the selected objects:

Initializing Animations

Playing an animation is as simple as the initialization method explained above. 100% of the time, you would have rigged and animated your model in Blender. The Digital Asset Exporter also imports armature and animation data into the engine. All that you need to do is provide the name of the animation and the name of the file containing this information. To initialize an animation, you create an animation object and load animation data, as shown below:

walkingAnimation=new U4DAnimation(this);

if (loadAnimationToModel(walkingAnimation, "walking", "walkinganimation.u4d")) {
}

To play the animation, simply call the play method:

walkingAnimation->play();

If the animation has been loaded properly, the engine will play the animation:

As you can see, you can load a model onto your mobile device using a few lines of code. And the engine allows you to enable shadows, collision, and animation with a single line of code.

Thanks for reading.

Tips to make Game Engine Development a joy

Developing a game engine is an insanely complex task. Not only will you spend thousands of hours trying to get the first version of your engine to work, but it will also test your persistence. Thus, developing a game engine is more of a psychological feat than a technical one.

Before you embark on this journey, I have some tips for you.

Play with other engines

Before developing your engine, spend some time developing a couple of games and using a couple of game engines. Use this experience to gather features that you like and dislike from each engine. For example, you may like how friendly an engine's API is versus the other.

Keep this in mind: Developing an engine does not only mean implementing amazing graphical features. It also means developing a user-friendly API which developers love.

View it as your favorite hobby

Again, developing a game engine is insanely difficult. I can't even express how difficult it is for one person to develop one. And not everyone has the stomach to work for thousands of hours before seeing their creation come to life.

So, if you want to see this through, you need to view engine development as a hobby. Don't rush, just sit back, open your laptop and enjoy. It may take you one year, two years, five years, who cares. Just enjoy and fall in love with your engine.

Is OK if some features are half baked

Not every feature in your engine needs to work correctly. Not in the beginning anyway. If rendering Shadows work 70% of the time but fail within some conditions; make a note of it, take a break and move on.

Don't kill yourself over it. Remember, this is your hobby, your favorite pastime. You have no deadlines, no nothing. All you need to do is promise yourself that after a while you will come back to fix the issue.

It is important to consider that when you start the journey to develop your engine, you will not know everything about computer graphics. And you will not know what you don't know.

Features may not work as you hoped for initially, but this is OK. It is a process of failing and learning. Let me give you a recent example.

Last week I ported the engine from OpenGL to Metal. As I was doing the porting, I noticed that some implementations I had done a couple of years ago were incorrect. At the time, I thought that they were correct. But the experience that I've gained over the years spot these mistakes right away.

Don't let perfection immobilize you. If a feature is not working 100% as you intended, make a note of it, take a break and make a promise that you will come back to fix it in the near future.

Thanks for reading

Porting the game engine from OpenGL to Metal

If you are not aware of, Apple came out with its own graphics library; similar to OpenGL, a couple of years ago. The name of this graphics library is Metal.

When Metal came out, I had barely finished the first stage of my game engine. Back then, I thought that it would be a good idea to port the engine to Metal. However, I resisted in doing so, until Metal became stable.

Well, on the weekend of July 4th, 2017, I happened to play around with some Metal demo apps, and I was impressed at Metal's power. It was then that I decided to use my four-day weekend learning and porting the engine from OpenGL to Metal.

And so I did.

I spent Saturday morning to Tuesday night learning Metal in depth. I didn't even go out at all during those four days. I was focused and excited to learn about Metal.

On Tuesday night, I laid out a plan on how to port the engine from OpenGL to Metal. All in all, the porting took about two weeks. There are some minor bugs to fix, but all in all, I'm pleased with the results. Here is a screenshot of the engine using Metal.

 
Engine using Metal

Engine using Metal

 

So what did I learn?

Most of what I know about Metal, you can find it here. I wrote these articles a while ago,; I used them as a reference during my review and the porting. But here are the main things that you should know about Metal:

  • Metal is not as convoluted as OpenGL. Metal is clean and has the concept of Objects. You can think of it as an Object-Oriented language, but for graphics. It is amazing to work with. Seriously.
  • Metal is less forgiving than OpenGL. During the porting, I noticed many incorrect Computer Graphics implementation done in the engine. At first, I thought that the issue was with Metal but then realized that I had made several mistakes using OpenGL. OpenGL did not make a big deal about it, but Metal did mind and kept spitting out error messages. For example, I had incorrectly implemented certain aspects of Normal Mapping, Shadows, and Vertex-Skinning.
  • The Perspective and Orthogonal projection matrices you use in OpenGL based games will not work on Metal. You need to compute a different projection matrix for Metal. The reason is that Metal uses a different coordinate system than OpenGL. I will write more about this issue later. However, if you need an example of these matrices, send me a message, and I'll provide you with one.

It is amazingly simple to work with Metal. However, since there are not plenty of tutorials out there, it may be hard for a newbie to get Metal working.

If you have experience working with OpenGL and have a good grasp of computer graphics, Metal will be a piece of cake to learn. I can guarantee that you will love it. However, if you are a newbie, keep in mind, that it may take a while for you to grasp Computer Graphics concepts and Metal at the same time. Hopefully, these articles can help you out.

Thanks for reading.

Implementing a Pathfinder algorithm- Soccer Game v0.0.6

In this version of the game, I implemented a Pathfinder algorithm. As the video below shows, the AI system computes a path towards the goal. Notice how the white jersey player drives the ball towards the goal.

 
 

Implementing a Pathfinder algorithm requires knowledge about Graph Theory and the Dijkstra's algorithm. I've heard about these topics, but I wasn't well versed on them. So I decided to spend my weekend reading about them. I did learn a lot. However, as I was about to implement the algorithm, I realized that the Dijkstra's algorithm might not be suitable for soccer.

Let me explain.

In Graph Theory, a graph is composed of nodes and edges, as shown in the figure below:

A node can represent a location. An edge represents a connection. For example, you can go from your Home to the School by taking the path towards the Park. Or by taking the path towards your friend's house and CoffeeHouse.

By scanning the image, you can see that the shortest path from your home to school is towards the park (only takes two connections). However, this route may not be the fastest.

If each edge is given a weight; in this case representing traffic, then the fastest path is the one towards your friend's house. The Dijkstra's algorithm is used for these instances. It analyzes a graph and determines the fastest path between two endpoints.

I figured that I could use the Dijkstra's algorithm in the game. My idea was to compute the path a dribbling player can pursue to reach the goal.

However, the Dijkstra's algorithm works well for static nodes. It does not operate well in a dynamic environment such as in soccer. For example, a path computed at time t=0s will be invalid at time t=1s.

So, instead of using Dijkstra's algorithm, I decided to implement a "Look and Go" algorithm. The algorithm works as follows:

Once a player obtains possession of the ball, the algorithm divides the player's surrounding area into 36 positions. The algorithm then scans each location. If an opposing player is close to the location, the algorithm discards the position.

The algorithm then analyzes the location that would bring the player closer to the goal. The AI system uses this information to move the player towards that direction.

Pathfinder -2.jpeg

Does this algorithm work? It works quite well as shown in the image below.

As you can see, unlike the Dijkstra's Algorithm, which computes a complete set of paths, the "Look and Go" algorithm analyzes one path at a time and is more suitable for dynamic environments.