Camera Movement

January 2, 2013

The camera in the 3D game world can now be moved and rotated.

Since I’ve had a few days off from work, I tried to make as much progress as I could with development of the Blasting Bits game.  I was able to get the new model processor working, and now the player model is animated in the game.  In Blender I can make multiple animations/clips, but unfortunately XNA will only allow one animation to be imported as noted in this article by Shawn Hargreaves.  So for now the player model continuously runs.  There doesn’t appear to be any elegant solutions for having multiple animations in an FBX file.

I added camera controls to the game, so when the player holds the right trigger the left thumbstick will zoom and rotate the game world.  Zoom is basically just the distance the camera is away from the game world.  Initially, I had the camera changes in set increments which appeared jarring when changed.  To fix this, I added three variables which are increment, speed, and target for both rotation and zoom.  When the player changes the camera, the increment value is added (or subtracted) from the target value.  Then for each update, the increment value is added to the current position until it reaches the target value.  This gives a much more smoother effect when rotating or zooming.


Custom Model Processor

January 2, 2013

After updating the Skinning Sample project to fix the rotation problem with Blender FBX files, I was able to compile this code into a library.  This library is found in the SkinningSample_4_0\SkinnedModel\bin\x86\Debug directory.  In my main BlastingBits game project, I added a reference to SkinnedModel.dll from that directory, which allows me to make references to the SkinningData, AnimationPlayer, and AnimationClip classes.


After running my game again, I got the ArgumentNullException error from the AnimationPlayer class from the code which checks to see if the skinningData is null.  This is most commonly due to the ContentProcessor of the model being imported not being set to the SkinnedModelProcessor.  Unfortunately, this was not an option for my fullbody5.fbx model which I had imported.  However, I figured out that this can be fixed by adding another reference under the BlastingBitsContent project, which points to the DLL for the SkinnedModelProcessor (SkinningSample_4_0\SkinnedModelPipeline\bin\x86\Debug\SkinnedModelPipeline.dll).  After setting that value, I was successfully able to change the Content Processor of my fullbody5 Model to SkinnedModelProcessor.  Compiling and running the game again resulted in no errors.


In my main game class, I added an instance variable for the AnimationPlayer, which will probably later be changed to a Dictionary to hold all of the animations for all of the models in the game.  In the model load method, I create the AnimationPlayer and AnimationClip for my model.  Then the StartClip method is called to start the animation.  Additionally, the Update method is called on the AnimationPlayer instance variable in the main game update method.


After adding the updated code to draw the player model in my GameScreen3D class, the animated model displayed correctly.  However, when I tried rotating the model 45 degrees, it still displayed but there still appeared to be polygon clipping problems.  After an hour or two of testing different camera positions and angles in both my game code and the Skinning Sample code, I was able to track down that this problem only occurs when a call to SpriteBatch Begin and End is made.  After I found the code that caused the problem, I was able to do a web search and found that this problem has been reported before, and the solution is to set two RenderState boolean values on the graphics device.  When I tried to run that code, it threw an error.  However, I was able to find that error reported and a more elegant solution, which is to set the GraphicsDevice.DepthStencilState to DepthStencilState.Default before rendering the model.  Adding that one line of code before calling the model draw method completely solved the problem with the overlapping polygons in my animated model.

Exporting FBX

January 1, 2013


Today, I worked some more on getting my player model to render correctly in XNA.  Previously, I got the animated model to display, however I noticed some clipping issues with polygons.  To tackle this problem, I decided to make an animated textured cube, since that would be easy to analyze.  I quickly realized that I was seeing the inside of the cube instead of the outside.

screen088This was happening because I was using a reflect modifier to display my model.  Originally, the player model was displaying on its side because Blender uses a Z-Up coordinate system, so in my code I would apply an X rotation of Pi (180 degrees) to fix that.  However, this resulted in the model displaying up-side-down.  To fix that, I used a reflection on the x-z plane where Y equals zero, Matrix.CreateReflection(new Plane(0f, 1f, 0f, 0f)), to display it correctly.  What I didn’t realize was that this apparently also flips all of the normals, so what I was actually seeing was the inside of the player model (with the outside transparent).  I believe there is a way to render both sides of the polygons, but that is typically wasteful since the player shouldn’t ever see the inside of a model.

After some searching, I found a good article by John C Brown at Diabolical the Shooter which explains how to make the necessary rotations in the content processor to get all models to display correctly.  Similar to his rotation example, I just added a method to the content processor which rotates the NodeContent by MathHelper.PiOver2 on the X axis and MathHelper.Pi on the Z axis.  Then that rotation matrix is passed to the MeshHelper.TransformScene method to rotate the model and animation.  It is important to note that this modification does not replace the “XNA Rotate Animation Hack” export option in Blender, which is still required to be checked when exporting a model.

After updating that code, I reran the Skinning Sample project with my player model and it now displays correctly.  The existing model (“Dude”) is now displaying on its side, since the rotation for Blender is applied to all objects.  It would have been nice if XNA provided a method natively to import models using Z Up coordinates.  Maybe they consider this a part of “paying your dues” to get a model to display.

Fun with XML

December 29, 2012


Tonight, I decided to go ahead and work on the Level Editor some more, so that I can start churning out some levels for a playable demo.  In Resistor, I used Mappy to generate arrays that I hardcoded as constants in my level class.  This probably wasn’t the best coding style, even though I’d bet it is faster than loading from disk.  In addition, I want players to be able to create their own levels in Blasting Bits, which means that having constant arrays is not an option.


To keep my data clean and organized, I decided to go with the XML format.  I was glad to find out that XNA has methods for handling reading XML files.  I found a good tutorial video by Richard Key of Paranoid Ferret Productions on how to do this.  I tried using this to load a String object from XML stored in my Content project area and it worked perfectly.  The “Boo” message that I entered into the XML file was loaded into the String variable I created using the Content.Load message, similar to a Sprite, Font, or Model.


However, I quickly realized that there is a problem when trying to create an XML data file for one of my own custom classes.  When trying to set the asset type to one of my classes, it is not able to find my class.  I created a simple Foo class with a Sting and int instance variables.  At first I thought the problem was due to my class not being public, but changing the scope of the class made no difference.  Doing a quick web search showed that this problem has been encountered before.  After digging deeper and consulting the video mentioned before, I believe that the reason why it can’t find my class is because the Content project doesn’t have a Reference defined to my main game project.  I tried adding a Reference, but it doesn’t allow this because it says that it would create a circular reference.  The only solution I was able to find is to create a third project just to hold the XML files.  This seemed really excessive, and this method probably won’t support loading XML files generated by the player so I found another approach.

screen083After finding a great example written by Microsoft, I was able to take  a new Foo object and serialize it.  In the example, it writes the XML to the Console.  Unfortunately, that isn’t very helpful aside from displaying the serialized object.  After some more trial and error, I found that instead of passing Console.Out to the Serialize method I could instead pass another Stream.  At first I tried using StreamReader and StreamWriter, but both of those require a filename, while I just want to store it as a String.  Finally, I found the StringWriter class, which takes no parameters and can hold the value of the serialized object.  The XML can be extracted using the ToString() method on the StringWriter object.  I was able to display the serialized data on the level editor screen.  From there, I just pass the String to my FileSaver class in ResistorKit, which handles opening and writing to the storage device.


That was enough to melt my brain for one night, so I will continue on later with reading the XML value back into my game object.  Last night I was able to get the remainder of my armor models (helmet, hands, boots, chest) to display in the game.  I also got the models for the basic collectible for money and the gun to display as well.  For now, I just used the second level helmet as the enemy model.  All of these models continually rotate during gameplay.

XNA Model Animation

December 26, 2012

Demonstration of switching between 2D and 3D rendering, along displaying an animated Blender model in XNA

Today, I worked on getting my simple character model animation to display in the game world.  Currently, the player model displays fine, but it does not display the arms and legs moving as the player moves.  I found quite a few references for animating a Model, but none were very simplistic.  One thing in common with all of the examples is that model animations are not handled “out of the box” by XNA.  The technique used requires that the animation information be extracted from the model using the “Tag” property, which is sent to a custom Pipeline animation processor.  Two good examples of how to do this are the Microsoft SkinnedSample project and Michael Neel’s XNA 3D Primer.  Using these references, I was able to get my player model moving in a XNA game window.


However, I found that the method I was using to generate the sprite animations was not compatible with importing for 3D animations.  The process for preparing a Blender model for export to FBX format for XNA is nicely explained in this article by StormCode.  Using that information, I was able to use the Action Editor in Blender to create a named animation.  In the 2.6 version of Blender, this is located under the DopeSheet screen.  The article also gives valuable tips on how to create a keyframe containing all bone information and how to duplicate keyframes.  Previously, I used the record button in Blender to create keyframes, but that only records the bones that are actually changed.

After exporting my model to FBX format, I was able to import it into the SkinnedSample project.  I created new Model and Animation objects for my model.  That project is covered by the Microsoft Permissive License (Ms-PL), so I think I can use that code in my own project.  However, before I do that I want to compile it into its own library, so I can just link it and pass my models to it without having to add Microsoft’s code to my own project.  The only problem I’ve seen so far is that some polygons will sometimes incorrectly overlap each other, so that is something I will need to resolve.


While I was working on the game screen, I was able to resolve the issue with the model appearing so dark.  This was because the diffuse property on the mesh effect needed to be set to White.

Scaling Models

December 25, 2012

The enemy HP values have been removed from above the enemies on the game screen, and a damage value that floats over the enemy when hit has been added.  I created a new class for overlay text, which is used by both the 2D and 3D game screen.  This prevents the code from being duplicated for things like the status text and enemy damage.  I added a new property to the enemy that is the dying state and counter.  This was needed because the damage value does not display if the enemy is not alive.  This kept the last damage number from displaying.  Now I’ve changed it so that the damage value displays if the enemy is alive or dying, so the final damage number now correctly displays.  The dying counter will also be needed to display a death/blowup animation.  On the 2D game display, I added logic so that the enemy just fades away using an alpha color value based on the percentage of the dying animation passed.


I created a simple graphic for the projectile.  I mapped this graphic to a simple 2D plane in Blender.  It’s important to remember that all points of the Blender model must have a Z coordinate equal or less than 0 (non-positive) to appear.  This may be backwards for my game, since I am rendering the 3D world with the Y axis facing downward, which makes translating from the 2D to 3D coordinates simpler.  Remember, the model coordinates are relative to the small orange dot, which is not necessarily the location where the axes cross in Blender.  It is a good idea to put the orange dot at coordinate 0,0,0 in Blender to keep from getting confused.  Therefore, all vertices must be moved in edit mode (not object mode) for the changes to appear in the game.  Similarly, scaling an object in Blender’s object mode will have not effect on the model in the game.  The model must be scaled in edit mode with all vertices selected for the size change to appear in the game.

Also, I converted the List of loaded Model objects to a Dictionary object.  I thought that a Dictionary was the same as a hash table, but Chris Gardner (@freestylecoder) explained to me that Dictionaries don’t allow multiple values to be assigned to a key like a hash table.  He said that Dictionaries should be used like symbolic links, which is what I am doing with my constants which represent each model in the game.  The previous method I was using was kludgy and bad style, which was adding the Model to the List and setting the constant ID of that object to the order number which it was added to the list.  That process was a hassle to maintain and a recipe for disaster later.

Project Cleanup

December 24, 2012


Not much coding was done over the past few days, since I have been working on rebuilding a PC.  I also upgraded my main PC to Windows 8, which required a complete wipe of my primary drive, which means I had to install Visual C Sharp 2010 Express, XNA Game Studio, Blender, Gimp, TortoiseHg/Mecurial, and other programs again.  One issue with installing GameStudio in Windows 8 is that the Games for Marketplace Client must be installed for the Game Studio installer to succeed.

Spent some time cleaning up the project directory.  I got rid of all references to the old game name, so now the name in the repository is now BlastingBits.  Just changing the name required all files in my project directory to be recommited and uploaded to the repository.  Unfortunately, due to the directory structure, my repository had four levels of BlastingBits folders (BlastingBits/BlastingBits/BlastingBits/BlastingBits).  Using the TortoiseHg rename option, I changed it to one level of BlastingBits and the rest in an “src” directory.  Again, the repository saw the moved files as files being deleted and added, so it required a full upload of my entire project directory again.  Not sure if there is any way around that, since I used the TortoiseHg rename option.

After the cleanup, I reimported all of the source files and assets.  The project was still trying to create an executable with the old name, so it was easier to just recreate the project and import the sources.


One issue that I was able to resolve was the warning about the textures associated with the models.  This is the “Asset was built two times with different settings” warning when running the game.  I figured out that this warning was due to the image being compiled for the model and then again as a regular texture.  I was able to get this warning to go away by setting the “Build Action” option to “None” for the textures used by the models.  This apparently prevents these images from being used as regular sprites in the game.


I’ve been working on updating the second armor set.  I added some additional changes to the helmet, and I started working on the body piece.  One lesson I learned the hard way is to model both the front and side at the same time.  I only modeled the front at first, so now it is really difficult to model the sides since there are so many vertices now.

Using some of the graphics that I have already created, I made a promotional graphic that I used on my main homepage.  This looks much better than the in-game screenshot that I had before.  I had to render the helmet again in Blender to add the circle plane below with the helmet’s shadow.  One important tip that I learned is that shadow in Blender are only displayed when using a “Spot” light source.  This same graphic and background were also used to update the theme for this development blog.

%d bloggers like this: