C# 如何使用角度进行拍摄?
坐着做一个2d射击游戏,我的玩家需要能够使用角度从左到右射击。我的问题是,我找不到一个办法使这项工作顺利进行 那么我如何计算以特定角度射出子弹的角度呢 我在player类中的代码C# 如何使用角度进行拍摄?,c#,xna,angle,projectile,C#,Xna,Angle,Projectile,坐着做一个2d射击游戏,我的玩家需要能够使用角度从左到右射击。我的问题是,我找不到一个办法使这项工作顺利进行 那么我如何计算以特定角度射出子弹的角度呢 我在player类中的代码 class Player { //Player public Rectangle playercollisionbox; public Texture2D Playertexture; public Vector2 Position = new Vector2(470, 850);
class Player
{
//Player
public Rectangle playercollisionbox;
public Texture2D Playertexture;
public Vector2 Position = new Vector2(470, 850);
//Bullet
public Texture2D bulletTexture;
//How fast you shoot
public float bulletDelay = 1;
public List<Bullet> bulletList;
//Angle bullet
public float spriterotation = 0;
public float speed = 1;
public Vector2 bulletposition;
//Health
public int Health;
public Vector2 healthbarposition = new Vector2(20, 40);
public Texture2D healthbartexture;
public Rectangle healthrectangle;
//Contructor
public Player()
{
bulletList = new List<Bullet>();
Health = 200;
}
public void LoadContent(ContentManager Content)
{
bulletTexture = Content.Load<Texture2D>(@"images/projectile2");
healthbartexture = Content.Load<Texture2D>(@"images/HealthBar");
}
public void Update(GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
playercollisionbox = new Rectangle((int)Position.X, (int)Position.Y, Playertexture.Width, Playertexture.Height);
//Player movement
if (keyboardState.IsKeyDown(Keys.Up))
{
Position.Y -= 7;
}
if (keyboardState.IsKeyDown(Keys.Left))
{
Position.X -= 7;
}
if (keyboardState.IsKeyDown(Keys.Down))
{
Position.Y += 7;
}
if (keyboardState.IsKeyDown(Keys.Right))
{
Position.X += 7;
}
//Player movement
healthrectangle = new Rectangle((int)healthbarposition.X, (int)healthbarposition.Y, Health, 25);
//Off-screen block
if (Position.X < 0)
{
Position.X = 0;
}
if (Position.Y < 0)
{
Position.Y = 0;
}
if (Position.X > 943)
{
Position.X = 943;
}
if (Position.Y > 904)
{
Position.Y = 904;
}
//Bullet
if (keyboardState.IsKeyDown(Keys.Space))
{
Shoot();
}
UpdateBullet();
}
public virtual void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Playertexture, Position, Color.White);
foreach (Bullet b in bulletList)
{
b.Draw(spriteBatch);
}
spriteBatch.Draw(healthbartexture, healthrectangle, Color.White);
}
//Shooting method
public void Shoot()
{
if (bulletDelay >= 0)
{
bulletDelay--;
}
if (bulletDelay <= 0)
{
//First Bullet
Bullet newBullet = new Bullet(bulletTexture);
newBullet.position = new Vector2(Position.X + 40 - newBullet.texture.Width / 2, Position.Y - 40);
newBullet.isVisible = true;
//Second Bullet
if (bulletDelay == 0)
{
bulletDelay = 20;
}
if (bulletList.Count() < 20)
{
bulletList.Add(newBullet);
}
}
}
//Updating bullet after shooting
public void UpdateBullet()
{
//speed on nullet
foreach (Bullet b in bulletList)
{
b.position.Y = b.position.Y - b.speed;
b.bulletcollisionbox = new Rectangle((int)b.position.X, (int)b.position.Y, b.texture.Width, b.texture.Height);
//outside screen removes it
if (b.position.Y <= 0)
{
b.isVisible = false;
}
}
for (int i = 0; i < bulletList.Count; i++)
{
if (!bulletList[i].isVisible)
{
bulletList.RemoveAt(i);
i--;
}
}
}
}
实际上,我有一个自制的小库,我在我所有的XNA项目中都使用它。助手函数之一是:
/// <summary>
/// Converts a given angle into a Vector2.
/// </summary>
/// <param name="angle">The angle (in radians).</param>
/// <param name="normalize">True to normalize the resultant vector, otherwise false.</param>
/// <returns>A vector representing the specified angle.</returns>
public static Vector2 Vector2FromAngle(double angle, bool normalize = true)
{
Vector2 vector = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
if (vector != Vector2.Zero && normalize)
vector.Normalize();
return vector;
}
//
///将给定角度转换为矢量2。
///
///角度(弧度)。
///True以规范化结果向量,否则为false。
///表示指定角度的向量。
公共静态向量2向量2Fromangle(双角度,布尔规格化=真)
{
vector2vector=新的Vector2((float)Math.Cos(angle),(float)Math.Sin(angle));
if(vector!=Vector2.Zero&&normalize)
vector.Normalize();
返回向量;
}
请随意使用它!它返回的向量就是子弹的轨迹
如果您不熟悉如何将度转换为弧度,
radians=degrees*pi/180
以及如何将角度转换回degrees=radians*180/pi
弧度中两个向量之间的角度
Math.Atan2(b.Y - a.Y,b.X - a.X);
子弹方向
Vector2 direction = playerPosition - enemyPosition;
direction.Normalize();
然后通过方向向量增加子弹位置Hi!谢谢你的回答。我不明白的是,我会替换“角度”而不是位置吗?不要真的找到放置角度的地方,让子弹找到那个位置。如果你能再描述一下我是如何做到这一点的,那就太好了。这里是使用角度吗?“newBullet.position=newvector2(position.X+40-newBullet.texture.Width/2,position.Y-40);”您还可以使用
MathHelper.ToRadians
进行转换,反之亦然position+=newNormalizedVector*速度
Vector2 direction = playerPosition - enemyPosition;
direction.Normalize();