gpt4 book ai didi

c# - Xna 4 Model Rotation with inertia 在变量更新时无法按预期工作

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:04 24 4
gpt4 key购买 nike

我正在尝试使用键盘旋转具有惯性阻尼的模型。该代码在固定时间步长下运行良好,但在可变更新频率下表现不同。

更新方法:

protected override void Update(GameTime gameTime)
{
base.Update(gameTime);

float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
const float rotationPerSecond = 0.25f;
const float decayPerSecond = 0.5f;

KeyboardState kb = Keyboard.GetState();

if (kb.IsKeyDown(Keys.Left))
{
m_velocity += rotationPerSecond * elapsed;
}
else if (kb.IsKeyDown(Keys.Right))
{
m_velocity -= rotationPerSecond * elapsed;
}
else
{
if (Math.Abs(m_velocity) < 0.01f)
{
m_velocity = 0;
}
else if (m_velocity > 0)
{
m_velocity -= decayPerSecond * elapsed;
}
else if (m_velocity < 0)
{
m_velocity += decayPerSecond * elapsed;
}
}

m_rotationAngle += m_velocity;
}

绘制方法:

Matrix rot = Matrix.CreateWorld(new Vector3(0, 0, 0), Vector3.Forward, Vector3.Up) * 
Matrix.CreateRotationY(MathHelper.ToRadians(m_rotationAngle));

我正在改变时间步长

IsFixedTimeStep = true;
graphics.SynchronizeWithVerticalRetrace = true;

IsFixedTimeStep = false;
graphics.SynchronizeWithVerticalRetrace = false;

旋转速度和阻尼随着更新频率的不同而完全不同。知道我做错了什么吗?

最佳答案

您在 m_rotationAngle 计算中忘记了 (float)gameTime.ElapsedGameTime.TotalSeconds:

m_rotationAngle += m_velocity * elapsed;

然后增加你的速度值:

const float rotationPerSecond = 250.0f;
const float decayPerSecond = 500.0f;

它适用于:

//IsFixedTimeStep = true/false;
//graphics.SynchronizeWithVerticalRetrace = true/false;

关于c# - Xna 4 Model Rotation with inertia 在变量更新时无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22236470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com