I have been working on some tiles for this project today to help me define a more solid overall look of the game. The task for today will be to update the engine to support custom maps and these nice tiles. The water rendering is also coming along so I am hoping I can have these things integrated by tomorrow.
Also, something that I have rarely seen in a platform game is genuine AI companions that can join the player on their journey. I guess this is in part due to the fact that it can be hard to create an AI that can navigate and jump between platforms properly, however I have devised what could be a great solution to this problem.
One of my ambitions for a while was to create my own efficient ray casting function which I finally managed yesterday, which is great news because ray casting has so many cool applications. There are so many basic uses for ray casting, for instance, in line of sight detection in the field of AI, detecting the collision points when shooting bullets and importantly for my game, detecting where to attach grappling hooks. More advanced uses could be in inverse kinematic routines, procedural animation systems or even, as I eventually want to try, Wolfenstein style 2.5D ray-casting engines. With some extensions It should be possible to modify a ray-casting algorithm to perform somewhat naive Voxel rendering, which is something else I really want to try. Ever since I played Voxatron, I have since fallen in love with Voxels and its on my large project list to make something simmilar.
Over the last few years I have studied a large number of ray-casting algorithm available on the web and read many websites on the subject. I would be lying if I said I really understood all of the approaches I have seen to ray-casting, but it is clear that the efficiency of them varies quite substantially, and in this regard they are easily examined.
Perhaps the most understandable and readable tutorial for ray-casting I could find was here, in article by Lode Vandevenne. On the face of it, his algorithm seems very efficient and he talk in detail about the efficiency and speed concerns, and even in the main loop there is no multiply or divides used. However, in exploring the code we can see that to initialize a ray cast, two square route functions are called, which adds up when you consider that two called are made for each vertical column of the screen. Thus even at 320×240, 640 square route calls are made in rendering one frame. Square route calls can be optimized somewhat by approximation, or look-up tables, however this seems still like a weak point of the algorithm.
Another nice tutorial can be found here, written by F. Permadi. In his tutorial he advocates mostly the same strategy as the above, but the formers square route calls are now replaced with a call to the tangent function, shown here. This is more efficient, since the tangent function can easily be packed into a look-up table and accessed quite efficiently, just as I did for my sin wave approximation library. Still this look-up costs space and has accuracy tradeoffs thus reducing the overall efficiency in other ways.
For my algorithm, I managed to avoid square routes and look up tables of any kind, in fact there are just four divides and six multiplies called for each ray cast, the rest being just addition and subtraction. The inner loop is entirely performed using addition and subtraction also, so the overall efficiency seems bloody high. I do however test inside the inner loop to see if the ray has exited the map space, but if we can guarantee that a ray will hit a tile (such is the case, when the map is surrounded by tiles) then efficiency could again be dramatically increased by omitting these tests. In the code below that I provided, you will find there is one multiply in each inner look, to perform the wall hit test, but this can be optimized away to equivalent addition and subtraction, as I have done in my own personal version. In my scheme unlike that of Lode, I separate the ray cast into two stages, that of ray casting over the x-axis and next the y-axis. Each of these steps may produce a hit, and at the end of the algorithm we can compare the squared distance to efficiently select the closest of the two hits. I also break the ray cast through each axis into two parts, that for a positive increment, and that of a negative, since we can tailor the inner loop to be more efficient for each case.
I am sure this ray casting code will find loads of awesome use in Ninja Flare as well as my other projects. I also need to find a better name for the game, other than Ninja Flare, which was chosen quickly for the deadline of Ludum Dare.
My decision to follow a tile based map representation has led me to explore several things I like in games, and to see how they can be implemented in a tile based world, efficiently and robustly.
One of the things I have been experimenting with, and love seeing implemented, is a method for computing fast real time 2D shadows. Since I want to take Ninja Flare in a direction towards a ninja stealth game, shadows will be a very welcome addition to the game play. The image above shows the state of my progress in this area. The large outlined grey rectangle represents a conceptual viewing area and thus the limit of the distance of the shadows. In reality some shadow edges project beyond this area because I have not yet implemented any clipping. The yellow circle in the middle of this area represents the light source. The darkening of the shadowed regions was added afterwards in Photoshop only to help give me an idea of the accuracy of my results. In order to complete this method I will need to implement a fast triangle or quad filler to shade all the shadowed regions. This could be done in software if I stick with SDL, or a perhaps easier strategy is to use Direct3D to do the filling for me. One of the nice benefits of the method I came up with for computing these shadows is that it requires no pre-processing so it will react to any changes in the map structure.