XImplosionX

Step into my mind.

Intro to the Kinect SDK–Drawing Joints in XNA

It’s been a few days since Microsoft released the Kinect for Windows SDK, and we’re already seeing a lot of work being done. I decided to get my hands dirty and try out the fancy new skeletal tracking. First things first, you’re going to need to make sure you have some pre-requisites:

With all of these requirements satisfied, you can get started.

The first thing you need to do is add a few variables to the project:

SkeletonData skeleton;
Runtime nui;

These will be used to track data from the Skeleton provided by the Kinect SDK. These will be used later.

Next, in the LoadContent function of the Primitives3DGame class, add the following code:

nui = new Runtime();
nui.Initialize(RuntimeOptions.UseSkeletalTracking);
nui.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nui_SkeletonFrameReady);
nui.NuiCamera.ElevationAngle = 0;

In these five lines of code we’ve initialized the Kinect Runtime engine to use the Skeletal Tracking feature of the Kinect, created an event handler for the SkeletonFrameReady event, and made sure the Kinect is not elevated from a previous use.

Next we’ll want to add some code to that nui_SkeletonFrameReady event handler:

void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
    foreach (SkeletonData s in e.SkeletonFrame.Skeletons)
    {
        if (s.TrackingState == SkeletonTrackingState.Tracked)
        {
            skeleton = s;
        }
    }
}

This function is called every time the program gets skeleton data from the Kinect. We make sure that the SkeletonEngine is currently tracking a skeleton, and then we make a reference to that skeleton so it can be rendered later.

Now we want to modify the Draw function of the sample:

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    if (isWireframe)
    {
        GraphicsDevice.RasterizerState = wireFrameState;
    }
    else
    {
        GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
    }

    Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, -20), new Vector3(0, 0, 100), Vector3.Up);
    Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                                                GraphicsDevice.Viewport.AspectRatio,
                                                1.0f,
                                                100);

    // Draw the current primitive.
    GeometricPrimitive currentPrimitive = primitives[currentPrimitiveIndex];
    Color color = colors[currentColorIndex];

    DrawPrimitveSkeleton(currentPrimitive, view, projection, color);

    // Reset the fill mode renderstate.
    GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

    // Draw overlay text.
    string text = "A or tap top of screen = Change primitive\n" +
                  "B or tap bottom left of screen = Change color\n" +
                  "Y or tap bottom right of screen = Toggle wireframe";

    spriteBatch.Begin();
    spriteBatch.DrawString(spriteFont, text, new Vector2(48, 48), Color.White);
    spriteBatch.End();

    base.Draw(gameTime);
}

There are a few things to note here, as this function is quite different than the draw function of the sample. The first thing is the view and projection matrices. We need to move the View Matrix further back from the Origin to get a better view of the skeleton. Then we simply pass any relevent data to the DrawPrimitiveSkeleton function, and allow that to draw all of the joints:

private void DrawPrimitveSkeleton(GeometricPrimitive primitive, Matrix view, Matrix projection, Color color)
{
    try
    {
        if (skeleton != null)
        {
            if (skeleton.TrackingState == SkeletonTrackingState.Tracked)
            {
                foreach (Joint joint in skeleton.Joints)
                {
                    var position = ConvertRealWorldPoint(joint.Position);
                    Matrix world = new Matrix();
                    world = Matrix.CreateTranslation(position);
                    primitive.Draw(world, view, projection, color);
                }
            }
        }
    }
    catch
    {

    }
}

So all we do in this function is check to see if the current Skeleton exists, and then we enumerate through each joint of the skeleton, drawing it to the screen. Note the ConvertRealWorldPoint function:

private Vector3 ConvertRealWorldPoint(Vector position)
{
    var returnVector = new Vector3();
    returnVector.X = position.X * 10;
    returnVector.Y = position.Y * 10;
    returnVector.Z = position.Z;
    return returnVector;
}

All we’re doing here is taking the postion from the Kinect SDK and scaling it up to be used in a 3D World. There are probably better approaches to this, so I’d like to see how anyone else does this. We then take that point and create a World Matrix to draw the primitve for each joint.

Once you’re done, run the sample and stand in front of your Kinect Sensor. The result should look something like this:

screen1

screen2

I’ve uploaded my version of the sample here so you can take a look and compare. Credit for the 3D Primitive Sample goes to Microsoft and the XNA Community Team. Let me know what you think in the comments, and please give me any feedback.

Enjoy,

Patrick Godwin

ASP.NET MVC3 on Windows Azure

In working with Windows Azure, I’ve noticed that the v1.3 of the Azure tools only support ASP.NET MVC2. This makes sense, given that that version of the tooling was released before ASP.NET MVC3 RTM’d. But what if we want to deploy an MVC3 application to Azure? I had to hunt around the internet for some info on this, so I figured I’d make a simple walkthrough for those who need the visuals. Well it’s easy, but takes a few steps.

First, open up Visual Studio and create a blank solution for the project:

step1

Next, add an ASP.NET MVC3 application to the solution:

step2

Next, we need to make sure that the ASP.NET MVC3 Assembly is set up for a bin deployment, as Azure does not yet support MVC3 out of the box. To do this, we set the “Copy Local” property of the System.Web.MVC.dll to true:

step3

Now, it’s time to add our Windows Azure project to the solution. Add another project to the solution, this time choose a “Windows Azure Project”:

Step4

You’ll now be asked what kind of Web or Worker Roles you’d like to add to the Azure project. You can feel free to add any Role you choose, but for this sample we’ll just hit OK with no Roles added:

Step5

Now we’re ready to add our MVC3 Project to our Azure project as a Web Role. Right click on Roles in the Azure project, mouse over Add and select “Web Role in Solution”:

Step6

Select your MVC3 Application and hit OK. It should be in the list of projects:

Step7

Now hit debug on the solution and Viola! MVC3 in the cloud! It should be noted that in order to debug Windows Azure applications, Visual Studio needs to be run at an elevated level.

It’s simple enough to do, and I’m sure the team in charge of the Azure tools for Visual Studio will fix this with the next release. Until then, enjoy ASP.NET MVC3. You can grab ASP.NET MVC3, the Windows Azure tools, and all sorts of other goodies using the Microsoft Web Platform Installer.

Questions? Comments? Feel free to hit me up on Twitter, or leave a comment here on the blog.

Until next time,
Patrick Godwin

Imagine Cup US 2010 Finals – Anthony Salcito Interview

During the first day of the US Imagine Cup Finals, I had the pleasure to speak with Anthony Salcito, Microsoft’s Vice President of Worldwide Education. We talked about the need for new approaches to technology in education, and then spoke for a while about what the Imagine Cup means for Microsoft and for the community. I filmed our conversation about the Imagine Cup, which you can watch below. A very special thanks to Austin Stewart, from Microsoft’s PR team, who helped get me this great opportunity to meet Mr. Salcito.

More tomorrow morning.

Microsoft Student Insider Kick-Off: Scott Hanselman

During my time at the Redmond campus, I was fortunate enough to meet with Scott Hanselman, Principal Program Manger Lead for Microsoft’s Developer Division. He also runs a very successful blog and podcast. Mr. Hanselman’s position at Microsoft requires him to travel a lot. This, combined with his living in Oregon, makes it difficult to snag a meeting with him. But we were lucky enough to meet with Mr. Hanselman for an hour in Building 5.

We started off our time with Mr. Hanselman by introducing ourselves. He then lectured us on the history of Bulletin Board Systems, relating it to a recent interview he had done on his podcasts. He then talked about his blog, and how useful social media is.

And then we got to the new technology. Mr. Hanselman first showed us Boot to VHD, a new feature in WIndows 7 that allows us to take the VHDs created in Virtual PC 2007, Hyper-V, and other parts of the virtualization platform, and boot our machine to this virtual OS. This allows the virtual hard disk to use hardware rather than the emulated environment when running. This follows the idea of “less virtual, more machine”.

The next demos Mr. Hanselman showed us struck me as very cool. He decided to demonstrate two new features of .Net 4.0: PLINQ and MEF. Now when I heard him say PLINQ, I had to do a double take. My understanding was PLINQ had been around for at least a year. But I was happily surprised to find that PLINQ (Parallel Language Integrated Query) was finally shipping as a part of the .Net Framework. This made me happy. Finally I could add support for multi-core processors with very little hassle. Simply invoke a query with the .AsParrallel() extension method, and you’ve got yourself a query working across multiple cores. There are other features in PLINQ, and I urge you to check out the Parallel Programming With .Net blog for more info.

The final demo Mr. Hanselman showed us was MEF. MEF stands for the Managed Extensibility Framework. Imagine the following situation: You are an enterprise developer working for a company that keeps inventory of different kinds of cars. Each car has similar qualities, defined in an interface, while being unique in itself. Your boss approaches you, requiring a new brand be added to the application used for keeping inventory. Due to the lack of a quality plugin system, this task would be very tedious and an all around pain in the neck. What MEF allows you to do, is simply create your new class, implementing this interface, export it through a new DLL, and the inventory application will do a “composition”, loading any and all classes that use the interface declared in the core of the API. I was extremely excited when Mr. Hanselman showed us this new feature. I’ve always loved the idea of extensibility, but hated using slow Reflection based techniques for dynamically loading classes. I highly recommend you take a look at MEF over on CodePlex. It’s a very cool framework and could make great leaps in improving application development productivity.

After that demo, we had to go our separate ways again. Like I said, Mr. Hanselman is a very busy man, and I was very grateful to have met with him. I highly suggest your follow him on Twitter or read his blog, as he frequently provides interesting links related to social media and new technology.

Excuses, Excuses…

Hey, turns out I have a blog. Who’d have thought.

I really must apologize for my not updating. I’m sure i could come up with plenty of excuses, but where would that get us? So instead, I’m going to give a brief update as to what is going on.

First and foremost: Happy New Year! 2009 was an interesting and hectic year. We saw history made when Barrack Obama was inaugurated as the first African American president. We had quite a few notable celebrities pass away this year, which got more news time then most issues.
It was also a very good year for social media. Since I joined Twitter, I saw many of my friends from school joining. I’ve also seen many people joining, Facebook. What this has taught me: Social Networking is here to stay.
It was also an exciting year for technology. This year we saw Microsoft announce Project Natal, release Windows 7, Windows Server 2008 R2, Visual Studio 2010 Beta 1 and 2, and the Zune HD. I was very excited to see these technologies released, and I can safely say I’ve gotten to play with all except Project Natal.
But 2009 had it’s low points. Most notable: the economy. It’s been a hard couple of years, and there is more to come. But we will get through it.
Now, seeing as it is 2010, I should make a New Years resolution, no? Well, I think I can make two very simple resolutions: Blog more, and write more code. Like I said, I could create multiple excuses as to why I have not done either of these lately, but where would that get me? So instead, I’m just going to work harder at it.

In closing, I’d like to give shout-outs to people online.

  • LittleKuriboh: You are awesome. You created a YouTube sensation, and have always brought a smile to my face. I wish you the best in the new year, and once again congratulate you on your Open Web Award.
  • Kelson Thomas: You always spark interesting conversation, be it on Twitter, in class, or over IM. You’ve made 2009 a hoot and a half.
  • Eric Hawtin: You’ve been just as entertaining as Kelson. You always keep Twitter alive and provide good (well not always) input to our conversations, and I thank you for that.
  • Microsoft: We saw this coming. They had a great year in releases, and I look forward to seeing what they will do in 2010.
  • Alfred Thompson: You always have interesting resources for students, and you always write interesting blog posts. You’ve had me scratching my chin more then once. I thank you, and wish you the best in 2010

I could go on for hours, listing the people who made 2009 a good year, but I won’t, as that would take up a few pages.

In closing, I’d like to thank everyone for an awesome year, and I am excited to work with you all in 2010. You’ve all been great, and I am keeping my head high for the next 365 days.

Thanks for reading,
Patrick Godwin

Older Posts »