Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site

Entries from March 20, 2011 - March 26, 2011

Saturday
Mar262011

Three Thing Game Team Videos

I’ve been round all the teams and done brief videos of how they are getting on:

Awesome Source

Bi Winning
Blank
Bottled Games
Brayshaw’s Bandits

CMYK
E for idiots
Fez Productions
Fresh Pot
Left 4 Dev
Pixel Bomb
So Fear
To Be Confirmed
Troll
Two and a Half Life

I’ve also put captions on the pictures on Flickr..

http://www.flickr.com/search/?q=threethinggame

I’ll do this all again later tonight, and see what progress we’ve made.

Saturday
Mar262011

Three Thing Game Gets Going

Three Thing Games Teams

These are the teams at the start of the competition. I’ll do another of everyone at the end…..

Three Thing Game Prizes

Awesome prizes, I think this explains the good turnout.

We’ve launched the game development competition today. We have over 60 students involved.  They are all busily in the development phase of their games at the moment, just a few hours left now…..

If you want to see what they are up to we have a variety of webcam feeds for you to choose from:

http://www.ustream.tv/channel/twenty-four-hour-game-dev

http://www.ustream.tv/channel/24-hour-game-dev

http://www.ustream.tv/channel/fresh-pot

I’m putting regular picture updates on Flickr and there will be videos later when I’ve been round and chatted to all the teams. And everyone is tweeting as well. The tag threethinggame will get you where you want to be.

Thursday
Mar242011

Pex for Windows Phone 7

image

I write programs for fun. Seriously. I’ve got games at home still in the box because I’d rather fiddle with some C# on Windows Phone than try to kill demons/drive cars/fly spaceships etc etc.

Pex is a great way to play games while you write programs. I’ve played with the web version before and now I can do that on my phone too. You can download Pex4fun and play along too. If you have any interest in brushing up your C# smarts in a fun (and portable) way then get this on your phone. It’s free too.

Wednesday
Mar232011

Another Open Day, Another Winner

Another Winner

Warren hands out another free copy of my book. We should have the proper ones for posting out tomorrow.

We had another great open day today, loads of people turned up and I hope you found it worth the trip. One group had travelled up from the south coast, well done. Perhaps we should have a prize for the most travelled guest.

Tuesday
Mar222011

Using Vectors to Position Draw actions in XNA

I did a session yesterday for Three Thing Game (work progressing nicely) about positioning objects in XNA using vectors. I said I’d post the content, and here it is.

The basis of the chat was that for maximum object drawing satisfaction you should use vectors to position objects you want to draw  on the screen. I then had all kinds of fun with Visual Studio IntelliSense (bless its cotton socks) trying to be “helpful” as I filled in the more complicated draw calls.

The simplest version of XNA drawing uses a Rectangle to specify the position and size of the item to be drawn. This works well, but it does have some disadvantages, the most notable of which is that you can’t rotate sprites when you draw them this way. Another significant problem is that the position of a rectangle is specified by an integer value, which means that if you want to make something which changing in position by less than one pixel per update (which is often the case) then you can’t just use a rectangle to record its position.  Vectors contain floating point values, which give you much more precise object positioning.

So, my vector powered sprite starts off as two things:

Texture2D logo;
Vector2 logoPosition;

The logo gives the image I want to draw, and the logoPosition says where. I can use a very simple drawing call to draw using this if I like:

spriteBatch.Draw(logo, logoPosition, Color.Red);

This draws the logo using a red light at the position held in the vector. The position is given as the top left hand corner of the texture, so this call works very like the use of a Rectangle to position the texture, where the X and Y properties of the Rectangle give the position of the top left hand corner of the object.  The difference is that with this draw operation the texture is always drawn full size. If you use a Rectangle you can give width and height values that will get XNA to scale and transform your texture when it is drawn. If you want to do this kind of thing you have to use the monster version of Draw, which lets you specify all kinds of fun stuff:

spriteBatch.Draw(
    logo,           // Texture
    logoPosition,   // Position
    null,           // Source area in texture to draw
                    // null for all of it
    Color.Blue,    // draw colour
    logoRotation,   // rotation angle
    new Vector2(logo.Width / 2.0f, logo.Height / 2.0f),
                    // origin of the draw
    1,              // scale value
    SpriteEffects.None, // can flip the sprite
    0);             //  layerdepth

 

This version of Draw lets you do pretty much anything. You still give it a texture and a position but you also give it a whole heap more. We can look at some of these:

Source Area: this is the region of the source texture you want to draw. This lets you have a single sheet of sprites and then pull the particular one you want to draw from that sheet. Alternatively you can have a huge map texture and then just display part of it. If you put null there this means the entire texture is used as the source,  which is what I’ve done.

Rotation Angle: this is really nice. You can rotate your sprites when you draw them. The angle is given in radians - which means that the value 2 * pi (6.28 or so) is a complete revolution.

Origin of the Draw: OK. This bit is fiddly. The origin is given in coordinates relative to the source texture (i.e. if the texture is 100 pixels square the value 50,50 would mean the centre of the sprite). I do a simple sum to get this centre position in my example. This is the point at which the draw and rotation behaviours will act. This means that if you do both draw operations with the position vector holding the same value you get an effect like the one you see below:

image

The red logo is being drawn with the vector giving the top left hand corner of the draw operation. The blue logo is being drawn with the origin set to the middle of the texture as shown above.  To make things even more interesting I’ve rotated the logo a bit. Having  a draw action that works on the middle of the sprite is actually very useful if you want to centre draws around things like touch positions. The thing to remember is that the units for this value are relative to the texture, not the drawing coordinates.

Scale Value: This scales the draw. If you want to stretch the image in X or Y there is another version of Draw where this is given as a vector where you can express different X and Y scale values.

Sprite Effects: You can flip or mirror the sprite with this.

Layer Depth: Normally XNA will layer things in the order they are drawn, in other words new draws will lay on top of older ones. If you want to control the layering of your drawing you can set a layer depth for a draw action.  A depth of 0 means the front and 1 means the back. If you use the simple version of spriteBatch.Begin() these depth values are ignored, they only have an effect if you select an appropriate SpriteSortMode value.