好的,所以我正准备制作一款游戏,它都围绕着玩家控制的一个圆圈。这个游戏是一个自上而下的视图。枪指向北方或“向上”。我创建了一个精灵表,其中枪指向每个直角之间的 4 个方向。所以在枪指向 ^ 和它指向 > 之间还有 4 个其他位置。这是真的。
我想要做的是对游戏进行编码,这样如果你将右拇指旋转一圈,它就会使枪在整个 360 度范围内旋转。但我不知道该怎么做,因为我只工作过到目前为止的基本方向。即使我让拇指杆轴运行,我也不确定如何将每个点作为拇指杆的位置。
帮助将不胜感激!
//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
To get more angles, you just do more of this. you may find it easier to just use the 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
Edit; To shoot bullets in the direction of the thumbstick, just use the thumbstick normalized * speed as velocity for the bullets.