c# - 使用 delta 的单体游戏精灵运动不平滑
问题描述
我是monogame和c#的新手
我正在更新精灵 X 以进行移动,但是它不流畅,所以我尝试使用 delta 游戏时间,但它似乎并没有更好,毫无疑问,我犯了一个菜鸟错误,但无法怀疑,任何帮助都会非常大赞赏,谢谢
我的更新如下
var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
vecPlayerPostion.X -= 1 * delta;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
vecPlayerPostion.X += 1 * delta;
}
和画是
spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
如果需要,我的整个代码如下
游戏1.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace SpaceShip
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
PlayerShip PlayerShipUser1;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1920; // set this value to the desired width of your window
graphics.PreferredBackBufferHeight = 1080; // set this value to the desired height of your window
graphics.IsFullScreen = true;
graphics.ApplyChanges();
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
PlayerShipUser1 = new PlayerShip();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
PlayerShipUser1.texPlayerTexture = Content.Load<Texture2D>("player");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// TODO: Add your update logic here
PlayerShipUser1.Update(gameTime);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
PlayerShipUser1.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
playershipclass.cs
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;
using Microsoft.Xna.Framework.Audio;
namespace SpaceShip
{
class PlayerShip
{
public Texture2D texPlayerTexture;
private Vector2 vecPlayerPostion;
public PlayerShip()
{
vecPlayerPostion.X = 800;
vecPlayerPostion.Y = 900;
}
public void Update(GameTime gameTime)
{
var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
vecPlayerPostion.X -= 1 * delta;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
vecPlayerPostion.X += 1 * delta;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
}
}
}
解决方案
您的代码的唯一问题是移动速度太快。在 60 fps 时,gameTime.ElapsedGameTime.TotalMilliseconds
是 16。这会导致每步跳跃 16 个像素(或每秒 1000 个像素),这会破坏视觉连续性。
我通常将对象的速度限制为每步 4-8 个像素。要实现这一点,请将 delta 乘数从 更改1
为0.25f
或0.5f
。
// Set speed to 4 pixels per step
vecPlayerPostion.X -= 0.25f * delta;
如果您需要速度保持不变(快速),您可以在同一步骤中绘制中间帧并降低 alpha。这种技术会导致运动模糊,但会减少跳跃的外观。
更新playershipclass.cs
如下:
public Texture2D texPlayerTexture;
private Vector2 vecPlayerPostion;
// Store the previous position
private Vector2 vecPlayerLastPostion;
public PlayerShip()
// ...
public void Update(GameTime gameTime)
{
// Store the value
vecPlayerLastPostion = vecPlayerPostion;
var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
// ...
public void Draw(SpriteBatch spriteBatch)
{
// Draw the texture halfway between the previous position and current at .4f alpha
spriteBatch.Draw(texPlayerTexture, (vecPlayerPostion + vecPlayerLastPostion) / 2, null, new Color (1,1,1,0.4f), 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);
}
第三种解决方案是不限制帧速率:
在 game1 的构造函数中:
// ...
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1920; // set this value to the desired width of your window
graphics.PreferredBackBufferHeight = 1080; // set this value to the desired height of your window
graphics.IsFullScreen = true;
graphics.ApplyChanges();
// Remove 60 fps target
IsFixedTimeStep = false;
// don't wait on vsync(will limit to 60 fps)
graphics.SynchronizeWithVerticalRetrace = false;
Content.RootDirectory = "Content";
}
推荐阅读
- laravel - 运行 docker-compose exec app php artisan migrate 时连接被拒绝
- scala - 在 Scala 中仅使用代数持续流式传输
- ocaml - 如何在 Ocaml 中为一组布尔变量生成一组值
- python - 用于投票系统和跟随功能的 Django 模型
- java - 用另一个字符串替换字符串中的字符?
- android - Android app with react-native works on emulator but doesn't on physical device
- azure-active-directory - 如何使用 Azure AD 进行身份验证时排除特定 URL?
- java - Swing/awt gcd 计算器
- python - 在曲面图中截断半个圆环
- ajax - Controller does not recieve data from jsp page