Use Micro Fibre for Shiny Gadgets

image

I reckon the Scotch ones are the best.

I was lucky enough to be able to spend some time with Scott Hanselman over the DevDays conferences. Scott is one of these folks who have technology running through their veins. It was great to see his eyes light up whenever he was showing off a cool new program or discussing the future of computing. However, I did actually have a gadget that managed to impress him, which was nice.

At one point during our chat I produced the micro-fibre cloth that I carry round to make my Windows Phone shiny bright before each demo. This went down very well, and was promptly put to work shining up all the devices we used for the keynote.

If you want to get rid of all the pesky fingerprints that these devices seem to accumulate then I recommend that you get one or two. You can get really good ones from the Pound Shops in the UK and places like Tesco also do multi-packs at a good price. But if you are properly rich you can get the original Scotch versions above.

XNA Windows Phone Session at DevDays 11

Windows Phone Audience Left

Windows Phone Audience Right

Two more halves of a room and one last Windows Phone XNA session. I seem to have taken a lot of audience pictures over the last few weeks. Thanks to you all for making the final session such a great one.  Remember folks that you can get all the code that you saw  from my demo pages:

/demos

The marks the end of my World Tour for a little while. Thanks to Helen for organising the European leg so well, and to Microsoft for inviting me along.

Windows Phone in Rhyme at Geek Night

Right Hand Poetry Fans
Some of the audience arriving. Left Hand Poetry Fans
The rest of the audience arriving

Well, that was fun. Once I found my notes. Thanks to the audience for really getting into the spirit of the event. The roar every time we had a “Silverlight Rhyme Alert” was most impressive. Hope you all had a good time. You can find the notes from the original Red Nose Day lecture here.

If you would like to donate to the Red Nose Day cause (which is very, very worthy and does lots of good work around the world) then you can find my donation page here.

And now I’m going for a lie down…

Recalcitrant Robots

left audienceRight Audience

Both sides of the room, click through to larger versions on Flickr.

I did my most “challenging” presentation this afternoon. Robots, hardware and the .NET Micro Framework.  Lots of things to go wrong, and most of them tried to. However, having carried my robot, a controller and a radio watch all the way around Europe I was going to do my level best to make sure that they worked at the end.

And work they did. In the end I had two debugging sessions running at the same time, one in the controller and the other in the robot. It was nice when the command went out of the controller and the robot program hit a breakpoint to indicate that the message had been received. Everyone was watching patiently to see the robot jump off the desk and shatter on the floor. Fortunately this didn’t actually happen, but the programs did all work.

Thanks for being a patient audience folks and remember, you are all embedded developers now.

Dev Days Keynote and Ambition

Rob

Also available for parties and discos…

Some time back I was asked the very perceptive question “What are your ambitions?” This is not a question that I get asked very often, and it gave me pause to think a bit about life. Eventually I  said “One day I’d like to take part in a keynote presentation at a technical conference”.  The keynote is the big session right at the start where everyone attending the conference turns up to hear some talks that set the scene for what follows.

Today I got to do that. I was on the same billing as Scott Hanselman, Wade Wegner and Ben Riga. I was talking about the joys of Windows Phone game development.

Dell

Everyone had their own laptop, mine is the Dell nearest (and they kept making fun of it because the video output was a bit dodgy).

Video

Brandon Foy’s video looks great, even from the back of the screen. Remember that you can see it here:

http://www.youtube.com/windowsphone

The presentations were all great, although I must admit I wasn’t concentrating that much – what with being the last man in.  My bit seemed to go OK, which was nice.

I now have to move on to my next ambition, which is to buy Bill Gates a drink. I’d just love to be standing at the bar with Bill, see him reach towards his pocket to pay for the glasses of Coke and say “No worries Bill, I’ve got this” and the produce my wallet….

Windows Phone XNA in TechDays Antwerp

Metropolis1000

This is the Metropolis in Antwerp. Nice venue.

Audience

This is my usual picture of the audience arriving.

Did my first Windows Phone session of this part of my “World Tour” ™. I’m at Tech-Days in the Netherlands tomorrow and Friday. Thanks for being a great audience folks, although with the bright lights it was a bit hard to see if there was anyone out there (although I did hear some clapping from time to time). You can find all the code I talked about, and a lot more, in my demo directory:

/demos

Heading to Antwerp

WP_000196

Some of these trains do not exist.

In the Netherlands trains don’t run late. They just don’t run. Which makes for all kinds of fun and games if you try to use the ones that aren’t there. The 14:07 from Schipol airport to Antwerp made a brief guest appearance on the station display before vanishing. After a while I got to recognise the “apologetic cough” in Dutch that pre-ceded messages about trains that weren’t going to run. Even though I couldn’t understand much of the rest of the announcements.

Fortunately they announce the international trains in English too (I still find the idea of trains that “go abroad” really exciting) and so I was able to figure out how to get to Rotterdam and then change for Antwerp. And here I am. The hotel is great, the presentations are written and the demos are working. I’ve even found a McDonalds to have tea in. Looking forward to tomorrow.

IMG_2719

Antwerp Central Station looking good.

Ready for Tech Days

clip_image001

I’m doing some bits and bobs at TechDays around Europe this week, starting with a Windows Phone gaming session in Antwerp on Wednesday 27th, then a keynote session, a Micro Framework session and a Geek Night Windows Phone session in rhyme on Thursday at The Hague in the Netherlands. On Friday, assuming I survive, I’m doing another Windows Phone gaming session. At the moment I’m busy preparing all the content and demos. I’m making something completely new for the keynote, and I’m rather pleased to find that it is mostly working, which is nice. 

You can find out about TechDays in the Netherlands here:

http://www.techdays.nl/

You can find out about TechDays in Antwerp here:

http://www.microsoft.com/belux/techdays/2011/

I head out tomorrow, I’m trying to squeeze a robot, microcontroller, laptop and all my other bits and bobs into a single carry on bag.  

Parrot Drone Flying

parrot Drone

The Parrot AR Drone is an amazing device. It contains a whole bunch of digital technology, including a WIFI access point, two cameras, an ultrasonic rangefinder and four computer controlled rotors. You can fly it from your iPhone and you see a view from the camera in the front of the device as you remotely pilot it around. Today we had a go at flying it, and it works really well, once you have told it that it is flying outdoors. This takes some limits off the behaviours, making it more able to cope with the small headwind.

It really is impressive to watch. It just hangs in the air waiting for commands, and it is very easy to control. In fact, in some ways it is too easy. If you leave it along it just flies, if you give it directions it just follows them, and then waits for some more. Even landing is mostly boring, except for once or twice when it flipped upside down for no reason. There are some augmented reality games you can get, and of course it gets really fun if you have someone else with a drone to fight against.

They are a bit pricey (fortunately I didn’t pay for this one, we use it in the department) but the biggest problem is the battery life. You can get around 10-12 minutes of flying time before the lithium ion battery needs a top up. I’d definitely get a couple of spares if I was going to get serious about flying a drone.

Mansions of Madness

Mansions of Madness

Now, this is what I call a board game. Cards, tokens, plastic figures, a die with 10 sides and a huge thick book of rules. Fortunately for us number one son was able to act as the one controlling the scenario, although fortunately might be going it a bit, as most of the time he was happily sending zombies and axe wielding maniacs our way.

The Mansions of Madness game is different each time you play it, with a set of different levels to work through. While this limits the lifetime of the game a bit, in that once you’ve played a level once you’d not want to play it again, it does make for quite a rich experience, and if you price it out in evenings worth of entertainment it starts to look good value, even if the game itself is quite expensive.

I played a gangster with a Tommy gun and a down on monsters in general, but with a quite astounding lack of physical ability, as evidenced by my knack of throwing exactly the wrong numbers consistently. However, with help from a typewriter wielding companion we managed to solve the gruesome mystery and get out alive. Towards the end things got really tense, as I struggled to shoot a zombie and avoid being burnt alive, while at the same time hunting to uncover the awful truth.

One good thing about this particular game is that although it is necessarily quite complex, the games themselves don’t take around a week or so to finish, we ended up having a couple of hours of fun, which is just about right I reckon. A good, social, alternative to watching the telly or playing video games methinks.

Get Portal 2

image

I’ve spent a reasonable amount of time over the last few days watching number one son play Portal 2. I quite like watching other people play video games. Whenever I play most of my time is spent being dead or otherwise inconvenienced. Number one son does not seem to have these problems, although in Portal the nice thing is that you are not actually under threat of sudden death most of the time. Unless you do something stupid in one of the puzzles that is of course. And there are a lot of puzzles. Along with the sing-song voice of the psychotic computer and another computer who also records adverts for a bank and works with Ricky Gervais. Or something.

The game is really good. Really good. So good I might just invest in a copy myself. Apparently if you get the PS3 version you also get a token allowing you to download a copy for your PC or Mac, which is nice.

I think it helps if you have played the previous version first, you can pick that up as part of the wonderful “Orange Box” compilation for most platforms.  

SS Great Britain

SS Great Britain Propellor

The SS Great Britain was one of the first large propeller powered vessels.

Up until the SS Great Britain nobody thought much of making ships out of metal. Especially iron, what with its well know lack of floating ability. Isambard Kingdom Brunel reckoned it would work though, and built an enormous ship to prove it.  I wish I’d thought to have given one of my kids the middle name “Kingdom”, but I digress.

Today we went round the first “modern” ship ever built. Instead of using wood, with lots of internal bracing and strengthening, Brunel decided you could make a perfectly workable boat out of metal plate. Moreover, making it really large would mean that you could carry enough coal on board to power the thing on long journeys.

And it worked. The ship had a long history, from carrying 200 passengers across the Atlantic in absolute luxury to carrying 600 would be gold diggers to Australia in conditions that must have been a lot less comfortable.   It ended its days as a gently rusting wool store in a bay in the Falkland Islands. Fortunately, after a lot of fund raising and effort it was brought back to Bristol, its spiritual home, and you can now go around and beneath it.

Well worth the trip. While it is sad to see the state of the vessel now, which must be mostly rust, it is very encouraging to see the work being done to keep it alive, and the imagination shown in making a look round as interesting as possible. They even have an “Isambard Brunel” wandering around in full Victorian dress, sideburns and stovepipe hat that you can chat to.

SS Great Britain Deck

The weather was very kind to us, and I took loads of pictures which will find their way onto these pages over time I’m sure.

Source Code Movie Review

image

In between shopping and helping to build furniture we managed to fit in a trip to the movies. We’d been meaning to see “Source Code” for a while, and everyone was glad of the trip. There seems to be a glut of “mystic” movies at the moment, what with “The Adjustment Bureau” and that one with Robert De Niro I’ve forgotten the name of.

Anyhoo, Source Code requires a bit of belief suspension, but once you get into it the film is really rather good.  Everyone plays their part very well and the action zips along at a nice pace, with one or two twists along the way. Worth seeing.

Dalby and Ducks

Went to Dalby Forest today. (having a tiny holiday) Great place. Once we arrived we had a coffee at the Purple Mountain Cafe. If you like riding bikes at great speed down steep hills you should go there. They arrange all kinds of races and stuff (including truly scary sounding 12 hour ones). Find out more here:

http://www.purplemountain.co.uk/

Dalby Bikes
“Excuse me, can you hire these bikes?”
"Certainly sir, they have a small adjustment at the back”

Note – this joke completely breaks if you write it down….

Then we went for a walk around, and I took the big camera.

Dalby walk
Look! Blue Sky!

We rounded off the trip with a visit to Thornton-Le-Dale, which has a lake and ducks.

Ducks

Nobody mention oranges.

Positioning Sprites with Rectangles and Vectors in XNA

Prada

Staying with the XNA techie theme, now a word about performing sprite positioning in a game. A sprite is something that has a texture (the image to be displayed when the sprite is drawn) and a position (the place to draw it). Generally sprites map onto objects in a game. If I am creating a game and I need to draw a bat, ball, spaceship, alien or asteroid I will use a sprite to do this.

There are two different ways to position sprites in XNA and each has its advantages and disadvantages. However, it is worth knowing about both.

Rectangle Positioning

With this form you create a rectangle which tells XNA where to draw the item, and how big it is:

Rectangle r = new Rectangle(0, 0, 200, 100);
spriteBatch.Draw(texture, r, Color.White);

This draws our texture in the top left hand corner (0,0) , in a rectangle 200 pixels wide and 100 pixels high.

Using a rectangle like this works well, it also gives you a ready made “bounding box” which you can use to test for collisions:

if (rect1.Intersects(rect2)) {
    // We have a collision between rect1 and rect2
}

However, there are some bad things about using rectangles that make me less keen on them

  • Rectangles are positioned using integers. The x and y properties you use describe where they are do not have a fractional part. This means that if you want to move a rectangle slowly around the screen (i.e. less than a pixel per update) you can’t just use the x and y properties to do this.
  • You can use rectangles to scale drawing, but this gets a bit tedious as you have to work out the size of rectangle you need, and you also need to be mindful of the aspect ratio of the item you are drawing so that it doesn’t end up squashed or stretched.
  • It is impossible to rotate a sprite which is positioned using a rectangle

So, rectangles are good for very simple sprites, but once you have become more familiar with XNA I think it is worth moving on to Vector positioning.

Vector Positioning

A vector is something that has direction and magnitude. That sounds posh. Actually it just contains an X and Y value, just like a coordinate. The “direction and magnitude” bit kicks in if you draw a line from the origin (0,0) to the X, Y position given. This line is the vector. The direction is the way the line points, and the magnitude is how long the line is.  In XNA terms the Vector2 type is the one you can use to position 2D sprites:

Vector2 v = new Vector2(10, 10);
spriteBatch.Draw(texture, v, Color.White);

This code draws the texture at position (10,10) . The texture is drawn in whatever size it happens to be, i.e. if the texture image was 200 pixels by 100 it would be drawn that size.  This means that you might need to scale your textures to fit a particular screen size – but as we shall see later this is not  a huge problem.

There are quite a few good things about vectors though.

  • The X and Y values of a vector are floating point, so you have very good control of sprite speed
  • The XNA framework supports vector mathematics directly. So you can write code like this:

    position = position + speed;

    - where position and speed are both vectors

When it comes to scaling and rotating a sprite positioned using a vector you can use a more complex version of the draw command (find a detailed description here) to do all this. Also, bear in mind that if you targeting Windows Phone you can fix your resolution in the game to a particular value and then make all your to assets fit.

graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 800;

The phone hardware will scale the display automatically to match whatever size you specify, now and in the future.

If you want to detect bounding box collisions you can write code like this:

public bool Contains(Vector2 pos)
{
    if (pos.X < position.X) return false;
    if (pos.X > (position.X + texture.Width)) return false;
    if (pos.Y < position.Y) return false;
    if (pos.Y > (position.Y + texture.Height)) return false;
    return true;
}

This returns true if the area covered by the sprite contains the given position.

For bounding box collisions you can use this:

public bool Intersects(Sprite c)
{
    if (pos.X + texture.Width < c.pos.X) return false;
    if (c.pos.X + c.texture.Width < pos.X) return false;
    if (pos.Y + texture.Height < c.pos.Y) return false;
    if (c.pos.Y + c.texture.Height < pos.Y) return false;
    return true;
}

This test will tell you if the bounding box around the two sprites intersects. However, if the sprites are textures that don’t fill the entire sprite rectangle this test is not a very accurate one. I’ll be covering pixel level collision detection next.

Game Object State Management in XNA

Vegas Building

Hmm. This sounds a bit deep for a Saturday blog post. I suppose it is, but I got asked a question at Mix 11 and I’ve been pondering it ever since then. The question was about some code like this:

class Cloud : CloudGame.ISprite
{
    public Texture2D CloudTexture;
    public Vector2 CloudPosition;
    public Vector2 CloudSpeed;
    public bool Burst = false;

    public void Draw(CloudGame game)
    {
        if (!Burst)
            game.spriteBatch.Draw(CloudTexture, 
                                                                       CloudPosition, 
                                                                       Color.White);
    }
   // rest of cloud code here
}

This is a tiny part of my “Cloud Bursting” game demo, where players touch clouds to burst them. The above code is the definition of the cloud itself. This has a texture, position, speed of movement and a Burst flag as properties.

Burst is how the cloud “knows” if the cloud has been burst. If the cloud is burst it is not drawn. At the start of the game all the clouds have the Burst flag set to false to indicate that they are still part of the game. As the player touches them the Burst flag is set true.

You see this in a lot of game play situations. Anywhere that things are “killed” they have to be able to remember whether they are still alive or not. The code above is simple and works well, at any instant a cloud is either burst or not. If it is burst (i.e. the Burst flag is true) plays no further part in the game and is not drawn.

However, this design gave rise to the question: “Why do we need to have a flag? Surely it would be more efficient to remove a ‘burst’ cloud from the list of things to be drawn?”

This is a very good question. To understand it you need to remember that in a game we will have many objects on the screen at any given time. To manage this the game contains a collection of some kind (usually a List). During Update and Draw the game works through the list acting on each item in it. If an item is removed from the list it will no longer be part of the game.  When the player bursts a cloud, rather than setting a flag you could just remove that cloud from the list of active game objects.

This approach has the advantage that it is more efficient. Instead of a cloud having to decide not to draw itself the game never tries to draw burst clouds at all.  However, it also adds complication. The game must now have a “master” list of clouds and an “active” list of clouds. At the start of a game the master cloud references are copied into the active list. This is to avoid the overheads of creating and destroying objects, something you really don’t want to be doing on a regular basis.

Furthermore, the time that is saved is probably not going to be that much use to us. If the game works fine with all the clouds visible (which it should do) then saving small amounts of processor time when some clouds are removed is not going to make that much difference to performance. In this situation it is the drawing of the objects that takes the time, not deciding whether or not to draw them.

The fundamental principle here is that you should go for simplicity first, then optimise when you discover you have a performance issue.  I like having the flags present, it makes for greater cohesion and easier debugging. I can look at any cloud and decide whether or not it should be drawn simply by inspecting the flag.  If the flag wasn’t there I’d have to check which lists held the cloud, and so on.

So, I go with internal flags, not lists, which is pretty much what I said at the time as I remember.

Some Mix Videos to Watch

Walk

If you weren’t at Mix and you want to watch the video of my session you can now view it here:

http://channel9.msdn.com/Events/MIX/MIX11/EXT06

You can get all the demo code that I talk about here:

/demos

This afternoon I was invited to take part in a Live Channel 9 session on video game design. Also on the stage was Brandon Foy, perhaps the coolest person I have ever met. He is the creator of an astonishing Windows Phone fan video that you can view on YouTube here:

http://www.youtube.com/windowsphone

Microsoft have said that if they get more that 200,000 views they will pay to put it out as a TV advertisement.  So, if you want to cost Microsoft some money, go take a look. Actually, it is a stunning piece of work and well worth 2 minutes of anyone’s time.

The video game session itself was great fun, as well as Brandon there was Nic Fillingham  from the XNA team and John Papa kept us all under control. For me the interesting thing was how much we all agreed on what is important in video game design. In a nutshell:

  • Figure out what your game is about and then protect this idea against all who would wish to “improve” it.
  • Get help with the graphics. Programmers think they can solve any problem with software. They probably can, but graphical design is not a problem as such. You really need someone with a good eye for that, and a coder is not guaranteed to be that person.
  • Put your game into the hands of people who can give you proper feedback. This doesn’t mean family or friends, this means those whose only reason the like the game is the game itself.
  • Design in the persistence features right at the start. A phone game will have to stop and start. When you make the game objects you need to figure out which bits must be saved and when. Adding this stuff right at the end of the development is really hard. If you have this stuff there all the time it means it will be properly tested when you send the game out.

After this I staggered back to the hotel to do some packing. My flight leaves at 7:15 am tomorrow. Lovely.