C# 如何从一颗一直漂浮在屏幕上的小行星上拍摄三角形?

C# 如何从一颗一直漂浮在屏幕上的小行星上拍摄三角形?,c#,vector,xna,drawing,C#,Vector,Xna,Drawing,我目前有一个小行星纹理作为我正在编写的游戏的“测试玩家”加载。我想弄清楚的是如何从小行星的中心拍摄一个三角形,然后继续拍摄直到它到达屏幕顶部。在我的例子中(正如你将从我发布的代码中看到的),三角形将显示,但它要么是一条长线,要么只是一个三角形,它将停留在小行星移动的相同位置(当我停止按空格键时消失),或者根本不会出现。我尝试过许多不同的方法,但我可以在这里使用一个公式 我所要做的就是为我的C#期末考试写一个太空入侵者克隆。我知道如何很好地编写代码,我的公式只需要工作就可以了 到目前为止,我的情况

我目前有一个小行星纹理作为我正在编写的游戏的“测试玩家”加载。我想弄清楚的是如何从小行星的中心拍摄一个三角形,然后继续拍摄直到它到达屏幕顶部。在我的例子中(正如你将从我发布的代码中看到的),三角形将显示,但它要么是一条长线,要么只是一个三角形,它将停留在小行星移动的相同位置(当我停止按空格键时消失),或者根本不会出现。我尝试过许多不同的方法,但我可以在这里使用一个公式

我所要做的就是为我的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;
    }
}
绘制代码

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;
    }
}
public void Draw()
{
VertexPositionColor[]顶点=新的VertexPositionColor[3];
int stopDrawing=mGraphicsDevice.Viewport.Width/mGraphicsDevice.Viewport.Height;
对于(int i=0;i
这并不是您在世界空间中操作模型位置的方式,因为您在每个绘图帧中创建一个新的顶点数组,所以在绘制多个三角形时,您会发现它的性能非常差

在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, 
    });
}
VertexBuffer三角缓冲区;
IndexBuffer三角形IndexBuffer;
受保护的覆盖void LoadContent()
{
//设置用于绘制三角形的基本效果。
mEffect=新的基本效果(图形设备);
//设置三角形顶点文本缓冲区并加载其内容。
TriangalLeverTexBuffer=新的VertexBuffer(GraphicsDevice,typeof(VertexPositionColor),3,BufferUsage.WriteOnly);
triangleVertexBuffer.SetData(新的VertexPositionColor[]
{
新VertexPositionColor(新矢量3(0f,-1f,0.0f),Color.Blue),//顶点
新的VertexPositionColor(新矢量3(-1f,1f,0.0f),Color.White),//左下角
新的VertexPositionColor(新矢量3(1f、1f、0.0f),颜色为红色),//右下角
});
//设置索引缓冲区以连接点!
triangleIndexBuffer=新的IndexBuffer(GraphicsDevice,IndexElementSize.SixteenBits,3,BufferUsage.WriteOnly);
triangleIndexBuffer.SetData(新短[]
{ 
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);
        }
protected override void Draw(游戏时间游戏时间)
{
图形设备。清晰(颜色:矢车菊蓝);
for(int i=0;i

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

您能为我们将其简化为一个简单的示例吗?所以我们可以在我们的电脑上复制它(+1只是为了问题的标题…哈哈)我打开这个只是为了标题,好的:)你可能想改变它,但是,给一个真正可能帮助你解决问题的人!!!非常感谢你。我还是一个图形编程新手,所以我非常感激这一点。