Leap Motion 探究 【第二篇】

版权声明:本文为PeterShen原创文章,未经PeterShen允许不得转载。 https://blog.csdn.net/prius0304/article/details/51279803

       昨晚捣鼓完Leap的数据输出,一看时间还早,写完前一篇博文,后手就开始写这个东西。团队是做飞行模拟设备的,我就直接拿微软模拟飞行来做个例子(模拟器接口部分不放开,主要看怎么整合Leap应用到外围)。测试视频链接:http://player.youku.com/player.php/sid/XMTU1MTkwNzU4MA==/v.swf

public partial class Form1 : Form
    {
        FListener MyListener = new FListener();
        Controller MyController = new Controller();

        short PitchFS;//模拟器中的Pitch值
        short RollFS;//模拟器中的Roll值
        short YawFS;//模拟器中的Yaw值
        public Form1()
        {
            InitializeComponent();
        }

        private void ConnectToLeap_Click(object sender, EventArgs e)
        {
            MyController.AddListener(MyListener);
            try
            {
                //FSX 通道预位
                DisConnectToLeap.Enabled = true;
                ConnectToLeap.Enabled = false;
                timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DisConnectToLeap_Click(object sender, EventArgs e)
        {
            MyController.RemoveListener(MyListener);
            MyController.Dispose();
            DisConnectToLeap.Enabled =false;
            ConnectToLeap.Enabled = true;
            timer1.Enabled = false;
            //FSX 通道关闭
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PitchFS = (short)(MyListener.PitchRaw / 90 * 16383);
            RollFS = (short)(MyListener.RollRaw / 90 * 16383);
            YawFS = (short)(MyListener.YawRaw / 90 * 16383);
            textPitch.Text = ((short)(MyListener.PitchRaw / 90 * 16383)).ToString();
            textRoll.Text = ((short)(MyListener.RollRaw / 90 * 16383)).ToString();
            textYaw.Text = ((short)(MyListener.YawRaw / 90 * 16383)).ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        }
    }
以上是Form类中的代码,与Leap提供的example几乎一致。后面看一下Listener的代码,我只取得了我需要的数据,剩下不需要的统统不要。

public class FListener : Listener
    {
        public float PitchRaw, RollRaw, YawRaw;
        public override void OnInit(Controller arg0)
        {
            Console.WriteLine("Initialized.");
        }

        public override void OnConnect(Controller arg0)
        {
            Console.WriteLine("Connected.");
            arg0.EnableGesture(Gesture.GestureType.TYPE_CIRCLE);
            arg0.EnableGesture(Gesture.GestureType.TYPE_KEY_TAP);
            arg0.EnableGesture(Gesture.GestureType.TYPE_SCREEN_TAP);
            arg0.EnableGesture(Gesture.GestureType.TYPE_SWIPE);
        }

        public override void OnDisconnect(Controller arg0)
        {
            Console.WriteLine("Disconnected.");
        }

        public override void OnExit(Controller arg0)
        {
            Console.WriteLine("Exited");
        }

        public override void OnFrame(Controller arg0)
        {
            Frame frame = arg0.Frame();
            foreach (Hand hand in frame.Hands)
            {
                Vector normal = hand.PalmNormal;
                Vector direction = hand.Direction;
                Console.WriteLine("Hand pitch: " + direction.Pitch * 180.0f / (float)Math.PI + "   "
                            + "roll: " + normal.Roll * 180.0f / (float)Math.PI + "   "
                            + "yaw: " + direction.Yaw * 180.0f / (float)Math.PI + "   ");
                PitchRaw = direction.Pitch * 180.0f / (float)Math.PI;
                RollRaw = normal.Roll * 180.0f / (float)Math.PI;
                YawRaw = direction.Yaw * 180.0f / (float)Math.PI;
            }
        }
    }

这篇写的比较简单,没有创新什么东西,只是将第一篇的东西做了一个“东西”出来,后面将测试几个手势。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页