移动鼠标的方向和速率

Moving mouse in direction and rate

我正在尝试创建一个简单的鼠标模拟器,该模拟器由操纵杆的右摇杆控制。我试图让鼠标沿着操纵杆指向的方向移动,压力值的平滑梯度决定了速度,但我在尝试这样做时遇到了一些障碍。

首先是如何将角度准确地转换为准确的 X 和 Y 值。我找不到正确实现角度的方法。就我而言,对角线的移动速度可能比红雀移动得快得多。 我在想我需要像 Math.Cos(angle) 这样的 X 值,和 Math.Sin(angle) 这样的 Y 值来增加鼠标,但我想不出一种方法来设置它向上。

其次,鼠标的流畅移动,这可能是两者中更重要的一个。由于 SetPosition() 函数仅适用于整数,因此像素随时间移动的速率似乎非常有限。我的代码非常基础,只注册 1-10 的整数值。这不仅会产生较小的 'jumps' 加速度,还会限制对角线运动。 目标是每秒 10 个像素,程序 运行 为 100hz,每个周期输出 0.1 个像素的移动。

我想我可以跟踪 X 和 Y 值的像素 'decimals' 并在它们构建为整数时将它们添加到轴上,但我想有一个更有效的方法,并且仍然不会激怒 SetPosition() 函数。

我觉得 Vector2 对象应该可以完成此操作,但我不知道角度如何适合。

示例代码:

        //Poll Gamepad and Mouse.  Update all variables.
    public void updateData(){

        padOne = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None);
        mouse = Mouse.GetState();

        currentStickRX = padOne.ThumbSticks.Right.X;
        currentStickRY = padOne.ThumbSticks.Right.Y;
        currentMouseX = mouse.X;
        currentMouseY = mouse.Y;
        angle = Math.Atan2(currentStickRY, currentStickRX);

        vectorX = (int)( currentStickRX*10 );
        vectorY = (int)( -currentStickRY*10 );
        mouseMoveVector.X = vectorX;
        mouseMoveVector.Y = vectorY;


        magnitude = Math.Sqrt(  Math.Pow( (currentStickRX - 0), 2 ) + Math.Pow( (currentStickRY - 0), 2 )  );
        if (magnitude > 1){
            magnitude = 1;
        }

        //Get values not in deadzone range and re-scale them from 0-1
        if(magnitude >= deadZone){
            activeRange = (magnitude - deadZone)/(1 - deadZone);
        }

        Console.WriteLine();  //Test Code
    }

    //Move mouse in in direction at specific rate.
    public void moveMouse(){
        if (magnitude > deadZone){
            Mouse.SetPosition( (currentMouseX + vectorX), (currentMouseY + vectorY));
        }


        previousStickRX = currentStickRX;
        previousStickRY = currentStickRY;
        previousActiveRange = activeRange;
    }

注意:我使用的是所有 xna 框架。

无论如何,如果我对这些事情的解释不正确,我们深表歉意。我一直没能为此找到好的资源,我搜索的矢量示例仅以整数增量从点 A 移动到 B。

非常感谢对此任何部分的任何帮助。

我自己还没有尝试过,但从我的角度来看,你应该在阅读它们之后标准化 pad 轴,这样对角线的移动速度就会与基数相同。对于第二部分,我会在浮动变量(例如 Vector2)中跟踪鼠标,并在设置鼠标位置时进行转换(可能更好地舍入)。

public void Start()
{
   mousePosV2 = Mouse.GetState().Position.ToVector2();
}

public void Update(float dt)
{
   Vector2 stickMovement = padOne.ThumbSticks.Right;
   stickMovement.Normalize();
   mousePosV2 += stickMovement*dt*desiredMouseSpeed; 

   /// clamp here values of mousePosV2 according to Screen Size
   /// ...

   Point roundedPos = new Point(Math.Round(mousePosV2.X), Math.Round(mousePosV2.Y));
   Mouse.SetPosition(roundedPos.X, roundedPos.Y);
}