Skip to content

November 1, 2010

Moving on from Mouse Events to Multi-touch

Well, let’s try another post.  First time I’m writing a public development journal, so we’ll see how it goes.

I really just want to share knowledge I’ve gained over hard hours of research and trial and error so others can learn from my own experiences.

So, there’s some built-in manipulation support in Silverlight meant for something like zooming in or rotating an image.  Which is great if you have a simple app viewing some single object.  There’s also some handy gesture libraries emerging and some built-in support via XNA.  I found out about that from this other post here.

It’s really quite cleaver and according to the current marketplace guidelines should pass certification as its not one of the forbidden XNA references (when using Silverlight):

4.2.5 The application must not call any APIs in the Microsoft.Xna.Framework.Game assembly or the Microsoft.Xna.Framework.Graphics assembly when using any methods from the System.Windows.Controls namespace.

In either case, what I really wanted was to be able to track the specific multiple points of the player and not just some gesture they may be trying to input.

So, using the referenced post as a base, I started to explore and found the methods/properties which better suited my needs.  So, I just polled the events in my game loop:

foreach (TouchLocation tl in TouchPanel.GetState())
{
    if (tl.State == TouchLocationState.Pressed)
    {
        touchedObjs[tl.Id] = tl.Position;
    }
    else if (touchedObjs.ContainsKey(tl.Id))
    {
        if (tl.State == TouchLocationState.Moved)
        {
            ...
        }
        else if (tl.State == TouchLocationState.Released)
        {
            ...
            touchedObjs.Remove(tl.Id);
        }
    }
}

The TouchLocation object then has the following properties: Id, State (Pressed, Moved, Released), and Position.  Using this information you can record a specific start position in a dictionary based on the Id when the location is Pressed.  Then when you see that same Id get Moved or Released you can calculate any movement info you need to.  On the release you can also clean-up your dictionary.

So, voila!  You suddenly can easily track multiple touch points in your game.

Comments are closed.