上一篇博文介绍了一下Kinect的简单姿势识别,详细内容请转到http://yacare.iteye.com/blog/1950112,鄙人之前还写过一个俄罗斯方块的程序,具体请看http://yacare.iteye.com/blog/1949398,本博文主要将这两者结合起来,用Kinect姿势来控制游戏。仅作为一个抛砖引玉的案例,不知道公安局是否备案(哈哈,开个玩笑)。来说明去我们利用Kinect开发使用的只是其9牛1毛,杯水车薪的功能。
至于纯代码键盘控制的俄罗斯方块游戏请阅读C#实现俄罗斯方块,在此就不展开说明。此博文是基于二维仿真版的俄罗斯方块制作。界面与之前的可能有点不一样,但用Kinect姿势控制游戏的部分是基本相同的。
首先,我们要自定义一个姿势库,来存放自定义的姿势。
定义该姿势为方块旋转,左右肩膀,手腕,手掌都为水平方向,阈值角度为8度
定义该姿势为方块左移,左边肩膀,手腕,手掌都为竖直向上方向,阈值角度为10度 ;右边肩膀,手腕,手掌都为竖直向下方向, 阈值角度为30度。
定义该姿势为方块右移,右边肩膀,手腕,手掌都为竖直向上方向,阈值角度为10度 , 左边肩膀,手腕,手掌都为竖直向下方向, 阈值角度为30度。
定义该姿势为方块加速下落,左右边肩手腕,手掌都为水平方向,阈值角度为8度 , 左右肩膀,手腕与竖直竖直向下方向成25度(多次测试,手自然交叉接近这个角度), 阈值角度为8度。
////// 自定义姿势库 /// private void PopulatePoseLibrary() { this.poseLibrary = new Pose[4]; //Pose 1 - 伸开双臂 Arms Extended this.poseLibrary[0] = new Pose(); this.poseLibrary[0].Title = "伸开双臂(Arms Extended)"; this.poseLibrary[0].Angles = new PoseAngle[4]; this.poseLibrary[0].Angles[0] = new PoseAngle(JointType.ShoulderLeft, JointType.ElbowLeft, 180, 8); this.poseLibrary[0].Angles[1] = new PoseAngle(JointType.ElbowLeft, JointType.WristLeft, 180, 8); this.poseLibrary[0].Angles[2] = new PoseAngle(JointType.ShoulderRight, JointType.ElbowRight, 0, 8); this.poseLibrary[0].Angles[3] = new PoseAngle(JointType.ElbowRight, JointType.WristRight, 0, 8); //Pose 2 - 把双手交叉 Both Hands Cross this.poseLibrary[1] = new Pose(); this.poseLibrary[1].Title = "把手交叉(Hands Cross)"; this.poseLibrary[1].Angles = new PoseAngle[4]; this.poseLibrary[1].Angles[0] = new PoseAngle(JointType.ShoulderLeft, JointType.ElbowLeft, 245, 8); this.poseLibrary[1].Angles[1] = new PoseAngle(JointType.ElbowLeft, JointType.WristLeft, 0, 8); this.poseLibrary[1].Angles[2] = new PoseAngle(JointType.ShoulderRight, JointType.ElbowRight, 285, 8); this.poseLibrary[1].Angles[3] = new PoseAngle(JointType.ElbowRight, JointType.WristRight, 180, 8); //Pose 3 - 举起左手 Left Up and Right Down this.poseLibrary[2] = new Pose(); this.poseLibrary[2].Title = "(举起左手)Left Up and Right Down"; this.poseLibrary[2].Angles = new PoseAngle[4]; this.poseLibrary[2].Angles[0] = new PoseAngle(JointType.ShoulderLeft, JointType.ElbowLeft, 95, 10); this.poseLibrary[2].Angles[1] = new PoseAngle(JointType.ElbowLeft, JointType.WristLeft, 95, 10); this.poseLibrary[2].Angles[2] = new PoseAngle(JointType.ShoulderRight, JointType.ElbowRight, 270, 30); this.poseLibrary[2].Angles[3] = new PoseAngle(JointType.ElbowRight, JointType.WristRight, 270, 30); //Pose 4 - 举起右手 Right Up and Left Down this.poseLibrary[3] = new Pose(); this.poseLibrary[3].Title = "(举起右手)Right Up and Left Down"; this.poseLibrary[3].Angles = new PoseAngle[4]; this.poseLibrary[3].Angles[0] = new PoseAngle(JointType.ShoulderLeft, JointType.ElbowLeft, 270, 30); this.poseLibrary[3].Angles[1] = new PoseAngle(JointType.ElbowLeft, JointType.WristLeft, 270, 30); this.poseLibrary[3].Angles[2] = new PoseAngle(JointType.ShoulderRight, JointType.ElbowRight, 85, 10); this.poseLibrary[3].Angles[3] = new PoseAngle(JointType.ElbowRight, JointType.WristRight, 85, 10); }
处理姿势的事件,控制方块的运动
////// 处理玩家姿势的方法 /// /// private void ProcessPosePerforming(Skeleton skeleton) { bool ret; Graphics grMain = screenPanel.CreateGraphics(); int value = 0; if (IsPose(skeleton, this.poseLibrary[0]))// up { Console.WriteLine("双手上举垂直姿势符合条件"); value = UP; } else if (IsPose(skeleton, this.poseLibrary[1])) { Console.WriteLine("双手交叉姿势符合条件");// fall down value = DOWN; } else if (IsPose(skeleton, this.poseLibrary[2])) { Console.WriteLine("伸直左手姿势符合条件");// left value = LEFT; } else if (IsPose(skeleton, this.poseLibrary[3])) { Console.WriteLine("伸直右手姿势符合条件"); // right value = RIGHT; } switch (value) { case UP: // up ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_ROATE); break; case LEFT: // left ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_LEFT); break; case RIGHT: // right ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_RIGHT); break; case DOWN: // fall down ret = mainBody.MoveShape(grMain, Body.MOVE_TYPE.MOVE_FALL); break; default: ret = false; break; } if (ret && value == DOWN) { DisposeShapeDown(); } }