C# 在XNA中旋转播放器
我一直在四处寻找如何正确地做到这一点的教程,而谷歌一直空着。也许这是一个很简单的问题,但我不知道该怎么办 到目前为止,我发现的代码如下所示:C# 在XNA中旋转播放器,c#,rotation,xna,game-physics,C#,Rotation,Xna,Game Physics,我一直在四处寻找如何正确地做到这一点的教程,而谷歌一直空着。也许这是一个很简单的问题,但我不知道该怎么办 到目前为止,我发现的代码如下所示: if (currentKeyboardState.IsKeyDown(Keys.A)) { RotationAngle += 0.01f; float circle = MathHelper.Pi * 2; RotationAngle = Rotatio
if (currentKeyboardState.IsKeyDown(Keys.A))
{
RotationAngle += 0.01f;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;
}
只是,这没什么用。它来自于。这是我能找到的最好的解决方案
我想做的就是让玩家旋转他们的飞船,朝另一个方向射击。
我正在做一个和小行星非常相似的游戏,但我现在只能朝一个方向射击
任何帮助都将不胜感激:)
编辑:这是我当前的播放器。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 += .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);
至于你想要拍摄的角度,你需要一些几何图形(像这样的,没有测试过……但如果我没记错的话):
此外,使用调试器确保旋转角度正确更改。在将精灵绘制到屏幕时,是否使用该旋转角度?我相当肯定
SpriteBatch
有重载,可能需要也可能不需要旋转。你说“它没什么用”是什么意思?球员是旋转的吗?比如说,你现在需要知道如何从某个角度拍摄吗?我在《SpriteBatch》中使用了它,是的。我的意思是它根本不旋转精灵,它现在什么也不做。我只是注意到了,并把它放在了代码中。。但由于某种原因,我改变了激光原点的位置,将旋转角度改为30f,使飞船旋转得非常好。我不知道是什么阻止代码旋转飞船…嗯。。。尝试在主更新方法中更改旋转角度,这样每次调用时它都会更新(我的意思是不需要按下按钮),然后告诉我发生了什么。仍然没有发生任何事情。我正在编辑的代码在一个单独的类中有什么区别吗?我在Player.cs中运行了这段代码,但它应该正确地链接到Game1.cs文件。在Game1.cs中的Draw()
方法中,它调用player.Draw(spriteBatch)代码>绘制播放器。您是否记得调用player.Update()?
Vector2 velocity = new Vector2(speed*Math.Cos(RotationAngle), speed*Math.Sin(RotationAngle));