如何从一直漂浮到屏幕上的小行星中射出三角形

本文关键字:小行星 三角形 屏幕 一直 漂浮 | 更新日期: 2023-09-27 18:35:54

我目前加载了一个小行星纹理作为我正在编写的游戏的"测试玩家"。我想弄清楚该怎么做的是让一个三角形从小行星的中心射击,然后继续前进,直到它击中屏幕顶部。在我的情况下(正如您将从我发布的代码中看到的那样),三角形将显示,但它要么是一条长线,要么只是一个三角形,它与小行星移动的位置保持在同一位置(当我停止按空格键时消失),或者它根本不会出现。我尝试了许多不同的方法,但我可以在这里使用公式。

我要做的就是用 C# 为我的决赛编写一个太空入侵者克隆。我知道如何很好地编码,我的公式只需要工作即可。

到目前为止,这就是我所拥有的:

主逻辑代码

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1, 1);
    mAsteroid.Draw(mSpriteBatch);
    if (mIsFired)
    {
        mPositions.Add(mAsteroid.LastPosition);
        mRay.Fire(mPositions);
        mIsFired = false;
        mRay.Bullets.Clear();
        mPositions.Clear();
    }
    base.Draw(gameTime);
}

绘制代码

public void Draw()
{
    VertexPositionColor[] vertices = new VertexPositionColor[3];
    int stopDrawing = mGraphicsDevice.Viewport.Width / mGraphicsDevice.Viewport.Height;
    for (int i = 0; i < mRayPos.Length(); ++i)
    {
        vertices[0].Position = new Vector3(mRayPos.X, mRayPos.Y + 5f, 10);
        vertices[0].Color = Color.Blue;
        vertices[1].Position = new Vector3(mRayPos.X - 5f, mRayPos.Y - 5f, 10);
        vertices[1].Color = Color.White;
        vertices[2].Position = new Vector3(mRayPos.X + 5f, mRayPos.Y - 5f, 10);
        vertices[2].Color = Color.Red;
        mShader.CurrentTechnique.Passes[0].Apply();
        mGraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleStrip, vertices, 0, 1);
        mRayPos += new Vector2(0, 1f);
        mGraphicsDevice.ReferenceStencil = 1;
    }
}

如何从一直漂浮到屏幕上的小行星中射出三角形

这不是你应该在世界空间中操纵模型位置的方式,因为你在每个绘制帧中创建一个新的顶点数组,你会发现当你画多个三角形时,它的表现非常糟糕。

在 LoadContent 方法中仅声明一次三角形的顶点和索引列表。

VertexBuffer triangleVertexBuffer;
IndexBuffer triangleIndexBuffer;
protected override void LoadContent()
{
    // Setup a basic effect to draw the triangles with.
    mEffect = new BasicEffect(GraphicsDevice);
    // setup the triangle vertext buffer and load up it's content.
    triangleVertexBuffer = new VertexBuffer(GraphicsDevice, typeof(VertexPositionColor), 3, BufferUsage.WriteOnly);
    triangleVertexBuffer.SetData<VertexPositionColor>(new VertexPositionColor[] 
    {
        new VertexPositionColor (new Vector3 (0f, -1f, 0.0f), Color.Blue),  // Top Point
        new VertexPositionColor (new Vector3 (-1f, 1f, 0.0f), Color.White), // Bottom Left
        new VertexPositionColor (new Vector3 (1f, 1f, 0.0f), Color.Red),    // Bottom Right
    });
    // setup an index buffer to join the dots!
    triangleIndexBuffer = new IndexBuffer(GraphicsDevice, IndexElementSize.SixteenBits, 3, BufferUsage.WriteOnly);
    triangleIndexBuffer.SetData<short>(new short[]
    { 
        0, 
        1, 
        2, 
    });
}

在此之后,假设您的效果考虑了世界变换(基本效果确实如此),您可以使用该参数来移动三角形。

 protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            for (int i = 0; i < mRayPos.Length ; i++)
            {
                // This is the line that moves the triangle along your ray.
                mEffect.World = Matrix.CreateTranslation(new Vector3(mRayPos[i].X, mRayPos[i].Y, mRayPos[i].Z));
                mEffect.Techniques[0].Passes[0].Apply();
                // These lines tell the graphics card that you want to draw your triangle.
                GraphicsDevice.SetVertexBuffer(triangleVertexBuffer);
                GraphicsDevice.Indices = triangleIndexBuffer;
                GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);
            }
            base.Draw(gameTime);
        }

如果使用此方法,则使用 Matrix.CreateRotate 和 Matrix.CreateScale 旋转或缩放 trangle 将变得非常简单。