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.

Cleaning up the AI Architecture- Soccer Game v0.0.5

The video below shows the latest implementations to the soccer game. In a nutshell, I cleaned up the AI architecture and implemented a Player Indicator.

 
 

Implemented modularity to the AI system

This month I focused on cleaning up the AI game architecture. As it turned out I was violating an OOP principle which states that:

A class should have only one responsibility.

In my case, one of the classes was not only responsible for assigning roles to each player, such as the attacker, defender and supporter role. It was also responsible for executing AI algorithms.

To remedy this, I created several classes responsible for AI operations. Specifically, I created an AI class responsible for Defending. And another class responsible for Attacking AI operations.

However, I went further and created an interface for these classes. Doing so provides modularity and flexibility whenever I need to change an AI strategy.

I also implemented methods in the AI to steal the ball from the attacking player. However, it does not know what to do once it gets possession of the ball, yet. I plan to fix this soon.

Added a Player Indicator

In the previous version of the game, it was somewhat hard to follow which player had possession of the ball. To remedy this, I added an indicator which shows who is the dribbling player and what is its heading. The indicator also points out the current defending player.

Improving the Soccer Kits

Finally, I decided to hire a game artist. I was not happy with the soccer kit texture; which was painted by me. He is currently working on the soccer kits, and I like what he has done as of now. Hopefully, by the next version update, I can show you a small video of the game with more realistic soccer kits.

 
 

Thanks for reading.

Become a documentarian of your work

I enjoy reading rags to riches stories. They are powerful, inspirational and they resonate with our emotions. Humble beginnings connect us. They are a reminder that no matter who you are, or how broke you are, you do have a chance to succeed.

Game Engine Beginnings. Circa 2013: Implementing the rendering engine.

Game Engine Beginnings. Circa 2013: Implementing the rendering engine.

Now and then, I search on Wikipedia the stories of successful companies. I look for any photos that were taken before they made it big. I enjoy reading their "About" page and get a glimpse of how it all started.

Success is not glamorous as the media sells it. In fact, It is a lot of time working in the dark. I know this fact first hand.

Game Engine Beginnings. Circa 2013: Improving the rendering engine

Game Engine Beginnings. Circa 2013: Improving the rendering engine

I worked over 15,000 hours in developing a 3D game engine. I would wake up at 5 am and work on the engine from 5am-7am before heading to work. I worked on it during my lunch break. And then again after work. And during the weekends, I would work on it for about 16 hours. I wanted to develop a 3D game engine so bad, that it became my life for about three years and a half.

Game Engine Beginnings. March 2014: Improved the engine to render textures.

Game Engine Beginnings. March 2014: Improved the engine to render textures.

Throughout this time, the thought of failure kept disturbing my peace. I kept thinking, what if I fail and have nothing to show? What if this is just a waste of time?

Game Engine Beginnings. April 2015: Improved the shadows in the engine

Game Engine Beginnings. April 2015: Improved the shadows in the engine

However, I found a way to combat it. I was inspired by Austin Kleon to document the humble beginnings of my game engine. At first, I was against this idea. In my mind, nobody would want to see the progress of my game engine. But Austin made me realize that it is the process, not the end product, that makes our work exciting and inspiring.

Game Engine Beginnings. July 2016: Collision Detection System implemented.

Game Engine Beginnings. July 2016: Collision Detection System implemented.

I want to let you know that you are not alone in your struggles. It is OK if your game is not glamorous yet. It is OK if your indie game studio is your room. That is OK if your game only sold ten copies. And there is something powerful about your struggles.

Game Engine Beginnings. Sept 2016: Improved animation and shadows effects.

Game Engine Beginnings. Sept 2016: Improved animation and shadows effects.

The finish line of success does not inspire people. It is the road you took to achieve it. It is your suffering, the pain, the obstacles that make your story interesting. It is what gives others strengths to go beyond they thought possible. It is your underdog story that connects us as human beings and inspires us.

Game Engine Beginnings. Aug 2016: Game engine first demo.

Game Engine Beginnings. Aug 2016: Game engine first demo.

So document your humble beginnings. Share photos and videos of your work in progress. Take screenshots of your game. Share pictures of your indie studio; even if it is your room. Write about your struggles and how you overcame them.

Game Engine Beginnings. Dec 2016: Added particle system to the engine.

Game Engine Beginnings. Dec 2016: Added particle system to the engine.

Don't worry if your game characters look like crap. Don't worry if your game mechanics suck. Don't worry if there are plenty of bugs. It is all OK. As indie game developers, we can all relate and do understand that it is a work in progress.

Game Engine Beginnings. Dec 2016: Tested the engine collision detection capabilities.

Game Engine Beginnings. Dec 2016: Tested the engine collision detection capabilities.

A final product is not interesting. It is not inspiring. What is, is your story.

So what is your story?

How long will it take to build a simple 2D Game Engine?

It took me three years to develop a 3D game engine. So I'm estimating that a decent 2D game engine should take about 1–1.5 years.

How long would it take to build a SIMPLE 2D engine?

I would estimate it should take you a couple of weeks. However, I doubt that you would call that an engine.

A game engine is a complex architecture. It is composed of algorithms and design patterns. All working together to render characters on a screen efficiently.

On the rendering side, your engine will need to render the different type of objects. Such as 2D images, sprites, text, etc. Thus, you will need to set up a Rendering Manager. The Rendering Manager will take care of all OpenGL operations.

You are also going to need to implement a Scenegraph. A scenegraph is a tree-like structure used in game engines to traverse game entities efficiently. The Scenegraph provides game entities to the Engine Loop. The engine loop then renders and updates the coordinate space of each entity.

In my opinion, a game engine is not an engine without a physics engine and a collision detection system.

How does a Physics Engine work? In a nutshell, A physics engine is responsible for solving the equation of motion and for detecting collisions. You can think of a physics engine as a continuous loop. It starts off by simulating an external force such as gravity. At each new time step, it detects any collisions, then calculates the velocity and position of an object. And finally, sends the location data to the GPU. This continuous loop creates the illusion that an object is falling due to gravity.

A physics engine does this by integrating Newton's Equation of motion. In computers, Integrals are evaluated using Numerical Integrations. Thus, you will have to implement at least the Euler Method. However, if you want a more precise method, you will need to implement the Runge-Kutta method.

To detect collisions, you will need to implement a Collision Detection System. A collision detection system works by detecting if bounding volumes are intersecting. You can set up your collision system to use spherical bounding volumes. However, they return many false detections.

A more precise detection system uses Convex Hulls as bounding volumes.

The intersection between Convex Hulls is computed using the Gilbert-Johnson-Keerthi (GJK) algorithm.

The GJK only tells you if two objects collided. It does not provide the Collision Contact Points. These points are essential to computing the proper collision response (impulse and resulting velocities) of collided characters. To obtain these Contact Points, you need to implement the Sutherland-Hodgman Algorithm.

The items listed above, are the minimum set of algorithms you need to implement to have a decent game engine. Nothing fancy or brag about. And per my experience, it should take you about 1-1.5 years to implement a 2D game engine you can be proud of.

P.S. Sign up to my newsletter and get Game Engine development tips. Or Get a Copy of my book below.