Get into Physics with your XNA games

Leaves

If I have one piece of advice for budding game developers who are into XNA and looking for somewhere to go, it would be “Get Into a Physics Engine”. We’ve been playing with various versions of Farseer in XNA and they are well worth the effort. Lots of problems concerned with making things move in a realistic way just vanish when you plug in one of these magical libraries. I’ll be doing  a very quick overview of how to make simple things happen in XNA with Farseer Version 2 in the talk tomorrow.

Note to Mad Game Developers: I’ve moved the talk to LTA because of a clash with a First Year tutorial at 1:15 tomorrow.

Destruction Golf in XNA

image

Did my XNA session today for the Mad Game Developers. Good turn out, and some interesting games out there. Good to see Physics engines being used, they make life much easier. Perhaps the next version of XNA could have one built in…

Anyhoo, I recorded the session as a screencast, just to see if it would work. The level was a bit high, so the sound is a bit clipped, but it is OK, apart from the bits where I’m pointing at the screen and talking about things…..

If you want to see how to create very simple projectiles in XNA, and then pan the screen to follow them around, then it might be worth a watch. Click through the image above to take a look. You can get the sample app from here.

Sample XNA Destruction Golf Application

ScreenShot

I think I might need to get some help with the artwork though….

As part of the Mad Game Development this week I’ve been putting together a demonstration XNA game that shows how to do sideways scrolling, firing a projectile and causing damage. It is coming along nicely. If you want to find out more, come along to Room 312 in the Robert Blackburn Building tomorrow and I’ll go through how it all works.

I’ve had loads of teams signing up during the day, this is going to be great fun.

Preparing for Mad Game Development

HookADuck

Getting plenty of interest in the Mad Game Development next week, which is great. If you are thinking of taking part, but worried about your programming background I’ll be doing some sessions next week which should help a bit. If you want something to do to prepare, I’d suggest getting in some practice with XNA and maybe a Physics engine. Easier than you think, and very, very useful.

Oh, and downloading the latest version of Visual Studio Express for Windows Phone might be a plan.

Mad Game Development Week at Hull

Sweeties

We are having a "Mad Game Development" in the department next week. Any students who are interested in taking part can sign up and get cracking on our game idea.

The game will be developed in a week, starting on Monday 18th of October and running up to the weekend of the 23rd and 24th of October when we are going have a "pressure session" where we can work overnight to get the game ready for submission to Xbox Live.

The scenario for the game will be released on Monday 18th at 9:00am on www.robmiles.com (i.e. here). Students from Hull are welcome to form teams of up to 4 to get together and produce a game. All the games must be written using XNA and target the Windows PC and Xbox 360. We will have some Xbox 360 machines on site for testing over the weekend, and some Windows Phones. If the game passes our stringent quality control we will submit to Xbox Indie games and maybe even Windows Phone Marketplace on Sunday.

If you want to create a Windows Phone version of the game then we will have a special category for Windows Phone submissions. A team can create one version that works on one platform or different versions. Your call.

If you sign up we would expect you to come along on Saturday morning to the department to take part in the "all nighter" part of the development, finishing at lunchtime on Sunday . We will be on hand to give help (we might even be working on our own version of the game).

Pizza and drinks will be provided during the night. We will also have a few games set up for rest and recreation. If you think you will have any time to spare.

We will be producing some artwork and other assets for the game. You can use ours, or make your own. We will also have a writer from 360 magazine along over the weekend to cover the game development and see what you are doing. So you might even end up in the magazine. And we have some Microsoft goodies to give away during the night.

We will be holding briefing meetings during next week when you can come and discuss how you are getting on. These will be at 1:15pm on Monday 18th, Wed 20th and Friday 22nd of October in the Design Lab (room 312).

If you want to take part, form a team and send me an email with the name of your team and the names of the members. I will register you and give you access to the resource pages on Skydrive, which will go live on Monday next week.

The development is open to students in the University of Hull from any Department and any year.

Staff can enter too, but they aren't allowed to have any of the goodies.

Xbox Game: Angry Robot Rampage

Tom has been busy writing a game over summer. Angry Robot Rampage is the result. He has a blog post all about it here and you can find it on Xbox indie games here.

Great stuff. Now Tom, I want a Windows Phone version…..

Using RenderTarget2D in XNA on the Zune HD

Have you been lying awake at night wondering why your texture rendering doesn’t work on the Zune HD? No? Oh, well it must be just me then.

Anyhoo, I’ve found the problem, and the story goes like this.

RenderTarget2D is a way that you can draw things on a texture rather than on the game display. Normally I’d write things like:

spriteBatch.Begin();
spriteBatch.Draw (lots of pretty stuff….)
spriteBatch.End();

When the End method runs it takes all my drawing operations, batches them up and pushes them onto the graphics hardware, so the screen now contains the results of all my drawing operations.

However, sometimes you want to do things like rear view mirrors in driving games. You want to draw the view you would see behind the game onto a texture and then draw the texture on top of the rear view mirror.  In this case you need to create a thing called a RenderTarget2D which can accept the draw operations and put them onto a texture rather than the screen. Render targets are not that hard to use:

RenderTarget r = new RenderTarget2D(
   GraphicsDevice,  // my graphics device
   800,              // width of texture   
   600,              // height of texture
   1,               // no. of levels (1 works)
   SurfaceFormat.Color);  // format

GraphicsDevice.SetRenderTarget(0,r);

spriteBatch.Begin();
spriteBatch.Draw (lots of pretty stuff….)
spriteBatch.End();

GraphicsDevices.SetRenderTarget(0,null);

Texture2D result = r.GetTexture();

In the example above I send a bunch of drawing operations to a render target which is 800 pixels wide and 600 pixels high. I set the GraphicsDevice to render to this target, and then at the end I point it at null, which means go back to drawing on the screen again. The render target that I create is called r, and it ends up providing me with a texture which holds the results of all the drawing operations. You can use this to make your whole game display slide about, or bounce around the screen and it is very effective.

I’m using it so that my game can create some textures when it starts, rather than having to load them. I got the rendering working just right on the Windows PC and then transferred my code over the the Zune HD and it broke. I could clear the texture with different colours but I couldn’t draw anything on them. Wah.

After some twiddling I’ve found the problem. When you get a graphics device to render to a texture it is doing something it isn’t particularly used to. Graphics devices are designed to put stuff on the screen, not into memory.  This means that you have to be careful when you set up the target for the rendering, so that the graphics device is happy to write to it. You can find our more about this kind of thing here.

From the point of view of the Zune HD hardware, it is only happy to draw things if the target texture is either the size of the display or half the size of the display. Don’t ask me why, I don’t make the rules.  Any other sizes don’t produce an error, but they don’t work either, which is no fun.

This is not actually a huge problem (except perhaps for memory usage I guess) in that although you have to create textures that are larger than you really want you can use a version of Draw to only pull out the actual part that you want when you finally render them onto the screen. So, if you want to render to textures on a Zune you need to create your render target thus:

drawBuffer =
    new RenderTarget2D(
        GraphicsDevice, 
        GraphicsDevice.Viewport.Width / 2, 
        GraphicsDevice.Viewport.Height / 2,
        1,
        SurfaceFormat.Color);

This is the smallest texture you can use, if you want to draw on the full screen just don’t divide by 2.

XNA Cross Platform Compilation Symbols

Garden Flowers 02

I’m writing a single XNA game that should work on a whole bunch of platforms. Actually I’m going for Windows PC, Windows Phone, Xbox 360 and Zune. Which is a full house I reckon.

Anyhoo, this is a bit of a pain, as I have to make the code do different things for the different target devices. For example the Xbox and the Windows PC don’t have a touch screen so I have to simulate this in some way, or provide a different input mechanism for those platforms.

I can do this by using the symbols provided by XNA to let me select code which is passed to the compiler when the program is built. There are a bunch of them.

#if WINDOWS
// only compile this bit if we are targeting Windows PC
#endif

#if XBOX
// only compile this bit if we are targeting XBOX
#endif

#if ZUNE
// only compile this bit if we are targeting ZUNE
#endif

#if WINDOWS_PHONE
// only compile this bit if we are targeting Windows
//  Phone
#endif

It is important that you understand what is happening here. We are not selecting which code to run when the program is active, we are actually selecting which code gets compiled when the program is built.  I could do this to really confuse a programmer:

#if ZUNE
     Har Har
#endif

This is a very nasty thing to do. It means that the program will not compile if the programmer tries to build version for the Zune, it will produce an error when the compiler sees “Har Har” and ties to compile it. It will however work for every other XNA platform.

One other thing worth knowing is that the symbols that you see when editing with Visual Studio depend on the context in which the file was opened. If you have a multi-project solution (one project for Zune, another for Windows and so on) then the project that you open the file from determines which symbols are active when the file is edited and compiled. This is true even if all the entries in the project source are links to the same actual source file. In other words, the view you have of the code depends on where you opened the file from.

XNA Materials

Before I went to give my talk yesterday we dropped in at the Microsoft offices in Schipol. Very plush.

Microsoft Schipol Lampshades

They have these amazing lamp shades in the restaurant.

Microsoft Schipol Clocks

Time around the world, Microsoft style.

The presentation was great fun and the audience was lovely. I really like these trips out. Thanks to all the folks at Microsoft Netherlands for looking after me

You can find the presentation and all the XNA content that I used during the talk here.

A Hot Day for writing Custom XNA Content Importers

Humber View Wide

In between gardening and feeling very warm I wrote a custom content importer for XNA 4.0 today. This is the way that you can bring in your own content into an XNA game. I’m playing around with some game ideas and I needed to get a bunch of data into my game engine so I can twiddle with it.

Turned out to be a lot easier to do than I thought it would be. Actually, the whole content setup is lovely to use in XNA. If you ever want to do this, you can find a really great start here:

http://msdn.microsoft.com/en-us/library/bb447754.aspx

The stuff seems to work fine in Windows Phone too. I really know how to enjoy myself in summer…..

Windows Phone FAQ

Novotel

I’ve decided to start building up my own little set of Windows Phone resources. I’m going to put down answers that I managed to figure out, so that I can look them up again if I ever need to. You can find my FAQ (it is a bit quiet at the moment, but I’ll add more as I find out more) here:

/windowsphonefaq/

This is all based my trial and plenty of error, I’ve left comments open on all the posts, so if you know different for any of them, feel free to put me right.

Hull Platform 2010 Fun and Games

Did a talk today at Hull Platform 2010. This was the first of what will be an annual event bringing together game developers and expertise.

4462282531
This picture was taken during my presentation. I hope I wasn’t going too fast for everyone…

4462268483
Jon Purdy hits the stage

4462266513
Two minutes into Darren’s presentation….

I had great fun and you were all a wonderful audience. I also got to judge the two competitions that they ran, which was also very interesting, with a high standard of entries. During my talk I said I’d put some resources up on the network for you, as it takes a long time to write these down. So, here they all are.

You can get my presentation, along with the XNA games that I showed during the talk, here:

https://static.squarespace.com/static/5019271be4b0807297e8f404/52c5bcfce4b0c4bcc9121347/52c5bd02e4b0c4bcc9123749/1269541761004/Rob%20Miles%20at%20Platform%202010.zip

To compile and run your C# game programs you will need a development environment. This is where you write the code and run it. You can get free versions of the Microsoft Visual Studio tool for the Windows PC  from:

http://www.microsoft.com/express/

If you want to write C# programs you just go for the C# version of Visual Studio. It takes a while to download, but it is worth waiting for…

If you want to write games you can then add the XNA framework which provides a set of resources for both 2D and 3D games. The framework is currently at version 3.1. You add this to your installation of Visual Studio and you can get the framework from:

http://creators.xna.com/en-US/downloads

If you are a student (and have an Athens account to prove it) you can get free copies of the professional versions of Visual Studio (and lots of other free stuff too) from Microsoft Dreamspark:

http://www.dreamspark.com/

I’ve written a Microsoft Press book which teaches programming using XNA games. You can buy it from Amazon here. You can also obtain a free PDF download of this from the Microsoft Faculty Site here:

http://www.facultyresourcecenter.com/curriculum/pfv.aspx?ID=8119

There is no need to actually sign up for faculty resource membership, you can skip that step. However, if you are a teacher or lecturer you can sign up for membership and get all kinds of good stuff, including an entire programming course based on the book content. The process is a bit of a faff (you have to send them something to prove you are a proper teacher) but it is well worth the effort as you get some valuable resources including free XNA Creators Club membership. This makes it possible for your students to put their games up on the Xbox Indie games site for anyone to download and play via Xbox Live.

If you want a free copy of our C# text book (the same one we teach our students from) you can download it in PDF form from:

http://www.csharpcourse.com

I’ve recorded a bunch of screencasts that cover how to get started writing XNA games. The episodes are available from Thirteen One, which is a Hull based gaming magazine. You can find all the screencasts up to now in my index at VerySillyGames:

http://verysillygames.com/Screencasts

If you want advice on getting started in the games (or any other) business you might find it useful to talk to the Enterprise Centre at the Univesity:

http://www.hull.ac.uk/enterprise

 

I’ve got a new Windows Phone

4435712539

Unfortunately it’s not real. Like many other facets of my life, it exists only within the mind of my computer. However, it does work, and I can write programs for it using the latest version of Visual Studio.

If you want your own Windows Phone 7 Series device you can head over to here to perform a one stop download that gives you all the tools you need, including Silverlight, XNA and the emulator.

If you are feeling rich (or are a student on Dreamspark) you can even signed up for the Developer experience here.

Free XNA Screencasts

pebbles

I’ve been recording XNA screencasts for the last six months or so for Thirteen1 magazine. The aim is to give you a gentle introduction to programming, using XNA games as the basis of the teaching.

I’ve just reached the end of the first section. If you want to view the screencasts, and download the practical sessions that go with them, you can find them on VerySillyGames:

http://verysillygames.com/Screencasts

XNA on Zune Session Fun

I did my final session of this TechEd today. I was on at 5:30, in session slot 13, but in the end pretty much everything worked. Thanks to everyone who came along, I hope you all had a good time and learned something useful. I’ve put the slides and all the demo programs up on Skydrive so feel free to download them and have fun. For some reason I forgot to take a picture of the audience, but then since you all know what you look like anyway I suppose this is not much of a loss…..

You might want to take a look at the other sites which are linked from my post from yesterday.

Zune HD Ins and Outs

I’ve been preparing my TechEd sessions today. I’m doing two, one to Microsoft Student Partners (in a couple of hour or so) and one Interactive Session to the general conference. If you are here in Berlin please come along, I’d love to see you.

Since the session is about creating XNA games for the Zune HD not surprisingly I’ve been doing a bit of creating games for the Zune HD. I got my AlbumShaker running really nicely, and I thought I’d try running it on the original Zune players. I wasn’t expecting much, but I was really surprised to find that performance was quite acceptable, with the albums bouncing around the screen most satisfactorily. The program doesn’t seem to mind that it is running on one of the older devices, the game just gets “central” settings from the accelerometer.  I even added some code to read the Zune pad, which emulates a gamepad  thumbstick. This worked fine, except for a couple of issues.

Using the XNA Gamepad to Slow your Zune Game Down

The code for the album shaker makes a bunch of album sprite instances and then calls Draw and Update behaviours on every instance. The first version of my game called the GamePad.GetState method for each of the fifty or so albums on the screen. This did horrible things to performance. For some reason reading the Zunepad is very slow. I assumed that the pad would be read for a a given clock tick, and then that value cached for use in the game. This is not so. I got a huge performance improvement by reading the state once for all the albums and then picking up this value in each album.

Using the XNA Accelerometer to Make your Zune Game wobble

I was so pleased with my speedup on the original Zune I thought I’d do something similar with the accelerometer input, so I changed the game so that it read the accelerometer once and then cached that for use by all the items in that update. This had a really strange effect, where sometimes the items flickered madly about. I’m not sure why this happens, it is as if the GetRotation method on the AccelerometerState object actually corrupts the state in some way.

I only know that if you get a “fresh” reading for each item you want to input the game works fine.  And this doesn’t seem to  slow the game down either, which is nice.

So the moral of this is that it is fine to cache the gamepad settings (and in fact on the Zune this might make your game go faster) but the accelerometer values should not be cached..