1

我正在制作一个玩家控制坦克的简单游戏。炮塔的旋转将由鼠标移动控制。目前的代码如下所示:

if (Game.MouseState.Y < yMovementBorder)
   PossessedTurretPitchValue += dist;

if (Game.MouseState.Y > yMovementBorder)
   PossessedTurretPitchValue -= dist;

if (Game.MouseState.X < xMovementBorder)
   PossessedTurretYaw += rotationDist / 6;

if (Game.MouseState.X > xMovementBorder)
   PossessedTurretYaw -= rotationDist / 6;

xMovementBorder 和 yMovementBorder 是代表游戏屏幕中点的值。问题是鼠标的任何移动都会导致炮塔转动直到其最大俯仰/偏航角。我怎样才能使它能够读取鼠标的移动及其幅度(即:鼠标的轻微移动只会导致炮塔的轻微俯仰/偏航移动)?

4

2 回答 2

0

我认为你想要做的是根据鼠标离中心的距离来设置限制。

喜欢:

if (Game.MouseState.Y < yMovementBorder)
    if (PossessedTurretPitchValue < yMovementBorder*someFactor)
        PossessedTurretPitchValue += dist;

其他三种情况也是如此。

如果不设置限制,任何偏离中心都会导致炮塔移动。它将一直在移动,并最终在某个地方运行。如果我理解正确,这就是您的代码现在所做的。

于 2013-08-15T19:14:17.137 回答
0

在您的更新方法中,您应该存储对先前鼠标状态和当前鼠标状态的引用。然后使用这两个变量来计算自上次更新以来鼠标移动了多少。需要进行一些调整才能使其正确,但请尝试以下操作:

//define your private variables
private MouseState prevMouseState = null;
private MouseState currMouseState = Game.MouseState;

public void Update(GameTime gt)
{
    prevMouseState = currMouseState;
    currMouseState = Game.MouseState;

    //calculate how much the mouse has moved since the last update
    var dX = currMouseState.X - prevMouseState.X;
    var dY = currMouseState.Y - prevMouseState.Y;

    //do your rotating depending on the values of dX and dY
}
于 2013-08-15T19:13:05.433 回答