Using orientation to calculate position on Windows Phone 7

Posted by Lavinski on Game Development See other posts from Game Development or by Lavinski
Published on 2012-01-04T01:59:09Z Indexed on 2012/03/18 18:23 UTC
Read the original article Hit count: 345

Filed under:
|
|

I'm using the motion API and I'm trying to figure out a control scheme for the game I'm currently developing.

What I'm trying to achive is for a orienation of the device to correlate directly to a position. Such that tilting the phone forward and to the left represents the top left position and back to the right would be the bottom right position.

Photos to make it clearer (the red dot would be the calculated position).

Tilt left top
Forward and Left

Tilt right bottom
Back and Right

Now for the tricky bit. I also have to make sure that the values take into account left landscape and right landscape device orientations (portrait is the default so no calculations would be needed for it).

Has anyone done anything like this?

Notes: I've tried using the yaw, pitch, roll and Quaternion readings.

Sample:

// Get device facing vector 
public static Vector3 GetState()
{
    lock (lockable)
    {
        var down = Vector3.Forward;
        var direction = Vector3.Transform(down, state);

        switch (Orientation)
        {
            case Orientation.LandscapeLeft:
                return Vector3.TransformNormal(direction, Matrix.CreateRotationZ(-rightAngle));
            case Orientation.LandscapeRight:
                return Vector3.TransformNormal(direction, Matrix.CreateRotationZ(rightAngle));
        }

        return direction;
    }
}

© Game Development or respective owner

Related posts about XNA

Related posts about windows-phone-7