Pages

Wednesday, March 28, 2012

Character State Management

   When making a game how should the program decide when the player should be walking? Well, maybe if they press the arrow keys. But, then what if the character is crouching and you would like the arrow keys to make them crawl. Also, what if you want the character to jump when the up button is pressed but only if the character is standing and not walking. One way is to have a variable for jump and another for walk and one for crouch, and by using a large combinations of if-statements you can determine the animation that should be played.

   Anyone that has followed this way of thinking before will know that eventually this if-statement will get large and the number of state related variables grows quickly. Once the number of variables becomes too large, handling the various exceptional cases becomes nearly impossible.

   When I started I too faced this problem, however I have since found a much more elegant solution. I have found that all of these states can be described as a finite state machine. Game developers can benefit from this approach by clearly defining which states can transition to others and by abstracting the transition logic away from keyboard inputs.

   To start a programmer needs to describe a simple generic finite state machine. A finite state machine consists of a graph of nodes representing states and directed edges representing actions. The state machine keeps track of its current state and when it receives an action it move to a new state only if it has an out going edge with the same action. Then, Once we have a generic finite state machine we simply draw whatever animation we want to associate with a particular state.

   One thing to note in the design of my state machines is that I never make the action refer to user inputs. This is because not all of the characters in my game are controlled by the player, so for non-player characters it does not sense to think of actions as user inputs. This also gives me the flexibility to change the inputs later if I want to run it on another platform, like the Xbox360 which has buttons and joysticks rather than a mouse and keyboard.

A partial character state diagram from my RPG game engine
   I have used this technique in my RPG game engine and have found it extremely elegant. I no longer need massive if statements for determining what character animation should be drawn. I have also found it has enabled me to have more complicated character actions, like action combos, that I used to find very difficult to program.

Monday, March 26, 2012

RPG Elements: Console Improvements

     Back in February I wrote a blog entry about an RPG game engine that I was working on called RPG Elements. I have been busy with university, however I have found some time for implementing interesting new features. I am most excited about some of the new features I have added to the map editing tools recently. These tools allow me to create maps much more quickly and even generate hundreds of simple maps in mere seconds.

     I mentioned in my last blog entry that the main editing tool is an in-game console with commands for adding new objects and creating new maps. As my engine has evolved many features had become merged with the console that really should not have been, for example the actual logic of the console commands in many cases needed to be separate functions. With all of the unrelated logic in one place, adding new commands became increasingly difficult. To fix this problem, I refactored the console by moving these features into more appropriate modules, I also separated the console commands into there own classes. Now the console module is complete independent of the commands which it runs.

     When I refactored the console I realized that I needed to redesign the settile hotkey. In my RPG engine, the map is represented by an array of tiles, the user can set the type of the tile their player is standing on by typing "settile <type>" into the console. Because this is such a common operation, it is not practical to make the user type this command in every time, so I had the console remember the last arguments passed to this command and then all the player would need to do was press the "w" key to execute the same command again. However, now I made the commands independent of the console I no longer wanted the console to be storing the arguments of a specific command. Well I was thinking about how to fix this I suddenly thought that it might be nice to have this hotkey feature for other commands or have hotkeys for different arguments. So I came up with a new console command that can assign any console command to any of the number keys. For example, "hotkey 1 settile;sand" sets the tile under the player to sand when the 1 key is pressed. Another advantage of this feature is that I can assign "sand" to the 1 key and "grass" to the 2 key, where before I would have to type in a console command every time I wanted to switch between the two tile types.

     For my next feature I wanted to be able to write a list of console commands in an external file and then have them executed sequentially. When I refactored my console I realized that this would not be very difficult to add and would help easily accomplish what I had originally designed my engine to do, generate a large amount of content with minimal high level user input. One difficult I was confronted with was that loading maps took a couple milliseconds to load and scripts executed in a separate thread. I solved this by adding a command to tell them to wait a certain number of milliseconds to give the map a chance to load. Another problem was that because I am executing hundreds of commands at once I more frequently come across some of the race conditions that I had noticed occasionally in the past. While at the moment these race conditions seem an annoyance, I think of this as an opportunity to find and fix these serious bugs that would not have been possible with manual testing. I am excited about how quickly I was able to add console scripting and I believe it will be very useful going forward.

     With the addition of console scripting I can now generate hundreds of maps and add doorways between them. I thought it would be interesting to visualize all of these maps and their connections, so I wrote a console command that would query the list of doors to identify the directed edges between the maps. The program then outputs a file with a list of these directed edges in a format that I can input into a graph making program called Graphviz. Well this graph is interesting to see now, it will be very useful for some of my planned features. One feature that will require a graph of directed edges is non-player character (NPC) path finding, eventually I want to program NPCs to walk between maps, and by analyzing graphs like this they will be able to identify the best path.

An example map graph generated using test data based on a popular classic RPG game. At this point all of the maps are empty, aside from doors (represented in the graph by directed edges)
     By improving the tools in these ways, along with some other minor improvements, I have greatly reduced the amount of work I need to do to create a map and can now focus on higher level design decisions. Well these tool improvements are very interesting, they are not the only new feature that I have implemented since February. In one of my next blog entries I will talk about some of the other new features. I am also hoping I will have time to upload another video to demonstrate some of these new features. In the meantime I am adding new feature to this project and the next big area I need to improve is NPC behavior and interaction. So stay tuned!

Friday, March 23, 2012

New SimCity Engine

     A couple days ago I wrote a blog entry about a simulation game that I had written to learn SDL and improve my C++ programming skills. I showed this project off at the game developers club and it reminded them about the new SimCity game that was just announced. The game was announced at a recent game developers conference where they also explained a bit about their new simulation engine called GlassBox. The algorithm that the engine uses has a particularly elegant design which I find very interesting.

     The system defines five basic object types: resources, units, maps, globals and agents. Resources are variables such as people, money, electricity. They are organized into bins that can store a limited amount of a resource. Units are physical objects in the world like houses. They are defined as a collection of resource bins. Maps are used to represent the distribution of resources across the environment. They are essentially an array of resource bins with one associated to each map tile. Globals are like units, since they are a set of resource bins, but do not represent physical objects. Lastly, are these things called agents that move around the environment distributing resources between resource bins. Game developers represent these different types of objects using rules which they define in scripts. [1]

    Game developers are able to simulate various natural phenomenon using these simple constructs (as can be seen in this video). For example, a map can be used to represent resources like underground minerals or areas covered by forests. Agents are used to represent the distribution of electricity, pollution or the movement of cars and pedestrians. According to the article the company that makes SimCity believes this engine could be applied to their other simulation games like, one of my favorites, SimTower. [1]

     Although this design seems very elegant I feel that there many some potential problems which have been left out. One aspect that may pose a problem is that there may be tens of thousands of independent agents running in parallel at any given time. Well this seems beneficial in the era of multi-core computing, managing these agents and ensuring mutual exclusion is non-trivial. Another thing to consider is if so few constructs can accurately represent everything needed in a simulation game with over simplifying some aspects.

     I believe that this engine was very elegantly designed. This level of elegance is what I often strive for in my own programs which is why I appreciate how difficult it is to achieve. To create such an elegant engine, software developers often need to iterate and rewrite the program, each time removing unnecessary or redundant features.

     I would very much like to try and implement a similar engine one day and this engine it was interesting to read about. Game development companies are often very secretive so when they reveal this kind of information it is very valuable.

References:
[1] Frank Cifaldi. "GDC 2012: Breaking down SimCity's Glassbox engine" Internet: http://www.gamasutra.com/view/news/164870/gdc_2012_breaking_down_simcitys_.php, March 7, 2012 [March 21, 2012].

Wednesday, March 21, 2012

Game Developers Club

     Back in January I wrote a blog entry about how I volunteered at clubs week to promote the game developers club. Since then we have held several meetings and had some interesting discussions. Since I have been a member for a while I am also looking for ways to get more involved.

     One of the things that I really enjoy about the game developers club is learning about all kinds of different technology that I might not otherwise have heard about. Usually when I am working independently on my own game projects I am so focused on the way that I am currently doing things that I do not stop to consider new or alternative technologies. By discussing ideas with other people I feel that I can learn more than I can individually.

     For example last week at the game developers club we discussed game engine called Unity. Some of the other members have used Unity to create very interesting games. Unity developers also have the choice of embedding there game in a web browser or deploying it to a mobile device. At one of our upcoming meetings one of the members might give a brief tutorial on how to use Unity because many people seem interested.


     I have really enjoyed being a part of the game developers club and often try to find ways to get more involved. As I already mentioned I volunteered at clubs week but I have also been looking for ways to help organize the meetings and other club activities. Over the past couple semesters some of the prominent founding members of the club graduated and there has been a gap in leadership. Last week one of the former club presidents drop by during the Burnaby meeting and we talked about some of the things we could do to try to improve the club. One of the problems is a lack of focus that we have had at our meetings lately, to fix this I will try to spend some time during the week before the meetings coming up with some more topics for discussion.

     Another thing that needed fixing up was the club website. Many of the pages were out of date, especially the projects page. I started by talking to some of the executives and gathered a list of the projects that they had worked on. I will also need to find out this week if any of the other members have projects they want to post on the website. The project page on the club website is a valuable feature for promotion of the club and its members. The former club president I was talking to was saying game development companies often visit the website and one of the first pages that they want to look at is the projects page.

Monday, March 19, 2012

Book Review: The Animators Survival Kit

     Back when I was working on my animation project, one of my group members recommended a book to me. Unfortunately, he recommended it to me near the end of the project when the deadline was fast approaching and I did not have time to read it. I remembered that book the other day while I was at the university library researching for a history project. I searched on the library's website and found that they had a copy of the book so I decided to read some of it.

    The book is called The Animators Survival Kit and was written by Richard Williams, the animator behind the film "Who Framed Roger Rabit?"[1]

    I found this book interesting because it explained some of the techniques used by traditional animators. Many of these techniques, such as tweening, are now done automatically by computer animation software. Even though these processes are now automated, I feel that understanding the workflow and techniques of traditional animators can be useful to improve digital animations and make them more expressive.

    One process digital animators often take for granted is called tweening. In traditional animation, tweening was the process of creating the frames in between the major actions. In digital animation software the in-between frames are inferred using mathematical interpolation. Often beginner animators will rely too heavily on the animation software to generate the correct in-between frames and as a result there animations look artificial.

    One thing that I never realized about tweening is that traditional animators would draw the in-between frames halfway between two frames they had already drawn. This is because it is easier for animators to imagine what the frame should look like if it is exactly half way rather than a third of the way to the next frame. Then they would draw further in-between frames to make the animation slower or smoother.[1]

    The book also explains about how to make characters more expressive and have display their personality through there actions. The most fundamental action that many animators overlook is how the character walks. The book probably has around one hundred pages just dealing with developing the walk cycle.

    Another technique the book explains to make the characters more expressive is to design their actions to have three phases. In the first phase, called the anticipation, we want the character to make a motion that sets the scene for the major action. In the second phase the character preforms the major action. The final phase is the result where we see the outcome of the major action.[1] For example consider a cartoon character hitting another character with an over sized hammer. The anticipation would be the character lifting the hammer over their head, the action would be them quickly slamming it down and the result would be the character lifting the hammer to reveal the other character has been flattened like a pancake.

    I have enjoyed reading this book and hope to try some of these techniques when I get some free time. I definitely recommend it to anyone interested in learning more about animation and the workflow of traditional animators.

References
[1] Richard Williams, Animators Survival Kit, Expanded ed. London, Faber And Faber Ltd, 2009.

Saturday, March 17, 2012

Summer in Innovation

     I mentioned I have already mentioned that since the courses offered at my university are very sparse I have been looking for alternatives. Initially I thought I should try to find another co-op job, however I have already done 4 semester of co-op and would really like to concentrate on graduating so that I can get a full time position.

     Then I heard about a program that my university is offering called the Summer in Innovation. The program is targeted at upper division computer science, business and interactive arts and technology (IAT) students. Twenty-four students, eight students from each of those three disciplines, will be chosen and the program will have a focus toward working with and learning cross disciplinary concepts to develop software applications.

     The theme for the summer is all about designing mobile applications. From what I understand the course will involve several mobile applications developed as part of smaller groups and a couple developed as a large group. There will also be several industry experts that will come to discuss innovative ideas in mobile applications to share their thoughts and get feedback from the students.

     This program would count toward my graduation as a 15 credit course which would fulfill the 14 credits that I still need to graduate. I would also fulfill the honours research credit and if I decided to I could take 4 more courses in the fall to graduate with honours. So in terms of program requirements this seems to meet my need perfectly.

     Considering that this is a 15 credit course I assume that it will be very intensive. It will be the only course I will be taking and will probably take at least as much time as working on co-op. I do not believe this will be a problem, since I have done co-op and have worked on intensive group projects like my animation project.

     I am also very interested in working with mobile application and feel that this would be valuable experience to have given their growing popularity. It would also give me more opportunities for possible careers after graduation at are not directly related to games.

     Since I found this program interesting I decided to apply. They liked my application and I was accepted into the program. So this is what I will be doing this summer I am very excited.

Thursday, March 15, 2012

Creativity in Games

     I have already talked about one of the suggestions that I received from my previous job interview. Today I would like to talk about another thing they suggested.

     During the interview they asked me questions about the kinds of video games that I had played and what I liked about them. I had some trouble coming up with reasons because I had not given it much serious thought. I believe that understanding why one likes something is important to think about, not only with respect to games but also, even more generally, with any kind of software. To help me understand why I like the things that I like, I will try to explain some of the aspects of games I like using specific examples of games that demonstrate this aspect well.

     I have liked playing video games all my life and have many reasons I prefer some games over others. Its very difficult to identify a single common element that I like in games but some themes that I like to see in games are creativity, exploration, simulation, expansion and progression. I wont address all of these in this blog but instead will focus on the first and then continue with the others in a future post.

     Unfortunately, since I began university I have played fewer and fewer games. The biggest reason for this is that I have been spending much more of my time working on university projects. Even still I do occasionally play games but I feel that I do not enjoy them as much as I did when I was younger. Maybe I have different interests now then I used to? I think part of the reason I have not played many games lately is that I enjoy making my own games. Making games is a much more creative activity and it is hard to find games that allow one to be creative.

   A perfect exception to this is Minecraft which gives players complete freedom to be creative. Minecraft has no objective and simply encourages the player to gather resources and use those resources to build things. Other games that I have really enjoyed are simulation games like Rollercoaster Tycoon and The Sims. Both of these games require the player to design a virtual world.

    One aspect I like about of these games that promotes creativity is the lack of a well defined objective. All three of these games have no end condition, the player just keeps creating until they decide to stop. In games with a clearly defined objective, game developers would design the features of a game as tools that the player can use to get them to the goal. In games where there is no goal it is often unclear what kinds of features the player will require. Game developers often find designing these kinds of games particularly difficult because they must create features that have many possible uses.

    A good example of a feature with many uses is the piston blocks in Minecraft. The piston block is a block the player can activate remotely and when activated will extend causing any block in its path to be moved. The piston can also be modified so that when it is deactivated it retracts and pulls the block back to its original position. Using this people have created elevators, automatic doors and even giant moving faces in the sides of mountains.

    While a lack of objective has in someway helped these games I often feel like there needs to be some kind of goal in order to validate the quality of the players design. Business simulation games like Rollercoaster Tycoon allow the player to gauge their skill based on how much money they make and how happy the people are that ride their roller coasters. However, Minecraft and The Sims seem to be lacking a validating goal and occasionally I wish there was some kind of goal that I could optionally try to achieve. For example, in Minecraft I built a castle but then realized that there was nothing worth defending the castle against and no way to prove that my castle was designed well.

     Creativity is not only limited to simulation games. I believe that players want to be creative in whatever game they are playing. There are countless examples on the internet of players finding situations where they can be creative in almost any game. Therefore, my suggestion to game developers is to always keep players creativity in mind.

     Well those are my thoughts on creativity. I find this kind of subject very hard to write about because I have many ideas but it is very hard to boil them down into a single argument. I had to rewrite this post several times because I kept going off on a tangent. I plan to think about these ideas over the next couple weeks and hopefully by writing and thinking about them I will further understand and solidify them in my mind.

Tuesday, March 13, 2012

Factory Simulator

     Last time I mentioned that I was writing a game in C++ using the graphics library Simple DirectMedia Layer (SDL). The goal in writing this game is to practice C++ and explore some of the popular C++ libraries available.

     I discovered that SDL has many similarities to XNA and was not difficult to learn. I was able to start making my game within about an hour or two of learning SDL basics.

     I decided to make a simulation game, similar to games like SimCity, since this is a style of game that I enjoyed when I was younger but most of my favorite series have since become discontinued. I came up with an interesting idea for a factory simulator where the player owns a factory and must manage the happiness of the workers. Then I broke the problem down in to the simplest parts so that I could plan to implement it in roughly two days.

     In my game the player can build three types of buildings: houses, power plants and factories. The houses produce workers, the power plants produce power and the factories produce money. Also, The factories can only produce money if they have enough workers and power.

     So far I have implemented everything I mentioned but will refrain from showing any pictures just yet since it is still mostly text based with a few placeholder graphics.

     I plan to extend this by giving factories different kinds of input and output resources, for example a paper factory might require wood as an input and produce paper as an output. By doing this I can create chains of resources that the player needs to manage, for example a logging camp is required to produce wood for the paper factory. Also, since the game is about managing the workers happiness I need to keep track of the mood of each worker which will be affected by factors such as wages, safety, housing and environment.

     Overall I have enjoyed learning SDL and I think it is a useful graphics library. It handles most of the same aspects of drawing 2D sprites to the screen as XNA and has extension libraries for drawing text, handling input and playing music. To draw 3D graphics it is common to combine SDL with OpenGL which is something I may try in the future. In contrast to XNA, I have not yet figured out how to use pixel shaders but suspect that I may need to use OpenGL's GLSL shader language which I have heard is more complicated than XNA's HLSL.

     I plan to continue working on this game in, my free time, to practice my C++. If I get it to a more complete state I will post some pictures and videos in a future blog entry.

Sunday, March 11, 2012

Interview Feedback

     This semester I have been trying to figure out what I will do during the summer. Unfortunately the course offerings for the summer are very sparse and I have already taken most that are offered. Since I would rather wait till the fall to take more courses I have been trying to find alternatives such as another co-op job.

     Two weeks ago I had an interview downtown at a video company. I have not had an interview in a while and I ended up doing rather poorly. A couple days later I received an email from the co-op coordinator saying the company decided not to hire me. Along with the email they had sent some feedback about what I could do to improve. I spent some time reflecting on this feedback last week, since my midterms were finished and I had a bit of free time.

     The things they told me were not overly surprising since I had already identified them as things I had struggled with during the interview. The first was that I needed to improve my C++ programming knowledge. During the interview they asked me to write a simple C++ program on a white board but I had trouble coming up with an answer.

     I have become very proficient in languages like Java and C# by using them for my hobby programming projects. I have only used C++ for smaller school projects and a couple times during co-op. Since I do not practice C++ very often I often struggle with it.

     For practice I decided to try writing a game in C++. I have never written a game in C++ before and wanted to find out what libraries are available for making games. I though there must be some popular libraries since game development companies use C++ almost exclusively. C++ is the language of choice since modern games are always trying to push the limits of the current hardware's performance.

     One popular graphics library I have heard about is called Simple DirectMedia Layer (SDL). SDL is one of the most fundamental graphics libraries and many higher level libraries, for example a python library called pygame, are written as extensions or wrappers of SDL.

     Another library that I have been interested in trying, not related to games, is QT. It is a user interface library that people have been talking about recently and which is used by applications like Google Earth and Autodesk Maya.

     In my next blog entries I will talk about the game that I made in SDL and a bit more about the interview feedback.

Monday, March 5, 2012

Graphics Software: Inkscape

     Over the next couple weeks I am going to write a couple blog entries about the computer graphics applications that I have used. I have been using these applications for a number of years and have become quite skilled with them. I will explain what they do, why I like them so much and how I have used them. All of these programs are open source and free to download.

     The first program I am going to talk about is Inkscape, an open source vector graphics application. It is used to produce a type of art known as Scaleable Vector Graphics (SVG). SVG differs from pixel-based graphics because it represents objects using shapes. Well pixel-based graphics usually store the value of each pixel in a large array, SVG stores primitive shapes in an XML format. SVG's biggest advantage is that there is no loss of quality when scaling an image. SVG has grown in popularity over the past few years because it has been supported by modern internet browsers and improves consistency across devices with different display resolutions such as mobile phones.

     I have tried many vector graphics applications but I have found Inkscape to be the best. These days it is my first choice for creating quick graphics. I use it to create most of the graphics for my games and I even like to use it to create diagrams for reports. I have also found it to be useful for planning website layouts and picking color schemes.

Notice the repeated elements and primitive shapes I used to create this complex scene

      What makes it so useful is that creating an object out of several primitive shapes is very intuitive. Inkscape provides all of the standard Constructive Solid Geometry (CSG) functions that allow you do such operations as taking the union or intersection of shapes. It is also easy to duplicate and reuse pieces of objects to help create more complex objects. For example, when creating a house you might start by designing one of the windows using primitive shapes, once you have a single window you can group the primitive shapes and then copy and paste the window as many times as you need. I often save objects that I have created so that I can reuse them in future projects.
Three diagrams using difference shading techniques to add depth.
Plain (Left), shadow (Center), Gradient and Shine (Right)
     I have also found that the gradient and blur tools are very useful for adding depth to any image and make it eye-catching. I have created a quick example diagram in the image above. I have added a shadow, gradient and a bit of a shine to give the graph some depth. These effects are so easy to do in Inkscape and make the image much more eye-catching.

     In many ways Inkscape reminds me of when I used to use Flash to create animations. Although I find Inkscape has more features and is more intuitive than flash, it does not have the animation tools that Flash had. Unfortunately, I have not been able to find another vector animation software that even comes close to Flash. Maybe one day I have will make one myself.

     Inkscape is definitely one of my favourite open source software applications and I am constantly recommending it. For me it is probably tied with blender in my list of favourite open source graphics applications, since I find Inkscape more useful on a daily basis, whereas blender is amazing because it is so powerful. Later this week I will probably talk more about why I like Blender. In the meantime, if you are interested you can checkout a tutorial video I created explaining my process for creating graphics using Inkscape.