Working Collision

Dev Blog #4 by George Baron

20 October 2019

The title says it all really. This week I finally got the collision (and response!) working properly. Honestly I was really quite proud of this, as fixing all of the bugs was a laborious and very dull way to spend my time. The results are really great though! Being able to actually collide with geometry adds an incredible amount of depth to the engine and it makes the world feel a hell of a lot more real. Having this working has really done a lot to boost morale.

I am not entirely finished with the collision system yet though. Yes, it works. But it’s also implemented in a pretty brute force way. This means it works but takes no concern for efficiency or scalability. Before moving onto the next thing I need to optimise everything, basically! Who cares about that though, let’s see what it looks like. ;)

The character colliding with one of the objects in the playground level.

As you can see, right now the collision is working pretty effectively! I can walk into objects and slide along the surfaces of them properly, as opposed to just stopping dead in my tracks. I also don’t get caught on corners of objects and the system is general enough that I can slide across any kind of concave or convex surface. Everything is based on checking if I have collided with individual polygons, so the sky’s the limit! Quite literally actually. I have now opened up the possibility of adding another dimension to the game - I can go up stairs!

The character climbing up some stairs. Weeeeee!

This felt especially good to see as it opened up so many possibilities for my level design. I can add any kind of verticality to my world!

So Why Is It Not Finished?

All those GIFs make it look like it’s all fine and dandy. Time to move onto the next challenge, George! Well, that’s almost true. In actuality the collision system does not scale very well at the moment. The problem is that I am currently testing for collision against every single collidable polygon in the level. The playground level isn’t very big so it doesn’t amount to much here, but any properly designed level is going to have far more collidable surfaces and the engine needs to be able to cope with that.

Luckily, the solution is actually quite simple. The first thing I need to do is structure all of my collidable polygons into an octree. This means that I will only be checking for collision against collidable polygons that are close to the character. Anything that is far away can just be ignored. This alone would vastly improve performance and most importantly make things scalable. Just think about it - it doesn’t matter how many collidable polygons I have in a scene, as long as they are reasonably spread out it won’t affect performance!

The other improvement I am going to make is to shift collision checking onto another thread. At the moment, the entire engine is running on a single CPU core. This highly under-utilises modern CPUs and it would therefore be beneficial to shift some of the workload onto another core. My collision system is an ideal candidate for this as there is only one channel of communication between it and the game - I provide an input (my character’s position) and it provides an output (my character’s updated position.) This single channel of brief communication means I can move all of the heavy calculations onto another thread and briefly sync up my game thread with the collision thread when I need to pass data between them.

What Comes Next?

After I am done with that lot, I am planning on changing focus slightly. I have spent a lot of time recently working on the engine and it is getting to a point where it is pretty useable. So now, I think my next step is to focus on art somewhat and give my engine a spin with some actual level geometry. My plan is to therefore spend some time 3D modeling, texturing and level designing. I have already planned out something to do, so it’s just a matter of actually doing it!

The other thing I might focus on reasonably soon is some more graphical effects. Currently, the engine is sorely missing some shadows. Shadows are also very important for level design, so I might start working on this sooner rather than later!