Using gestures in MonoGame (and XNA)
I was trying to use this bit of code yesterday, but it didn’t work:
protected override void Update(GameTime gameTime)
{
while (TouchPanel.IsGestureAvailable)
{
var gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.FreeDrag)
{
_cue.Rotate(gesture.Delta, GraphicsDevice.Viewport.Bounds);
}
}
base.Update(gameTime);
}
Why? Because I forgot to enable gestures! This line was all that was missing:
TouchPanel.EnabledGestures = GestureType.FreeDrag;
EnabledGestures is a flag property so you can enable multiple types of gestures by using bitwise or, like, for example:
TouchPanel.EnabledGestures = GestureType.FreeDrag | GestureType.Tap;
In my rotate method I simply translate the delta into something more useful, which in my cases are degrees:
public void Rotate(Vector2 delta, Rectangle viewPortBounds)
{
var degreesAroundYAxis = (delta.X / viewPortBounds.Width) * 360;
var degreesAroundXAxis = (delta.Y / viewPortBounds.Height) * 90;
Rotation += new Vector3(MathHelper.ToRadians(degreesAroundXAxis), MathHelper.ToRadians(degreesAroundYAxis), 0.0f);
if (MathHelper.ToDegrees(Rotation.X) < 5.0f)
{
Rotation = new Vector3(MathHelper.ToRadians(5.0f), Rotation.Y, Rotation.Z);
}
}