I’ve moved

September 4, 2009 at 11:05 pm (Other Stuff)

It’s about time I got a real domain name for this thing. Please update your book marks to the new blog at http://www.conkerjo.com/
Please Mind the dust while I work with the new theme 🙂

Permalink Leave a Comment

The Week in Code by Björn

July 14, 2009 at 1:18 am (Other Stuff, XNA) (, )

Björn (or boki) has written his 7th instalment of The Week in Code. Hit the RSS button and maybe he’ll feel obliged to keep it up and add some more. I’m in there 2 weeks on the trot by the way 😀

Björn’s XNA Adventures

Permalink Leave a Comment

XNA & Game A.I

July 7, 2009 at 7:28 pm (Other Stuff, XNA) (, , )

Over the past few weeks I have been developing an A.I middleware library for use with XNA games. I’ve spent a lot of this time in google searching for A.I articles and samples. It’s a force of habit to append XNA to the end of my code related searches and I wasn’t finding the information I needed to get the inspiration for my A.I library. I did figure out how to use google eventually and found lots of good, general A.I articles and samples. Most samples are c++ but it’s more about the concepts of A.I I was looking for than the samples.

At some point in the very near future I will be releasing the source code to this library along with a sandbox for you to see how to use it and to play around with it. One thing I’m struggling with is deciding what small chunks of A.I I should demonstrate. If you have some ideas of what you would like from an A.I sandbox please comment on this post and we can discuss.

In the meantime, here is a bunch of links I’ve been reading over the past few weeks relating A.I and some XNA specific ones I did find.


XNA Creators Club Online


Permalink 3 Comments

Spatial hashing implementation for fast 2D collisions

June 13, 2009 at 6:15 pm (Other Stuff, XNA) (, , , , )

This is a sample prototype I wrote to fix a performance limitation with collision checking in SBARG. So what is spatial hashing? Here is a good 1 liner I will borrow from the source material i used.

“Spatial hashing is a process by which a 3D or 2D domain space is projected into a 1D hash table.” Optimization of Large-Scale, Real-Time Simulations by Spatial Hashing

So why do we need it?

Well, for me it was a problem in my collision code and my A.I code which was trying to find nearby objects to check collisions for. Due to my brute force nature if I had 10 monsters in the world there would be 10*10 = 100 collision checks in every update. Now ramp the number up to 100 to be a bit excessive and we end up with 100*100=10,000 collision checks. This makes the cpu cry like a little baby as I’m sure you can imagine. To rectify this we need to reduce the amount of collision checks we need to do in the first place. This is where spatial hashing comes in handy.

Imagine the game world in a flat 2d grid. 100 by 100 pixels and each cell was 25 by 25 pixels. Now number the cells from left to right, top to bottom 0 onwards. You will end up with something like this. The orange circles are the game objects, in my case, monsters.


Each cell is a bucket of game objects and a unique hash id. If we imagine the bucket as a list of 16 buckets, 0-15 cells and placed the game objects in that bucket. It might look something like this.


This is our 1D grid mentioned in the introduction.

It’s a simple premise really, any item in bucket 3 for example, cannot possibly collide with something in bucket 9. This reduces the amount of times we need to cycle the nearby objects but also dramatically reduce the amount of times we need to check if a collision is happening.

Ok, so the above implementation is fine as long as a game object only ever exists in 1 bucket. But what if it crosses a line and exists in more than 1 bucket. To resolve this I imagined a box around each game object, and I calculated the hash id for each corner of the box. I then populate a List<GameObject> going through each bucket the game object is in. Sounds simple ?

Let me show you some of this theory in code.

First we need a game object. For this sample all we need is a position and a radius.

    public class GameObject
        public Vector2 Position { get; set; }
        public float Radius { get; set; }

We create a new class to store the grid data in including the buckets. I’m terrible at naming classes so I called it SpatialManager. I gave this class a Setup method which takes a full scenewidth, height and a cellsize. In our example this would be 100,100,25

    public void Setup(int scenewidth, int sceneheight, int cellsize)

We can work out how many buckets we need by first calculating the rows and cols then simply create a new Dictionary of buckets to the tune of Rows*Cols. I also store these variables passed in for future use.

    Cols= scenewidth / cellsize;
    Rows= sceneheight / cellsize;
    Buckets = new Dictionary<int  , list><gameobject>(Cols * Rows);

    for (int i = 0; i < Cols*Rows; i++)
        Buckets.Add(i, new List());

    SceneWidth = scenewidth;
    SceneHeight = sceneheight;
    CellSize = cellsize;

Each update, we need to clear out the buckets and re calculate the buckets each game object are in. I created a method called ClearBuckets to start fresh.

   internal void ClearBuckets()  
       for (int i = 0; i < Cols * Rows; i++)
           Buckets.Add(i, new List());   

We now need a method to register a game object into the buckets it sits in.

    internal void RegisterObject(GameObject obj)
        List cellIds= GetIdForObj(obj);
        foreach (var item in cellIds)

As you can see, the code retrieves a list of cellids to add the game object to.

In the GetIdForObj method it calculates the cell id for each corner of the game object.

If we were just checking the position of the game object the calculation would be.

float width = SceneWidth / CellSize; // 100 / 25

int hashid=(int)(

    (Math.Floor(position.X / CellSize)) +

    (Math.Floor(position.Y / CellSize)) * width);

We need to do this for each corner and add our game to each bucket.

The GetIdForObj method looks like this.

    private List GetIdForObj(GameObject obj)
        List bucketsObjIsIn = new List();
        Vector2 min = new Vector2(
            obj.Position.X - (obj.Radius),
            obj.Position.Y - (obj.Radius));   
        Vector2 max = new Vector2(
            obj.Position.X + (obj.Radius),
            obj.Position.Y + (obj.Radius));

        float width = SceneWidth / CellSize;   
        AddBucket(new Vector2(max.X, min.Y), width, bucketsObjIsIn);
        AddBucket(new Vector2(max.X, max.Y), width, bucketsObjIsIn);
        AddBucket(new Vector2(min.X, max.Y), width, bucketsObjIsIn);

	return bucketsObjIsIn;    

And here is the AddBucket method which uses the calculation described above and adds it to the list of bucket id’s to add to.

    private void AddBucket(Vector2 vector,float width,List buckettoaddto)
        int cellPosition = (int)(
                   (Math.Floor(vector.X / CellSize)) +
                   (Math.Floor(vector.Y / CellSize)) *

Now that we have our grid of buckets. It’s a very simple retrieval process. I added a method to get the nearby objects of a given object. This uses the GetIdForObj method and populates a list of GameObject’s and returns once complete. This is the key part to this solution, you only now need to check items which are actually nearby and not items the other side of the theoretical world.

    internal List GetNearby(GameObject obj)
        List objects = new List();
        List bucketIds = GetIdForObj(obj);
        foreach (var item in bucketIds)
        return objects;   

So that’s it. Now you can do your normal collision checking by retrieving nearby GameObjects. And here’s a screenshot to prove it.

I’ve placed my mouse over one of the GameObjects and it’s highlighted all nearby items. Notice how they cross over cells. This is because the item i hover over is on the line and exists in multiple cells. Also notice the amount of checks it has to do. For brute force it has to do 250,000 bounding box collision checks. For spatial hashing, it checks only 4840 times. Wicked.


And what would a sample be without source code?


Link To Sample

Permalink 25 Comments

Simply RenderTargets

May 26, 2009 at 6:39 pm (Other Stuff, XNA) (, , )

Somebody in the #xna IRC channel just asked how to use RenderTargets to only draw a portion of the screen. Here’s my answer.

I guessed from his brief description he might have a game scene which didn’t look like this



And the result he wanted, was something almost like … this ?


What you see here is the game scene drawn, with a triangle portion visible and the rest not so much. My favourite way to achieve this is to use 2 RenderTargets, A VERY simple Effect(shader) and good old SpriteBatch. So what do we use all this for?

First we need to draw the game scene. There is a way around this but to make things easier to understand we will draw this to a RenderTarget. Simply put, a RenderTarget is an imaginary “screen” to draw to which you can specify the size of. Then once done, you can use the texture like any normal Texture2D you might load from the Content Pipeline.

To draw the scene we first want to set the correct RenderTarget, then use spritebatch just like you would to draw any other sprite. Like this.

            GraphicsDevice.SetRenderTarget(0, _gameRT);
            //Clear the RT screen
            //Draw the game


Simple uh?

Now we need to create another RenderTarget, and Draw a triangle to it. This will never be displayed in the final scene. Although with the RenderTarget it allows you to if you so desire, maybe for debugging purposes?

            GraphicsDevice.SetRenderTarget(0, _lightRT);
            //Clears the screen transparent.


This code is almost identical to the last except we draw the _triangle Texture which looks like this.


And we use the color transparent black.

The final thing we need to do now is draw it to the scene.

If we just draw the triangle on top of the game scene you will see the game scene with a white triangle on top. This isnt what we want. So we use a shader to take the game scene, and change the alpha channel corresponding to the white triangle. Wherever there is a solid non transparent color pixel, this will be set to a fully non transparent pixel in the game result, however, a transparent pixel in the triangle image will result in a completely transparent pixel in the final result. Resulting in you not being able to see that pixel. The final result is like this.



The shader code is very simple. It is a pixel shader which takes a AlphaTexture parameter which is the texture taken from the triangle RT. It takes the alpha from that texture, and sets the coresponding pixel on the final image to be the same.

float4 PixelShader(float2 texCoord: TEXCOORD0) : COLOR
	float4 Color = tex2D(ScreenS, texCoord);
    float alphaLayerAlpha = tex2D(AlphaSampler, texCoord).a;
    return Color;  


To use this we just create a new Effect variable and load it in LoadContent. Call Begin/End in the appropriate places, set the parametar and hey presto. A RESULT!!!

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);


To get a better grasp of the code you can download the sample project here.


As I said, Someone in IRC literally just asked. So I knocked this up. Apologies for the rushed post 🙂 BACK TO SBARG!!!

Permalink 2 Comments

XBox Live Community Games Listings

April 27, 2009 at 4:59 pm (Other Stuff, XNA) (, , , )

A community created (Nick Gravelyn)  website has existed for some time which set out to emulate the functionality of the Xbox Marketplace but until very recently, it kind of sucked to look at. The information was there, but it was a fairly dull and average affair. But now, wow. Take a look for yourself. It’s almost like a direct copy of the Xbox Marketplace but it has 2 major benefits. It’s not slow, so you can go to it today, and read it today, and the other is user ratings. Go check it out for yourself, oh a third thing, the Box Art Collage is pretty sweet too, oh, and Random Game, i could go on but here’s the link. I’m going to find some new games to play 🙂


Congratulations to Nick Gravelyn & Scott Wendt & Björn Graf for getting this out without too much of a hiccup, I can see why Nick was so excited to get it out now. Great work with the new site.


Permalink Leave a Comment

XBOX Community Games Sales – On Reflection

March 31, 2009 at 7:06 pm (Other Stuff, XNA)

Earlier I posted about the initial reaction the XBOX Community Games sales data, I immediately changed my perception of the results we know and had I not had an amazing new feature to add to Level 7 I would have responded earlier.
Some of the reaction in the forums is a little naive like with the I have my sales numbers and I want to cry thread, I must say though there are plenty of developers on there with their head screwed on and got almost exactly what they anticipated.

There has been awesome coverage of this new found information about the Community Games. If anything it’s good publicity for the service.. “There is NOT a free money pit, look elsewhere” or something, but on the other hand with analysis such as gamerbytes did here we see that some of the better quality games, and games which have done their own hard work to promote their own game, have come out on top. Wordsoup has performed incredibly well, congratulations to those guys.

I think this has worked out perfectly. The service is going to gain more coverage, even if its more developers downloading trials, it’s something more than now and it will continue to grow. But it will also inspire the hard working people with passion to make even more great games. I particularlyyyy like ZP2K9 and Solar, a handful of others too but these 2 I play often, except.. ZP2K9 needs more customers OK!! so go buy it immediately and make James Silva even more richerererer.

P.S Did you also know? you can go onto the market place wherever you have a browser and view info about all of these games available. Good stuff? Find something you like? Hit the Game Demo button, or the Full game button if you’re feeling flush, or if the game is called SBARG, then down below is a button saying “Download to Xbox 360” then when you get home and turn on your Xbox 360, which you do as your priority of course, it will automatically download them for you. Make a brew, and you’re ready to go demo some Community Games.

Permalink Leave a Comment

XBox Community Games Submission Tests

February 23, 2009 at 6:49 pm (Other Stuff, XNA) (, , )

I was looking around the internet just a few days ago for a checklist of the not so common tests we might do before submitting our games for review to the Community Games Channel. I came across this post in the XNA forums which is just what I was looking for. I recommend you bookmark it if you are planning to publish anything to the Community Games channel.
Credit goes to The ZMAN for this post – (Update) I just noticed that isnt TheZman’s post. Apologies to Spyn Doctor who did infact write the original post

Permalink 1 Comment

Pimpin XNA Roundup!

February 23, 2009 at 6:04 pm (Other Stuff, XNA)

XNA Roundup

As the time draws nearer to my game being finished, I’m finding it easier to procrastinate. I’ve caught up on the new show XNA Roundup on youtube and I particularly liked the look of one of this weeks games ArkX which is essentially a clone of Arkenoid or Breakout as I knew it. You can catchup on the show here http://xnaroundup.blogspot.com/
This game kind of goes against my XBCG moral’s of not playing the stupidly simple game or totally not appropriate game and this falls under the category of the former by name association alone. Which isn’t fair i know, but it’s just how I and many others feel about where XBCG is really going, however this weeks XNA Roundup has sure caught my attention and I am going to try ArkX out for sure, along with a couple of others I saw in the earlier episodes.

I recommend you subscribe to the iTunes podcast or just favourite his youtube page, there are a couple of games that just hit Community Games playtest that look really exciting from first glance, so they should be hitting the Marketplace sometime soon.

Permalink Leave a Comment