2012-05-31 29 views
3

我目前有一個小行星紋理加載爲我正在寫的遊戲的「測試玩家」。我試圖弄清楚如何做的是從小行星的中心拍攝一個三角形,並繼續前進,直到它碰到屏幕的頂部。在我的例子中發生了什麼(就像你從我發佈的代碼中看到的那樣),就是三角形會顯示,但是它可能是一條長線,或者它只是一個三角形,它停留在相同的位置當小行星在四處移動時(當我停止按下空格鍵時消失),或者根本不會出現。我嘗試了許多不同的方法,但我可以在這裏使用一個公式。如何從小行星上拍攝出一個三角形,這個小行星一直浮到屏幕上?

我想要做的就是在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; 
    } 
} 
+1

你可以將它剝離爲我們的裸骨頭例子嗎?所以我們可以在我們的個人電腦上覆制它(+1只是問題標題...笑) –

+0

我打開這只是爲了標題,好的一個:)你可能想改變它,但是,以吸引真正可以幫助的人你解決 – YavgenyP

回答

3

這倒不怎麼你應該要在世界空間中操縱模型的位置,並且因爲您要爲每個dr創建一個新的頂點數組aw框架,你會發現當你繪製超過幾個三角形時,它的表現會非常糟糕。

只在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.CreateRotation和Matrix.CreateScale您trangle。

+0

Gahh !!!非常感謝。我仍然是圖形編程的新手,所以我非常感謝這一點。 – zeboidlund