I spent some time evaluating using Lua in one of our games. I hadn’t spent a large amount of time with the language, so I wanted to implement some rudimentary collision detection code to see how it worked. This might be helpful to you, but it was mostly just for me to get a feel for working in the language in a game development context. This is how I went about it in Lua.
TLDR; I liked it! It can offer a lot of power to the end user if integrated well.
Points and Distance
I set up an easy way to work with points and track distance. In come cases, this can be helpful in cutting down on how many collision tests we perform, so I wanted to include it in the kit. This is in a separate file, points.lua.
2D Rects
I set up an easy way to work with rectangles, based losely off of other implementations. I created an AABB collision test. This is basic, so we’re assuming axis-aligned. This could be suitable for a number of 2d implmentations. This is in rects.lua.
2D Circles
I added the usual function to build the circle. Testing circles is fairly easy. To do that we just need to test the radius and the point. We’re basically using distance between the points to check it. The contents of circles.lua is this:
3D Cubes
I set up an easy way to define the cube using the standard information. This test relies on axis alignment as well. To do this, you have to have all 3 axis colliding. If you’re missing one, the cube could be above it, below it, or off to the side. So, all 3 means collision. Here’s what that code looks like.
Putting It All Together
So, what does this basic implemtation look like? How might we use it? You might use these functions in a really basic game, maybe to do some quick tests for grid-based games. Or, if you’re really smart, you’ll use these to spring board into better, full featured implementations. Here’s how these look in use. This is just a simple way to test the functions. Here you go!