XNA Essentials

Game Programming for Xbox 360, PC + Windows Phone


Windows Phone 7 Developer Sweepstakes end June 30th, 2011

If you are almost done with your Windows Phone 7 App or Game, you may want to put a little extra effort into finishing it up and get it certified so you can beat the deadline.

Microsoft’s fiscal year ends June 30th and so do this promotion:

Use Code: 5DXDN

URL: http://bit.ly/WP7DevOffer

If you have 1 app in the marketplace you can win a Samsung Focus. (Details & Rules on the site)

If you have 2 apps in the marketplace you get a refund on your Marketplace account fee.

Register on this site for that: http://phone.microsoftplatformready.com/Offers.aspx

If you have 5 or more apps you can get Free Advertising for one of your apps!

Enter in the WP7 Sweepstakes (Code: 5DXDN; URL: http://bit.ly/WP7DevOffer

If you publish(ed) 5 new apps between April 1 and June 30 you get FREE advertising for one of those apps or games.

So if you are almost done – push through and get it done!

Happy Coding!

Shake that Camera

I was browsing the educational catalog on the App Hub and I saw something that I had missed previously.  I wanted to point it out and also point out that if you haven’t been to the educational catalog section in a while, you should go browse it for some nice examples.

The little demo I saw was one that shakes the camera.  It also vibrates the controller (or the Phone).  It was pretty easy to take the important pieces of code and modify the Camera object I used in the XELibrary (the library the book uses).


Inside the Update method of the game class (i.e. Game1 inside of Game1.cs) you can add the following code:

if ((input.ButtonHandler.WasButtonPressed((int)PlayerIndex.One, Buttons.A) || (input.KeyboardState.WasKeyPressed(Keys.Space))))
    camera.Shake(25f, 2f);


Next, we need to actually create that method (Shake) on the camera.  Since we would want to use this on the FirstPersonCamera and the static Camera we will apply it to the base class Camera.  Add the following variables to Camera.cs:

// We only need one Random object no matter how many Cameras we have
private static readonly Random random = new Random();
// Are we shaking?
private bool shaking;
// The maximum magnitude of our shake offset
private float shakeMagnitude;
// The total duration of the current shake
private float shakeDuration;
// A timer that determines how far into our shake we are
private float shakeTimer;
// The shake offset vector
private Vector3 shakeOffset;


Next, add the following two methods to the Camera.cs file:

/// <summary>
/// Helper to generate a random float in the range of [-1, 1].
/// </summary>
private float NextFloat()
    return (float)random.NextDouble() * 2f - 1f;
/// <summary>
/// Shakes the camera with a specific magnitude and duration.
/// </summary>
/// <param name="magnitude">The largest magnitude to apply to the shake.</param>
/// <param name="duration">The length of time (in seconds) for which the shake should occur.</param>
public void Shake(float magnitude, float duration)
    // We're now shaking
    shaking = true;
    // Store our magnitude and duration
    shakeMagnitude = magnitude;
    shakeDuration = duration;
    // Reset our timer
    shakeTimer = 0f;

The first method is just a helper method we need soon.  The second method is the public Shake method on our camera that initializes the values so we can begin shaking the camera’s view.

Finally, in the Update method of Camera.cs add replace the following code at the bottom of the method:

Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector,
   out view);

With the following code:

// If we're shaking...
if (shaking)
   // Move our timer ahead based on the elapsed time
   shakeTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
   // If we're at the max duration, we're not going to be shaking anymore
   if (shakeTimer >= shakeDuration)
       shaking = false;
       shakeTimer = shakeDuration;
   // Compute our progress in a [0, 1] range
   float progress = shakeTimer / shakeDuration;
   // Compute our magnitude based on our maximum value and our progress. This causes
   // the shake to reduce in magnitude as time moves on, giving us a smooth transition
   // back to being stationary. We use progress * progress to have a non-linear fall 
   // off of our magnitude. We could switch that with just progress if we want a linear 
   // fall off.
   float magnitude = shakeMagnitude * (1f - (progress * progress));
   // Generate a new offset vector with three random values and our magnitude
   shakeOffset = new Vector3(NextFloat(), NextFloat(), NextFloat()) * magnitude;
   // If we're shaking, add our offset to our position and target
   cameraPosition += shakeOffset;
   cameraTarget += shakeOffset;
Matrix.CreateLookAt(ref cameraPosition, ref cameraTarget, ref cameraUpVector,
   out view);


So whe the main game class calls camera.Shake, the flag shaking becomes true and the shaeMagnitude, shakeDuration and shakeTimer are set.  This way when the camera game component has its Update method called, it sees that it needs to shake the camera and adds the game’s total seconds to the shakeTimer and if it has already been shaking too long it sets it to false and resets the shakeTimer.  Assuming it hasn’t been shaking long enough (or even started)  it computes the progress and determines the magnitude of the shaking from that value.  It then uses that magnitude to create an offset with some randomness.  The cameraPosition and cameraTarget then has the offset added to it.  This is what makes the camera shake.  When the view is calculated again it has new values for the camera position and target.

So just a little bit of code allows us to create a cool shaking effect in our games.  Nice.

Happy Coding!


Windows Phone 7 Hands On Lab–Tombstoning

At my local .NET User Group, Triad Developers Guild, I was able to give a talk on WP7.  Usually the sessions are lecture style, but I tried to do a hands on lab.  Unfortunately, not many folks brought their laptops so for some it was still basically a lecture style session with me doing the lab in front of everyone.  The labs are very good, I’d encourage you to download them and go through them.  The lab we did on Tuesday, February 15th was the Tombstoning, Launcher and Chooser, and then some with XNA Framework.

You can download lab here:


The files are as follows:

WP7TrainingKitOffline_RTM1.Setup.exe is the full download (both Silverlight and XNA)

WP7SLTrainingKitOffline_RTM.Setup.exe is only the Silverlight labs

WP7XNATrainingKitOffline_RTM.Setup.exe is only the XNA labs


Also, Microsoft is running a contest and if you use the following code when you submit your app or game to the Marketplace you could win a Windows Phone 7!

Use code: 5DXDN

Check out http://www.WindowsPhone7Event.com/contest/ for more information!

Happy Programming!


Running a Game in Full Screen in Windows Phone 7

If you create a game in Windows Phone 7 using XNA you may not want the Application Bar to be displayed.  You can hide that status bar in Windows Phone 7 by setting the GraphicsDeviceManager.IsFullScreen property to true.  The default is false.

public Game1() 
   graphics = new GraphicsDeviceManager(this); 
   graphics.IsFullScreen = true; 

XNA Game Studio 4.0

On September 16th, the XNA Framework team will be releasing XNA Game Studio 4.0.  While games created for the Xbox 360 will still be based on XNA Game Studio 3.1 for the near future, the team is also releasing a beta version of the new Connect tool (that allows you to connect your development machine to your Xbox 360 to transfer your XNA games) on the same day.

September 16th is when the Windows Phone 7 tools will be launched as well.  The timing isn’t coincidental since the Windows Phone 7 will allow developers to create games and apps using either Silverlight or XNA Game Studio.  The tools have been in beta for a while now.  The actual Windows Phone 7 Series devices will be released sometime this holiday season.

XNA Game Studio 4.0 has breaking code changes. The XNA Framework team went with the mantra of “If you break it, break it good.” They went with a  new approach of Reach vs. HiDef.  Reach is being able to write the same code for all supported platforms – Windows, Xbox 360, Windows Phone 7. HiDef is targeting a particular platform and the specific features it has available.  This was a great move and while it causes discomfort in updating any existing code it makes a stronger base for bringing in even more platforms in the future.

Make sure you grab the tools as soon as they are available in a couple of days!

I certainly hope that all of my readers read Shawn Hargreaves blog regularly. I’ve only linked to a couple of his posts here about XNA Game Studio 4.0, but he has a wealth of information that you will find beneficial. So make sure to catch up on his blog posts if you haven’t already.

Happy Programming!