Camera Movement

http://www.youtube.com/watch?v=2WwGt3QqENc

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

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_0SkinnedModelbinx86Debug 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.

screen090

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_0SkinnedModelPipelinebinx86DebugSkinnedModelPipeline.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.

screen091

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.

screen092

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

screen089

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.