Tuesday, August 9, 2011

Velocita Part II

I had made quite a mess of the project by now, I had all the buildings look 3D by keeping track of the angle of the car from the position of the building and projecting the image of the building in the opposite direction to fake the effect of height. I figured that the best way out would be to start all the way from the beginning. So I did just that, but this time in DirectX. I had myself convinced that it was Allegro that was slowing down the game, and not the multiple textures that were being loaded at runtime!

This version of the game was less complicated and stayed a 2D car physics simulator for the longest time. All you could do was drive around and pull a few powerslides. The sound and graphics were almost the same, except with light trails and motion blur! It took a while for me to get to know the DirectX 9 API especially because I was starting off with 2D. Soon after I was done setting it all up, I had a dear friend draw out some images of a few cars from the top down perspective. We used a camera on a tripod to take shots of the artwork and cropped it to put it into the game. Even though we had a more realistic car it was still just an image. I was bored of looking at a static image of a car which was not fitting in with the graphics of the rest of the world which were still being prepared in MS Paint. That was when I decided that it was time to step things up a bit.

Before starting off, I had myself convinced that it'll be really simple, just the same physics but models instead of images. All  I though I had to do was learn how to load in some models of a car, a set of wheels and a simple world and position them properly. Obviously, I had no idea what I was getting myself into and I ended up spending the next three years on a 3D car physics simulator before it was complete.

Everything went according to plan, importing models, positioning the camera and running the same simulation on it. I learned how to set up a DirectX window, load up models, store them as meshes, get textures, set rendering states, position lights(fixed function), set transforms and draw meshes using subsets. I ran into pitfalls on every step. The tutorials in the DirectX SDK weren't any help at all. All you could do is copy a whole sample project and tell yourself that you've successfully learned how to set up instanced skinned meshes which didn't sound very exciting to me. Books, forums and other helpful articles on the internet were of great help,  I had a car run on an open field with a camera looking down on it. I accomplished the task quite easily, but it was sad to see how my car reacted when it went over a bump - it didn't.

I applied a not-so-quick fix to the game where the car oriented itself according to the height of the wheels from the ground mesh. A few raycasts to the ground were easy to figure out but I got really stuck when I started working with matrices and quaternions, I hadn't braced myself for 3D math. Though the car was now reacting to the heightmap, it misbehaved when it went off a cliff so I knew that we needed a proper suspension set up to makes those reactions more physically acceptable.

That was when my hard drive gave up on me and I lost all my files.

Saturday, August 6, 2011

Velocita Part I

I meant to buy a book on DirectX but I'm glad I got Game Programming All in One (2nd Edition) instead which focused on Allegro. Slowly and patiently trying out all the functions written in the book and keeping note of the ones that I thought were important, it was the book that introduced me to game programming fundamentals. From simple 2D vector graphics to loading bitmaps and drawing animated sprites, I was armed with the tools to make my first top-down car physics simulator. I learned the API by working on Velocita side by side, everything that was usable would be copied from the book and pasted into the project. The end goal was a free roaming car physics simulator in an open world and I had myself convinced that I can't go wrong with it. It was my first and last lesson on Feature Creep, something I'll never forget. So I took a small simple bitmap of a car and a large 5000x5000 bitmap showing the top down view of a city baked in MSPaint. The map consisted of a few extra squares and rectangles for buildings, bitmaps that doubled up for the collision model as well.

I included a particle system for the dust, smoke and skidmarks, had Gourad shaded sprites for doing a day/night cycle and translucent images for car and street lights. Tinting all the sprites in real time in Allegro wasn't one of the brightest ideas I had. As the game slowed down to an unbearable framerate, I tried a few tricks to speed things up a bit, and soon I discovered scrolling. If only I had read a few more pages of the book, I would've saved a few days work, but figuring it out myself was a great experience. Cropping the visible part of the world map around the position of the camera and blitting(copy) to screen was an amazing find in my opinion.

The game entered it's longest stage of development when I started working on the physics of the car. I had no clue as to how car physics worked but I didn't let that stop me, soon I saw myself trying out random mathematical functions that related the throttle and steering to the speed and angle of the car respectively. Failing miserably, I searched on Google for a simple article that explains 2D car physics for games. After looking for days and rejecting a few dozen complicated articles, I found that one article that I still have saved on my computer to this day - "Car Physics for Games by Macro Monster" or the "Monstrous Car Physics Tutorial". Even though the article was simple, I had a hard time understanding it. My brother did most of the work actually and later helped me understand how it all really worked. This article is very dear to me, I spent days staring at it, brings back good old memories of being in class and having the printed pages hidden inside the history textbook!

The racing game came to life when the car started to react in a more realistic way, with the sound of a skid playing in the background with it's volume and pitch attached to the speed and angle of the car respectively. You could now go to specific buildings to select custom colors and spoilers for your flat 2D car and change the weight or cornering stiffness to adjust it's performance. Obviously influenced by Need for Speed, all these features slowly found their way into the game which came as no surprise. I soon found myself trying to incorporate a 2D side scrolling shooter in one of the buildings, with frame by frame animations exported from flash, and muzzle flashes from Max Payne 2, by taking snapshots from the game by pausing it in the middle of an action sequence, a not so minor distraction that I'm not particularly proud of.

I had no track of time whatsoever. There was nothing to worry about and no deadline to take care of, it was pure fun, creating whatever came to mind. I wouldn't call it all a waste of time, I spent days trying to have the cop car figure out the position of the racer by shooting rays into the world not realizing that the position of the racer is already sitting in a variable which is accessible to me. I did a lot of stupid things, like, planting a dummy pixel on the tip of the gun so that the program can iterate through the world map to find the colored pixel and spawn a bullet at the right location. The good part is that a lesson was learned even though countless days were spent doing foolish things, I ended up with lots of experience of doing all the stuff the wrong way.