OPENNI实践-体感方向盘玩赛车游戏(2)

这一篇是关于如何通过捕捉到的双手进行方向盘的线性模拟的过程。其实模型很简单。

也就是通过左右手在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);

}

油门是两只手握住方向盘,刹车是两只手向后,增加油门是两只手向前推。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值