如何使用XNA Visual Studio中的拇指操纵杆编码360度移动

时间:2013-03-01 15:54:05

标签: xna rotation controls

好的,所以我准备制作一个游戏,它都围绕着玩家控制的圆圈。这个游戏是一个自上而下的视图。枪朝北或“向上”。我创建了一个spritesheet,其中枪指向每个直角之间的4个方向。所以枪指向^和指向>还有4个其他职位。这一直都是如此。

我想要做的就是对游戏进行编码,这样如果你将右手拇指旋转一圈,它就可以使整个360周围的枪动起来。但是我不知道该如何做到这一点。到目前为止,我只使用了基本方向。即使我操作了拇指操纵杆,我也不确定如何将每个点作为拇指操纵杆的位置。

非常感谢帮助!

1 个答案:

答案 0 :(得分:0)

//Gets us the rotation from the stick: (taking this from memory, som might be reverse order etc)
float radians = Math.Atan2(GamePadState.ThumbSticks.Left.Y, GamePadState.ThumbSticks.Left.X);

//Top half: 0 = stick to the right to Pi = stick to the left.
//Bottom half: 0 = stick to the right to -Pi = stick to the left.

//I'll just do this for 4 directions, as I have limited time, and the concept should be obvious:

if ((radians > Math.Pi * -0.25f) && (radians < Math.Pi * 0.25f))
    //Direction is Right
else if ((radians >= Math.Pi * 0.25f) && (radians < Math.Pi * 0.75f))
    //Direction is Up
else if ((radians >= Math.Pi * 0.75f) || (radians <= Math.Pi * -0.75f))
    //Direction is Left
else if ((radians <= Math.Pi * -0.25f) && (radians > Math.Pi * -0.75f))
    //Direction is Down

要获得更多角度,您只需执行更多操作即可。您可能会发现使用Thumbstick更容易:

Vector2 stick = GamePadState.ThumbSticks.Left;
stick.Normalize();

if (stick.X >= 0.5f) (NE, E, SE)
{
    if (stick.Y >= 0.5f)
        //Direction is NE
    else if (stick.Y <= -0.5f)
        //Direction is SE
    else
        //Direction is E
}
else if (stick.X <= -0.5f) (NW, W, SW)
{
    if (stick.Y >= 0.5f)
        //Direction is NW
    else if (stick.Y <= -0.5f)
        //Direction is SW
    else
        //Direction is W
}
else if (stick.Y >= 0.5f)
    //Direction is N
else if (stick.Y <= -0.5f)
    //Direction is S
else
    //Direction is nothing. should not happen

编辑;要在拇指操纵杆的方向上射击子弹,只需使用缩略图标准化*速度作为子弹的速度。