在Game1.cs的代码顶部,我有:
private float angle = 0f;
private float angleRightLeft = 0f;
private float angleUpDown = 0f;
private bool rotationDirection;
private int terrainWidth = 4;
private int terrainHeight = 3;
private float[,] heightData;
在draw方法中,我有这段代码:
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
device.Clear(Color.Black);
RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
rs.FillMode = FillMode.WireFrame;
device.RasterizerState = rs;
if (rotationDirection == true)
{
worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left,
angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f,
0,
terrainHeight / 2.0f) *
Matrix.CreateRotationY(angleRightLeft);
}
else
{
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f,
0,
terrainHeight / 2.0f) *
Matrix.CreateRotationX(angleUpDown);
}
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);
effect.Parameters["xWorld"].SetValue(worldMatrix);
}
密钥处理输入法:
private void ProcessInput(float amount)
{
previousState = currentState;
currentState = Mouse.GetState();
Vector3 moveVector = new Vector3(0 , 0 , 0);
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
{
angleUpDown += 0.05f;
rotationDirection = false;
}
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
{
angleUpDown -= 0.05f;
rotationDirection = false;
}
if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
{
angleRightLeft += 0.05f;
rotationDirection = true;
}
if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
{
angleRightLeft -= 0.05f;
rotationDirection = true;
}
if (keyState.IsKeyDown(Keys.Q))
cameraPosition += new Vector3(0, 1, 0);
if (keyState.IsKeyDown(Keys.Z))
cameraPosition += new Vector3(0, -1, 0);
if (keyState.IsKeyDown(Keys.Escape))
{
this.graphics.PreferredBackBufferWidth = 800;
this.graphics.PreferredBackBufferHeight = 600;
this.graphics.IsFullScreen = false;
this.graphics.ApplyChanges();
}
if (this.graphics.PreferredBackBufferWidth < 1920)
{
/*
if (WasMouseLeftClick())
{
changeScreenNode(this.graphics, 1920, 1080, true);
}*/
if (WasDoubleClick())
{
changeScreenNode(this.graphics, 1920, 1080, true);
}
}
if (WasMouseLeftClick())
{
previousClick = DateTime.Now;
}
}
更新方法:
protected override void Update(GameTime gameTime)
{
fpsm.Update();
float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
ProcessInput(timeDifference);
modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
MathHelper.ToRadians(0.1f);
base.Update(gameTime);
}
问题在于,当我按下D按钮并向右旋转地形时如果停止按D并按下W以向上旋转地形,则地形首先移回原位。
我希望它会保持在我按下D或A或S或W后的位置,当我点击另一个键时,它将从那里继续并且不会先重置到其原始位置。
我想我需要在draw方法中改变一些东西。
当我点击一个键然后在另一个键上它会从它停止的位置旋转而不是首先返回/重置到其原始位置时,我该怎么做呢?
编辑**
这就是我在绘制方法中所做的:
if (rotationDirection == true)
{
worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);
oldPosition = worldMatrix;
}
else
{
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationX(angleUpDown);
oldPosition = worldMatrix;
}
oldPosition是Matrix类型。 然后在每个按下的键上的键处理方法中:worldMatrix = oldPosition;
if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
{
angleUpDown += 0.05f;
rotationDirection = false;
worldMatrix = oldPosition;
}
但它仍然不能正常运作。 我做错了什么?
答案 0 :(得分:2)
如果我正在阅读你的问题,你想在改变输入方向时保持先前的旋转。如果是这种情况,您可以通过替换它来实现此目的:
if (rotationDirection == true)
{
worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);
oldPosition = worldMatrix;
}
else
{
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationX(angleUpDown);
oldPosition = worldMatrix;
}
用这个
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationX(angleUpDown) * Matrix.CreateRotationY(angleLeftRight);
您现有的代码一次只使用一个轴旋转矩阵。如果您在下次更新/绘制时更改了方向,则会将前一个轴重置为零。上面更新的代码使用了两个值。除此之外你也可以使用Matrix.CreateFromYawPitchRoll(我可能在这里订购错误,所以请仔细检查):
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateFromYawPitchRoll(angleUpDown, angleLeftRight, 0);