这一篇是关于如何通过捕捉到的双手进行方向盘的线性模拟的过程。其实模型很简单。
也就是通过左右手在x轴的夹角及左右手相对高度去判断的。
void transControl()
{
XnVector3D xnVector_Point_Right;
XnVector3D xnVector_Point_Left;
XnVector3D xnVector_Point_Hand;
xnVector_Point_Hand.X=xnVector_Point_Right.X-xnVector_Point_Left.X;
xnVector_Point_Hand.Y=xnVector_Point_Right.Y-xnVector_Point_Left.Y;
xnVector_Point_Hand.Z=xnVector_Point_Right.Z-xnVector_Point_Left.Z;
float angle_hand=atan(xnVector_Point_Hand.Y/xnVector_Point_Hand.X)*180/3.1415;
if (xnVector_Point_Hand.Y>=0.0)
{
cout<<"右手抬起";
if (angle_hand>=0.0)
{
nMove_LetfJoystic_X=Get_Move_Length(angle_hand,32767);
nMove_LetfJoystic_X=-nMove_LetfJoystic_X;
}
else
{
nMove_LetfJoystic_X=-32767;
}
}
else
{
cout<<"左手抬起";
if (angle_hand<0.0)
{
nMove_LetfJoystic_X=Get_Move_Length(angle_hand,32767);
}
else
{
nMove_LetfJoystic_X=32767
}
}
}
int Get_Move_Length(float angle_float,int length)
{
int nMoveLength=0;
int angle=int(angle_float);
angle=abs(angle);
if (angle>90)
{
angle=angle%90;
}
if (angle<10)
{
nMoveLength=1000;
}
else if ((angle>=10)&&(angle<=50))
{
nMoveLength=length/90*angle;
}
else if ((angle>50)&&(angle<80))
{
nMoveLength=length/80*angle;
}
else if ((angle>=80))
{
nMoveLength=length;
}
return nMoveLength;
}
然后是刹车和加速的设计:
XnFloat Get_arm_length(XnPoint3D leftshoulder,XnPoint3D lefthand)
{
XnFloat length_x=abs(lefthand.X-leftshoulder.X);
XnFloat length_y=abs(lefthand.Y-leftshoulder.Y);
XnFloat length_z=abs(lefthand.Z-leftshoulder.Z);
return sqrt(length_x*length_x+length_y*length_y+length_z*length_z);
}
void clockup()
{
XnVector3D xnShoulder_Point_Right;
XnVector3D xnShoulder_Point_Left;
XnFloat ArmLength_Left=Get_arm_length(xnShoulder_Point_Left,xnVector_Point_Left);
XnFloat ArmLength_Right=Get_arm_length(xnShoulder_Point_Right,xnVector_Point_Right);
if (armlength<ArmLength_Left||armlength<ArmLength_Right)
{
armlength=ArmLength_Left>ArmLength_Right?ArmLength_Left:ArmLength_Right;
}
XnFloat ArmLength_RealTime=(ArmLength_Left+ArmLength_Right)/2;
if ((ArmLength_RealTime>=(armlength/5))&&(ArmLength_RealTime<=(armlength*4/5)))
{
nMove_RightJoystic_X=-(426/armlength)*ArmLength_RealTime+341;
}
else if(ArmLength_RealTime<(armlength/5))
{
nMove_RightJoystic_X=255;
}
else if (ArmLength_RealTime>(armlength*4/5))
{
nMove_RightJoystic_X=0;
}
SetEvent(hEvent_send);
}
油门是两只手握住方向盘,刹车是两只手向后,增加油门是两只手向前推。