29. October 2012 03:06
by nKast
0 Comments
One of the coolest things you can do on mobile phones are Augmented reality apps.
Combining the camera with the Motion API of windows phone is all you need. So, here's what you need to make AR on XNA.
First, Initialize the Motion API
Here I also set the update interval to 60FPS
motion =
new
Motion();
motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(16.666);
motion.Start();
The important part is to align your XNA camera to the physical camera. For that we need the Attitude property from the motion API. In order to use it we must first apply some transformations. The last line is needed for landscape apps. Remove it If your app works in portrait mode.
AttitudeReading attitude = motion.CurrentValue.Attitude;
Matrix orientation = Matrix.Identity;
orientation = Matrix.CreateRotationX(MathHelper.PiOver2);
orientation *= attitude.RotationMatrix;
orientation *= Matrix.CreateRotationZ(MathHelper.PiOver2);
What you have now is a View matrix. You can use it to draw your stuff with Model.Draw(...) or assign it to Effect.View.
Sometimes this isn't enough. What I wanted was the actual orientation of the camera/phone as a 3D vector.
My first thought was to use Vector3.Transform(...) and transform a Vector3.Forward using the orientation matrix. That didn't work. Then I tried to get the orientation.Forward & orientation.Up but that didn't work either. Finally i wrote a small method that extracts the two vectors from a view matrix.
As you can see, you can use the results in order to create your own view matrix or use it in your Camera class.
Vector3 cameraForward = Vector3.Zero;
Vector3 cameraUp = Vector3.Zero;
GetViewOrientation(
ref
orientation,
out
cameraForward,
out
cameraUp);
view = Matrix.CreateLookAt(cameraPosition, cameraPosition + cameraForward, cameraUp);
And here is the method to extracts the Fordward and Up vectors
public
void
GetViewOrientation(
ref
Matrix view,
out
Vector3 forward,
out
Vector3 up)
{
forward =
new
Vector3(-view.M13, -view.M23, -view.M33);
up =
new
Vector3(view.M12, view.M22, view.M32);
}
MotionAPI XNA AR.zip (773.14 kb)