# XNA 3D cube rotation problem

I am new to game programming and still trying to clear up my basics. I am stuck with a problem and unable to resolve on my own knowledge. I am trying to rotate a 3d cube in MonoGame (Similar to XNA) Windows Phone 8 application. The rotation works fine but it has a problem. Following is the scenario

• Y axis (up/down) rotation works fine. I am able to rotate it as many times as possible and everything works fine.

• But let's say that the cube was in its default position. I rotate
my cube 180 degree on y-axis. Now when i rotate my cube on X axis. It rotates in opposite direction. Forexample if i try to move it towards positive x-axis it moves in negative x-axis.

• If i again move the cube 180 degree in y-axis, the x-axis
rotations works fine again.

public void Update(GameTime gameTime) { _userInputHandler.HandleTouchEvents();

Matrix unrotatedView = Matrix.CreateLookAt(
cameraPosition, ModelPosition, Vector3.Up);

viewMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(Angle.Y)) *     // right/left rotation
unrotatedView;

CameraCurPos = Cube.MatrixVectorMultiply(cameraPosition, viewMatrix);

projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, Game.GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);


}

Here is the Input handle function

public void HandleTouchEvents()
{
TouchCollection tc = TouchPanel.GetState();

if (tc.Count > 0)
{
TouchLocation current = tc;
TouchLocation prev;
if (current.TryGetPreviousLocation(out prev))
{
Vector3 newAngle = _gameEngine.Angle;
//newAngle.X += MathHelper.ToRadians((current.Position.X - prev.Position.X) * TURN_SPEED); // pitch
//newAngle.Y += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * TURN_SPEED); // yaw
newAngle.X += MathHelper.ToRadians((current.Position.Y - prev.Position.Y) * TURN_SPEED); // pitch
newAngle.Y += MathHelper.ToRadians((current.Position.X - prev.Position.X) * TURN_SPEED); // yaw

_gameEngine.Angle = newAngle;

}
}
}


I know the problem is simple but i am unable to resolve it. Thanks in advance.