0

我有一个坦克车身的 2D 图像(自上而下),可以在屏幕上左右移动。在顶部,还有第二张坦克炮塔的图像。随着用户鼠标沿 Y 轴移动,该炮塔可以在屏幕边缘旋转。

当用户按下“Enter”时,会出现一个项目符号,并以炮塔的角度在屏幕上移动。然而,虽然角度很好,但子弹的位置似乎变化很大。有时,它会位于它应该位于的位置(在大炮的中心),但是,当您移动鼠标时,它似乎会被抵消。


编辑:出于某种奇怪的原因,我的照片似乎没有出现 - 所以这里有一个直接链接:http: //img824.imageshack.us/img824/7093/khte.png

编辑代码:

Tank Fire
     if (keyBoardState.IsKeyDown(Keys.Enter))
                {
                    shell.Initialize(rotation, new Vector2(location.X + 25, location.Y - 15));
                    shell.makeAlive();
                }

(它是用坦克的位置(+25,-25)初始化的,所以它出现在炮塔的末端。把这个设置在坦克的位置(shell.Initialize(rotation,location);)似乎没有什么区别到偏移量。)

子弹/外壳:

public void Update(GameTime gameTime)
    {
        if (alive)
        {
            movement.X -= speed * (float)Math.Cos(rotation);
            movement.Y -= speed * (float)Math.Sin(rotation);

            location.X += (int)movement.X;
            location.Y += (int)movement.Y;
        }
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        if (alive)
        {
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
            spriteBatch.Draw(texture, location, null, Color.White, rotation - MathHelper.PiOver2, new Vector2(texture.Width / 2, texture.Height / 2), 1.0f, SpriteEffects.None, 0f);
            spriteBatch.End();
        }
    }
4

3 回答 3

1

如果你想让你的子弹围绕你的坦克旋转,那么我个人将子弹画在坦克顶部,你会做与你发射子弹(但幅度更大)后所做的完全相同的事情以取代它,

但是如果你想使用 , 的origin参数SpriteBatch.Draw,我会想象它看起来像这样:

给定以下数据点:

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

// and this is what you'd pass into your sprite-batch.draw method
originToDraw = (tank.position + tank.origin)-(this.position + this.origin)

而且您还必须将子弹的位置初始化为适当的起点因为我实际上不知道旋转来自哪里,所以我不能确定,但​​是如果是坦克和鼠标之间的角度,那么我会想象它会是以下

this.startingPosition = tank.position + tank.origin - this.origin
于 2013-06-27T19:59:52.800 回答
0

如果是,我认为这shell是一个Bullet实例,您的问题是Bullet课堂上的起源。它应该在LoadTextures方法中设置这个值

public void LoadTextures(Texture2D texture)
{
    this.texture = texture;
    this.origin = new Vector2(texture.Width, texture.Height);
}

还有一点额外的Bullet.Draw方法:

spriteBatch.Draw(texture, location, null, Color.White, rotation - 1.5f, origin, 1.0f, SpriteEffects.None, 0f);

当您想将某物旋转 90 度时,将 1.5f 更改MathHelper.PiOver2为更准确

于 2013-06-27T19:51:12.990 回答
0

你在这里有一个讨厌的舍入问题。

您以浮点计算速度,但切换到子弹位置的整数。问题是,在平均情况下,即使任何给定的项目符号的错误每次都会向上舍入或向下舍入。

让我们举一个极端的例子:子弹偏离垂直轴 25 度发射,每个更新周期移动两个像素。奇怪的是,子弹直接沿着轴线飞了下来。

或者更极端的情况。速度为每周期 1/3 像素。子弹静止不动。

于 2013-06-27T23:05:40.767 回答