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 an 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.

Here are some photos that should make my intent clearer. The red dot would be the calculated position.

Tilt left top
Tilted forward and left, upper left selected.

Tilt right bottom
Tilted back and right, bottom right selected.

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

I've tried using the yaw, pitch, roll and Quaternion readings. Here's a sample of my current attempt:

// 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;
  • 2
    \$\begingroup\$ I would recommend you ask this on SO instead. It isn't directly related to gaming, and you'll probably get better answers (esp. regarding the questions on the Windows Phone API). I'll vote to transfer if you think this is a good idea. \$\endgroup\$ – DMan Jan 4 '12 at 2:14
  • \$\begingroup\$ @DMan yeah maybe \$\endgroup\$ – Daniel Little Jan 4 '12 at 2:31
  • \$\begingroup\$ Try multiplying accelerometer values by 1 or -1 depending on screen orientation. it worked for me. \$\endgroup\$ – Vector3d Aug 13 '12 at 9:50
  • \$\begingroup\$ Cross-posted, with an accepted answer (for posterity): stackoverflow.com/questions/8721561/… \$\endgroup\$ – user1430 Dec 2 '13 at 16:45

I generally use something like this to rotate a point around another point:

Vector3 rotatedVector = Vector3.Transform(vectorToRotate, Matrix.CreateFromAxisAngle(rotationAxis, radiansToRotate));

Your approach seems to be similar, but I'm not sure what your state value is. I would recommend trying Matrix.CreateFromAxisAngle(PhoneFaceAxis, radiansToRotate);, where PhoneFaceAxis is the axis represented by the normal vector of the screen of your phone (which may be Z, judging by your code sample', and radiansToRotate would be some form of a 90 or 180 degree rotation depending on the phone's orientation.

I've also put information for issues like this at XNAWiki.com. It's a fairly small wiki, but it has some useful information about 3D Vector Math when using XNA.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.