game development — Blog - Harold Serrano | Game Engine Developer

game development

How to Implement a Scene Graph in ECS: A Simple Level-Based Approach

In the previous post, I mentioned that the Untold Engine lacks a way to propagate transformations (position, rotation, scales) from a parent entity to a child entity.

You can see this problem in the video in the previous post; The car moves but the wheels do not rotate. Moreover, the steering sucks.

What I need to do is implement a Scene Graph into the engine. Doing so, allows me to set the F1 car body as the parent node and the wheels as children nodes. The goal is to implement a Scene Graph so that the steering looks like shown below.

 
 

The problem is:

How do you implement a Scene Graph in an ECS architecture?

What is a Scene graph

It is a hierarchical data structure that helps manage spatial relationships between objects in a scene. In game engines, they are used to manage hierarchical transformations, where a parent's transformation (position, rotation, scale) affects its children.

One key feature of a scene graph is that objects are arranged in a tree-like structure, where transformations propagate from parent to child. For example, if a robot's arm rotates, its hand and attached tools rotate.

OOP vs ECS differences

In object-oriented programming, implementing a scene graph is straightforward because objects can reference their parents and children. It allows for simple, recursive traversal and updates of hierarchical transformations.

In contrast, in ECS, entities are represented by IDs (like uint64), which are simple numeric values with no built-in structure or relationships. This raises the question, how do you propagate transformations in a hierarchy when all you have are IDs?

I searched for answers online and could not find anything helpful. So, I devised a crazy idea: Traversing the scene graph by levels.

Traversing the scene graph by levels in ECS

A scene graph is a tree with different levels, correct?

  • The root node is at level 0
  • Children are at level 1
  • Grandchildren are at level 2, and so on.

I thought, "What if I could use these levels to simplify traversal?"
For example:

  • First, fetch all entities at level 0 and update their transforms.
  • Then, fetch all entities at level 1 and update their transforms, applying the transformations of their parents.
  • Continue this process for level 2, level 3, and so on.

So, I introduced a component in my engine that tracks entities' parent IDs and their current level in the hierarchy:

struct HierarchyComponent{
 var parent: EntityID
 var level: Int
}

Whenever a child is assigned a parent, the system calculates and assigns the child's level as the parent's level + 1.

For example, a car entity, wheel entity, and a rim entity start at level 0. If the wheel is attached to the car, the wheel's level is updated to 1. If the rim is linked to the wheel, the rim's level becomes 2.

Traversing and propagating hierarchy transformation works as follows:

  • First, process the car (level 0) and calculate its world transform.
  • Then, process the wheel (level 1) and combine its local transform with the car's world transform.
  • Finally, process the rim (level 2) and combine its local transform with the wheel's transform.

This approach ensures that parent transforms are always processed before their children, eliminating the need for recursive traversal.

Results

To get this working, I needed to modify the F1 model in Blender. I ended up breaking it up into F1 car body, steering, wheels as shown below.

 
 

Once the meshes were broken up, I was able to set up hierarchies between meshes using the new Scene Graph. Once I hit play, the steering looked a lot better.

 
 

Implementing this idea was simple and I didn't have to deal with recursive functions. I'm sure there are flaws but out of the other crazy ideas I came up with to solve this problem, this one was the cleanest and the easiest to implement.

Let me know what you think? How would you implement it?

Building a car racing game with the Untold Engine - Part II

I've been building a video game with the Untold Engine, a game engine I developed. I wanted to create a racing car video game for my kids. I figured that it would make them happy. You can read part I here.

I showed the current state of the game to my kids. To my surprise, they didn't like the direction of the game.

 
 

I was wrong in assuming they would like a cartoonish type of video game. I asked my son what he didn't like, and he replied that he wanted the car to be more real and to look like an F1 car. I was a bit "hurt" (lol) and also surprised. I guess they have better taste than I do.

So, we went to cgtrader.com and found a car they loved. Here the F1 car he chose. The video below shows the F1 car in a PBR renderer a made a while back.

 
 

Now that I knew what my kids wanted, I tried to render the car in the Untold Engine, and it failed to load. The reason is that the engine associates one entity id to one mesh. The model had several meshes. So, I modified the engine to handle one-to-many relationships, i.e. one entity can have many meshes. After doing so, the Untold Engine was able to render the F1 car.

 
 

What I did next was to buy a race-track asset for the game. I added control points (path) to help my kids drive the car; I call it "AI Assist" lol.

 
 

However, once I hit play, I realized several things were missing in the engine:

  • The wheels are not rotating.
  • The car's steering sucks.
 
 

Ideally, we want the car to be the parent entity and the wheels and the steering to be child entities. By doing so, the wheels can rotate independently as the car drives. However, the engine does not have a concept of "Transform Hierarchies". I need to figure out how to do so in an ECS architecture.

If you have any ideas, let me know.

Thanks for reading.

Building a City Driving Game with the Untold Engine - Part I

As stated in my previous post, my goal is to improve the robustness of the Untold Engine by focusing on developing various games.

I was thinking of a game to make, and I thought about how much my kids love car games. So, I decided to develop one.

I went to BlenderMarket to check out any car assets I could use, and I found a nice City asset.

 
 

My idea is for the car to drive around the city. Since this game is mainly for my kids, I figured that it would be a good idea to add some AI assistance for them. By AI assist, I simply mean a path the car will follow throughout the game. That way, my kids do not have to worry about driving, and just enjoy the game as the car speeds throughout the city.

So, I opened up Blender and added control points representing the path. I then wrote a simple script to export the control points into an array that the Untold Engine can use as waypoints.

 
 

Since the Untold Engine has a Steering System in place, it didn’t take long to hook up the car object and make it follow a path; it took only one function call.

I wanted a camera to follow the car. This was also simple to do. The Untold Engine has a function that allows for a camera to follow a model at a determined offset.

In no time, the car was driving around the city following a determined path.

 
 

So, now is time to make the game entertaining. I’m thinking of adding power-ups throughout the city which will make the car go faster. Such mechanics would be perfect for testing the collision system, which I have yet to develop. I also want the game to be played on the Apple TV. However, for this to happen, I need to make the game educational so that my wife approves of it. lol.

So, what issues did find?

Even though the setup took a few minutes, I saw problems right away. For example,

  1. the engine does not have any culling mechanism.
  2. Loading times were too long and
  3. Z-fighting was apparent.

Stay tuned for part-II

Useful algorithms to use in your game engine

In this video, I explain five algorithms you may want to implement in your game engine and how they are used during Collision-Detection.

 
 

In case you want to know more about these algorithms, these articles provide an in-depth explanation of how they work.

Thanks for watching.

Useful tips for Game Engine Developers

Useful tips for Game Engine Developers

This live stream will go over tips and insights that will help you develop a game engine. I have accumulated these tips throughout the development of my game engine and I'm sharing them with you. Enjoy.