【问题标题】:Rotate the player in XNA在 XNA 中旋转播放器
【发布时间】:2013-04-18 13:13:09
【问题描述】:

我一直在寻找有关如何正确执行此操作的教程,但 Google 一直空无一物。也许这是一个非常简单的主题,但我不知道该怎么做。

到目前为止,我发现的代码如下所示:

        if (currentKeyboardState.IsKeyDown(Keys.A))
        {
            RotationAngle += 0.01f;
            float circle = MathHelper.Pi * 2;
            RotationAngle = RotationAngle % circle;
        }

只是,这并没有多大作用。它来自MSDN。这是我能找到的最好看的解决方案。

我想要做的就是让玩家旋转他们的船并朝另一个方向射击。 我正在制作一款与小行星非常相似的游戏,但目前我只能朝一个方向射击。

任何帮助将不胜感激:)

编辑:这是我当前的 Player.cs:

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GameTest
{
    class Player
    {
        public Texture2D PlayerTexture;
        public Vector2 Position;
        public bool Alive;
        public int Health;
        public Vector2 origin;
        public float RotationAngle;
        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;
        public int Width
        {
            get { return PlayerTexture.Width; }
        }
        public int Height
        {
            get { return PlayerTexture.Height; }
        }


        public void Initialize(Texture2D texture, Vector2 position)
        {
            PlayerTexture = texture;

            Position = position;

            Alive = true;

            Health = 10;

            origin.X = PlayerTexture.Width / 2;
            origin.Y = PlayerTexture.Height / 2;
        }

        public void Update(GameTime gameTime)
        {
            RotationAngle += 10f;

            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (currentKeyboardState.IsKeyDown(Keys.A))
            {

                //float circle = MathHelper.Pi * 2;
                //RotationAngle = RotationAngle % circle;
            }

            if (currentKeyboardState.IsKeyDown(Keys.D))
            {
                //rotation
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(PlayerTexture, Position, null, Color.White, RotationAngle, origin, 1f, SpriteEffects.None, 0f);
        }
    }
}

【问题讨论】:

  • 您在将精灵绘制到屏幕时是否使用了RotationAngle?我相当确定SpriteBatch 有可能需要或不需要轮换的重载。
  • “它没有多大作用”是什么意思?绘制的玩家是否旋转?例如,您现在是否需要研究如何以某个角度拍摄?
  • 我在SpriteBatch 中使用它,是的。我的意思是它根本不旋转精灵,它现在什么都不做。

标签: c# rotation xna game-physics


【解决方案1】:

我的猜测是您缺少“原点”或旋转中心:

RotationAngle += .5f;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;

Vector2 origin = new Vector2(texture.Width / 2, texure.Height / 2);

spriteBatch.Draw(texture, position, null, Color.White, RotationAngle, origin, 1.0f, SpriteEffects.None, 0f);

至于你想要拍摄的角度,你需要一些几何形状(类似这样的东西,没有测试过......但如果我没记错的话):

Vector2 velocity = new Vector2(speed*Math.Cos(RotationAngle), speed*Math.Sin(RotationAngle));

另外,使用调试器确保旋转角度正确变化。

【讨论】:

  • 我刚刚注意到并将其粘贴在代码中.. 但所做的只是由于某种原因改变了激光原点的位置
  • 我将RotationAngle 更改为仅 30f,它使船旋转得非常好。我不知道是什么阻止了代码旋转船..
  • 嗯...尝试在主更新方法中更改旋转角度,以便在每次调用时都会更新(我的意思是不需要按下按钮),然后告诉我会发生什么。
  • 什么也没有发生。我正在编辑的代码在一个单独的类中是否有任何区别?我在我的 Player.cs 中运行了这段代码,但它应该都正确链接到 Game1.cs 文件。在Game1.cs的Draw()方法中,调用player.Draw(spriteBatch);来绘制玩家。
  • 你记得调用 player.Update() 吗?
猜你喜欢
  • 2023-03-15
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 1970-01-01
相关资源
最近更新 更多