这个东东我前年也整过,那时在一个同学的unity3d打飞机程序上进行的修改,除了控制飞机还可以射出子弹来~~总体来说LeapMotion用手指代替键盘输入,可以吸引大家玩起来~程序的关键在于如何计算yaw、row、pitch的数值,但倘若想加入子弹射击等手势,这里的手势方式可能就不能使用了,需要重新设计。这位博主太厉害,一天一篇我转载都来不及。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。
昨晚捣鼓完Leap的数据输出,一看时间还早,写完前一篇博文,后手就开始写这个东西。团队是做飞行模拟设备的,我就直接拿微软模拟飞行来做个例子(模拟器接口部分不放开,主要看怎么整合Leap应用到外围)。视频:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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的代码,我只取得了我需要的数据,剩下不需要的统统不要。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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; } } } |
这篇写的比较简单,没有创新什么东西,只是将第一篇的东西做了一个“东西”出来,后面将测试几个手势。