I've been a programmer for over a decade, professionally for the last five years, but I've never really done any games programming. I've had a look at some tutorials but most of what I've found is geared towards people with no programming experience, so I've tended to get bored and moved on. So, after grasping the basics of the game loop, I've decided to dive right in.
As my first project I am programming a basic 2D top down maze game (a Pac Man clone). I've got all the basics working fine, and am now thinking about optimizations. I have an idea to improve the performance of my wall collision detection but I'm interested to know the 'proper' way of doing it.
At the moment my algorithm is basically:
- Check direction player wants to move in
- Loop through all walls and see if any are at that position
- If there are no walls, move the player
This is working fine, but I imagine if I scale it up that the act of looping through all the walls each time will become an issue. Due to the simplicity of the game and the speed of modern computers, it might not ever be noticeable, but I'd rather be using the cycles for something else.
So it occurs to me that as this data is never changing, I could just build a lookup table as part of my map creation within my level editor. I've heard about similar things being done for more complex 3D games. I would then have a lookup table that stored each possible player position, each possible direction, and whether the player can move there. This means that on each move, I can just do a quick check to the lookup table to see if the move is possible, rather than repeatedly looping through a load of static data every single frame.
Is this a common thing to do? Or are neither of these approaches any use, in which case what is the best way to handle this?