Creating and consuming services in your XNA Game

February 18, 2010 .net

The GameServiceContainer implements the IServiceProvider interface and the MSDN documentation says about the IServiceProvider interface:

Defines a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.

This article will "attempt" to describe how can you use the GameServiceContainer in your XNA game, in both your GameComponent(s) and your game's entity objects.

Read More

Changing the PlatformTarget in Visual C# Express

January 18, 2010 .net msbuild

Some project types in Visual C# Express (Empty Project) will not allow you to change the PlatformTarget from the UI. You can still change the target platform though by editing the .csproj file in a text editor. Close the project and open it up in your favorite text editor (I use Notpad++). The .csproj file is really just a XML file. You should see somewhere in the file something like:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
	<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

Inside the PropertyGroup elements, add the PlatformTarget element:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
	<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">

Save the file and open your project back up. Your project's output should now target only x86.

Calculating an angle from a Vector2

February 28, 2009 .net vectors xna

When you need to calculate an angle from a Vector2 structure, you can use this piece of code:

public static class Vector2Helper
	public static float CalculateAngle(Vector2 v)
		float angle = 0.0f;

		if(v != Vector2.Zero)

			angle = (float)Math.Acos(v.Y);

			if(v.X &lt; 0.0f)
			angle = -angle;

		return angle;

I used this to calculate an angle from the Vector2 of the Left Stick.

The original credit for this source code comes from here.

The Game Show Host Problem, aka The Monty Hall Problem

February 12, 2009

NOTE: This is a repost from my old blog.

So, me and my girlfriend went to see 21 last night and in the movie they make mention of The Game Show Host problem, aka The Monty Hall Problem.

The jist of the problem is this: You are on a game show. The host presents you with 3 doors, 1 of which has a car behind it, the other 2 have goats. The game show host tells you to pick a door. You do so, at which point the game show host opens up a door to show you a goat behind the door and then asks you if you would like to switch your choice to the other closed door. The question is then, should you switch your choice?

The correct answer is yes. More on why after the jump.

I won’t go too far into detail about why you should switch your answer, I’ll leave that to Wikipedia. Some things to note that may not be obvious: 1) The game show host will always open a door that is not the correct door 2) He will never open your door. These are the keys to this problem. By switching, you will win a prize 2/3 of the time as opposed to only winning 1/3 of the time if you do not.

Many people will argue that once the game show host opens the door with the goat behind it that there is now a 50% chance of you picking the right door by either staying with your door or switching. This is simply not true. Each door still only has 33.3% chance of being the door with the car behind it. The thing is though, once you pick your door, the game show host then eliminates a door based on 2 criteria: 1) The door is not the one with the prize behind it 2) The door is not yours. Due to these criteria, the odds of the correct door do not change for the door that you have picked, but rather change for the doors that you have not picked. The 2 doors not chosen by you then in a sense combine into one option and they together have a probability of 66.7%.

The Wikipedia article explains in much more mathematical detail why it is better to switch. I suggest you look there if I have done nothing but confuse you. This is simple strategy and probability. Knowing exactly how the game works can make you alot better at it.

Drawing 2D Lines as Rotated Quads

February 07, 2009 .net

I haven't had much time lately with work but one question I've seen asked many many times is how to draw lines of different widths. So, to cut to the chase I'll share the code I've used to do it.

public void DrawLine(Vector3 p1, Color c1, Vector3 p2, Color c2, int width)
	float distance = Vector3.Distance(p1, p2);
	float halfDistance = distance / 2.0f;
	float halfWidth = width / 2.0f;

	Vector3 difference = p2 - p1;
	Vector3 destination = new Vector3(p1.X + difference.X / 2.0f, p1.Y + difference.Y / 2.0f, p1.Z + difference.Z);

	// Calculate angle between two points
	float angle = (float)Math.Atan2(difference.Y, difference.X);

	Vector3 v1, v2, v3, v4;

	v1 = new Vector3(-halfDistance, -halfWidth, 0); // Top Left
	v2 = new Vector3(halfDistance, -halfWidth, 0); // Top Right
	v3 = new Vector3(halfDistance, halfWidth, 0); // Bottom Right
	v4 = new Vector3(-halfDistance, halfWidth, 0); // Bottom Left

	Matrix m =
		Matrix.Identity *
		Matrix.CreateRotationZ(angle) *

	v1 = Vector3.Transform(v1, m);
	v2 = Vector3.Transform(v2, m);
	v3 = Vector3.Transform(v3, m);
	v4 = Vector3.Transform(v4, m);

	DrawQuad(v1, c1, v2, c2, v3, c2, v4, c1);

I've left a lot of fluff code out. I usually check if the line is a width of 1 and draw a normal line. I also left out the code on how to draw a quad as that can be found many other places already.