为什么增量时间不起作用? (MonoGame, C#)
Why is delta time not working? (MonoGame, C#)
我正在尝试让我的游戏使用增量时间,使其独立于 FPS。我试了一下,并排比较了它们:
但是 144fps 的播放器仍然运行得更快。我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace _2D_Game_Test.GameStates
{
public class TestState : GameState
{
Vector2 velocity;
Vector2 position;
double change = 0;
KeyboardState keyboardState;
public override void Initialize()
{
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
public override void Update(GameTime gameTime)
{
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
change = 0;
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
double delta = gameTime.ElapsedGameTime.TotalSeconds;
keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A))
{
if (change > 0)
{
change -= (10 * delta);
}
else if (change > -5f)
{
if (change - (5 * delta) < -5f)
{
change = -5f;
}
else
{
change -= (5 * delta);
}
}
}
else if (keyboardState.IsKeyDown(Keys.D))
{
if (change < 0)
{
change += (10 * delta);
}
else if (change < 5f)
{
if (change + (5 * delta) > 5f)
{
change = 5f;
}
else
{
change += (5 * delta);
}
}
}
if (keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.D))
{
if (change > 0)
{
if ((change -= (2 * delta)) < 0)
{
change = 0;
}
else
{
change -= (2 * delta);
}
}
else if (change < 0)
{
if ((change += (2 * delta)) > 0)
{
change = 0;
}
else
{
change += (2 * delta);
}
}
}
velocity.X = (float)change;
position += velocity;
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Game.Bob, position, Color.White);
spriteBatch.DrawString(Game.Debug, "A and D up: " + (keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.D)), new Vector2(10, 10), Color.Black);
spriteBatch.DrawString(Game.Debug, "A down: " + (keyboardState.IsKeyDown(Keys.A)), new Vector2(10, 30), Color.Black);
spriteBatch.DrawString(Game.Debug, "D down: " + (keyboardState.IsKeyDown(Keys.D)), new Vector2(10, 50), Color.Black);
spriteBatch.DrawString(Game.Debug, "Velocity modifier: " + velocity, new Vector2(10, 70), Color.Black);
spriteBatch.DrawString(Game.Debug, "Current velocity: " + velocity.X, new Vector2(10, 90), Color.Black);
}
}
}
我做错了什么?
P.S:我很肯定这不仅仅是我的眼睛在玩弄我。我可以看到播放器在 144fps 时比在 60fps 时运行得更快。
这里的问题不是增量时间本身。
当您计算速度变化时,您完美地应用了增量时间。
当您实际将速度应用于播放器本身时,问题就来了。假设您处于最大速度 (5)。由于在将速度应用于位置时没有考虑增量时间,因此与 60fps 相比,144fps 时您将更频繁地应用速度。这就是为什么角色似乎走得更快的原因。
您可能想要执行以下操作:
position += velocity * delta;
这应该可以解决您的问题。
我正在尝试让我的游戏使用增量时间,使其独立于 FPS。我试了一下,并排比较了它们:
但是 144fps 的播放器仍然运行得更快。我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace _2D_Game_Test.GameStates
{
public class TestState : GameState
{
Vector2 velocity;
Vector2 position;
double change = 0;
KeyboardState keyboardState;
public override void Initialize()
{
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
public override void Update(GameTime gameTime)
{
if (Mouse.GetState().LeftButton == ButtonState.Pressed)
{
change = 0;
velocity = new Vector2(0, 0);
position = new Vector2((1280 / 2) - (Game.Bob.Width / 2), (720 / 2) - (Game.Bob.Height / 2));
}
double delta = gameTime.ElapsedGameTime.TotalSeconds;
keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A))
{
if (change > 0)
{
change -= (10 * delta);
}
else if (change > -5f)
{
if (change - (5 * delta) < -5f)
{
change = -5f;
}
else
{
change -= (5 * delta);
}
}
}
else if (keyboardState.IsKeyDown(Keys.D))
{
if (change < 0)
{
change += (10 * delta);
}
else if (change < 5f)
{
if (change + (5 * delta) > 5f)
{
change = 5f;
}
else
{
change += (5 * delta);
}
}
}
if (keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.D))
{
if (change > 0)
{
if ((change -= (2 * delta)) < 0)
{
change = 0;
}
else
{
change -= (2 * delta);
}
}
else if (change < 0)
{
if ((change += (2 * delta)) > 0)
{
change = 0;
}
else
{
change += (2 * delta);
}
}
}
velocity.X = (float)change;
position += velocity;
}
public override void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Game.Bob, position, Color.White);
spriteBatch.DrawString(Game.Debug, "A and D up: " + (keyboardState.IsKeyUp(Keys.A) && keyboardState.IsKeyUp(Keys.D)), new Vector2(10, 10), Color.Black);
spriteBatch.DrawString(Game.Debug, "A down: " + (keyboardState.IsKeyDown(Keys.A)), new Vector2(10, 30), Color.Black);
spriteBatch.DrawString(Game.Debug, "D down: " + (keyboardState.IsKeyDown(Keys.D)), new Vector2(10, 50), Color.Black);
spriteBatch.DrawString(Game.Debug, "Velocity modifier: " + velocity, new Vector2(10, 70), Color.Black);
spriteBatch.DrawString(Game.Debug, "Current velocity: " + velocity.X, new Vector2(10, 90), Color.Black);
}
}
}
我做错了什么?
P.S:我很肯定这不仅仅是我的眼睛在玩弄我。我可以看到播放器在 144fps 时比在 60fps 时运行得更快。
这里的问题不是增量时间本身。
当您计算速度变化时,您完美地应用了增量时间。
当您实际将速度应用于播放器本身时,问题就来了。假设您处于最大速度 (5)。由于在将速度应用于位置时没有考虑增量时间,因此与 60fps 相比,144fps 时您将更频繁地应用速度。这就是为什么角色似乎走得更快的原因。
您可能想要执行以下操作:
position += velocity * delta;
这应该可以解决您的问题。