XNA - 如何判断拇指棒是否被“抽动”在某个方向

发布于 2024-10-18 15:15:23 字数 180 浏览 2 评论 0原文

API(3 或 4)中是否有任何内容可以告诉我操纵杆是否朝一个方向移动,就像在菜单中相当于按 DPad 上的某个方向一样? Buttons 枚举中似乎有一些 Thumbstick* 成员,但我找不到关于它们的合适文档。

只是想在我自己动手之前确保我没有遗漏一些明显的东西。谢谢!

Is there anything in the API (3 or 4) to tell me if the stick moved in one direction, as in a menu where it's equivalent to hitting a direction on the DPad? There appear to be some Thumbstick* members in the Buttons enum, but I can't find decent documentation on them.

Just want to make sure I'm not missing something obvious before I go and roll my own. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的呼吸 2024-10-25 15:15:23

没有 XNA 方法可以告诉您拇指操纵杆是否在该帧中“抽动”。

最简单的方法是存储旧的操纵杆状态。如果状态原本为零且现在非零,则它已被抽搐。

加法:

而不是检查状态是否为零且现在是否非零。您可以使用问题中提到的枚举中的拇指操纵杆按钮来确定操纵杆是否已“抽动”。在这种情况下,您将操纵杆视为 DPad,并且必须独立测试每个方向。下面的代码展示了这个方法:

private void ProcessUserInput()
{
    GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickUp) && gamePadState.IsButtonDown(Buttons.LeftThumbstickUp))
    {
        PrevMenuItem();
    }

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickDown) && gamePadState.IsButtonDown(Buttons.LeftThumbstickDown))
    {
        NextMenuItem();
    }

    m_lastGamePadState = gamePadState;
}

There is no XNA method to tell you if a thumbstick was "twitched" this frame.

The easiest method is to store the old thumbstick state. If the state was zero and is now non-zero, it has been twitched.

Addition:

Instead of checking if the state was zero and is now non-zero. You can use the thumbstick buttons from the enumeration you mention in your question to determine if the stick has been "twitched". In this case you are treating the stick like a DPad and have to test each direction independently. The following code shows this method:

private void ProcessUserInput()
{
    GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickUp) && gamePadState.IsButtonDown(Buttons.LeftThumbstickUp))
    {
        PrevMenuItem();
    }

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickDown) && gamePadState.IsButtonDown(Buttons.LeftThumbstickDown))
    {
        NextMenuItem();
    }

    m_lastGamePadState = gamePadState;
}
蓦然回首 2024-10-25 15:15:23

Xbox 360 控制器上的摇杆可以像按钮一样“推入”,映射到 GamePadButtons.LeftStick 和 GamePadButtons.RightStick。这些显然不是您想要的。

以下是我用于检测任何方向上的“按下”的代码(其中padLeftPushActive存储在帧之间):

Vector2 padLeftVector = gamePadState.ThumbSticks.Left;
bool lastPadLeftPushActive = padLeftPushActive;
if(padLeftVector.Length() > 0.85f)
    padLeftPushActive = true;
else if(padLeftVector.Length() < 0.75f)
    padLeftPushActive = false;

if(!lastPadLeftPushActive && padLeftPushActive)
{
    DoSomething(Vector2.Normalize(padLeftVector));
}

修改此代码以使其检测应该相当简单只需按菜单所需的特定方向即可。

The thumbsticks on an Xbox 360 controller can be pushed "in" like buttons, which map to GamePadButtons.LeftStick and GamePadButtons.RightStick. These are obviously not what you want.

Here is the code that I use for detecting "presses" in any direction (where padLeftPushActive is stored between frames):

Vector2 padLeftVector = gamePadState.ThumbSticks.Left;
bool lastPadLeftPushActive = padLeftPushActive;
if(padLeftVector.Length() > 0.85f)
    padLeftPushActive = true;
else if(padLeftVector.Length() < 0.75f)
    padLeftPushActive = false;

if(!lastPadLeftPushActive && padLeftPushActive)
{
    DoSomething(Vector2.Normalize(padLeftVector));
}

It should be fairly simple to modify this so that it detects just presses in the particular directions necessary for your menu.

-黛色若梦 2024-10-25 15:15:23

GamePadState.Thumbsticks您在寻找什么财产?

Is the GamePadState.Thumbsticks property what you're looking for?

知足的幸福 2024-10-25 15:15:23

这是我想出的解决方案,以防它对任何人有用:

    enum Stick {
        Left,
        Right,
    }
    GamePadState oldState;
    GamePadState newState;

    /// <summary>
    /// Checks if a thumbstick was quickly tapped in a certain direction. 
    /// This is useful for navigating menus and other situations where
    /// we treat a thumbstick as a D-Pad.
    /// </summary>
    /// <param name="which">Which stick to check: left or right</param>
    /// <param name="direction">A vector in the direction to check. 
    /// The length, which should be between 0.0 and 1.0, determines 
    /// the threshold.</param>
    /// <returns>True if a twitch was detected</returns>
    public bool WasStickTwitched(Stick which, Vector2 direction)
    {
        if (direction.X == 0 && direction.Y == 0)
            return false;

        Vector2 sold, snew;
        if (which == Stick.Left)
        {
            sold = oldState.ThumbSticks.Left;
            snew = newState.ThumbSticks.Left;
        }
        else
        {
            sold = oldState.ThumbSticks.Right;
            snew = newState.ThumbSticks.Right;
        }

        Vector2 twitch = snew;
        bool x = (direction.X == 0 || twitch.X / direction.X > 1);
        bool y = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool tnew = x && y;

        twitch = sold;
        x      = (direction.X == 0 || twitch.X / direction.X > 1);
        y      = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool told = x && y;

        return tnew && !told;
    }

Here's the solution I came up with, in case it's useful for anyone:

    enum Stick {
        Left,
        Right,
    }
    GamePadState oldState;
    GamePadState newState;

    /// <summary>
    /// Checks if a thumbstick was quickly tapped in a certain direction. 
    /// This is useful for navigating menus and other situations where
    /// we treat a thumbstick as a D-Pad.
    /// </summary>
    /// <param name="which">Which stick to check: left or right</param>
    /// <param name="direction">A vector in the direction to check. 
    /// The length, which should be between 0.0 and 1.0, determines 
    /// the threshold.</param>
    /// <returns>True if a twitch was detected</returns>
    public bool WasStickTwitched(Stick which, Vector2 direction)
    {
        if (direction.X == 0 && direction.Y == 0)
            return false;

        Vector2 sold, snew;
        if (which == Stick.Left)
        {
            sold = oldState.ThumbSticks.Left;
            snew = newState.ThumbSticks.Left;
        }
        else
        {
            sold = oldState.ThumbSticks.Right;
            snew = newState.ThumbSticks.Right;
        }

        Vector2 twitch = snew;
        bool x = (direction.X == 0 || twitch.X / direction.X > 1);
        bool y = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool tnew = x && y;

        twitch = sold;
        x      = (direction.X == 0 || twitch.X / direction.X > 1);
        y      = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool told = x && y;

        return tnew && !told;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文