我想在XNA中繪製一個正方形,並根據用戶輸入簡單地用鍵盤移動它。不幸的是,我甚至沒有能夠讓廣場顯示 - 我很積極,我錯過了一些東西,但我無法弄清楚它到底是什麼。我打算創建一個我可以在我的遊戲中使用的複雜的原始形狀層次結構。 Shape
類的主要組件被初始化,並且派生類(在這種情況下爲Rect
)將這些組件用於進一步使用,並對其進行配置。如何在XNA中繪製和移動正方形?
我在這裏做錯了什麼?
代碼
Shape.cs
public abstract class Shape
{
public Vector3 Center;
public Color[] Colors;
public bool SetColorsOnUpdate;
public virtual Rectangle BoundingBox { get; set; }
public float Radius { get { return mRadius; } }
protected VertexBuffer mVertexBuf;
protected IndexBuffer mIndexBuf;
protected float mRadius;
protected BasicEffect mShader;
public Shape(Vector3 center, Color[] colors, int numVertices, float radius)
{
Colors = colors;
Center = center;
mVertexBuf = new VertexBuffer(Graphics.MainDevice, typeof(VertexPositionColor), numVertices, BufferUsage.WriteOnly);
mIndexBuf = new IndexBuffer(Graphics.MainDevice, IndexElementSize.SixteenBits, numVertices, BufferUsage.WriteOnly);
SetColorsOnUpdate = true;
mShader = new BasicEffect(Graphics.MainDevice);
mRadius = radius;
}
public abstract void Update();
public abstract void Draw();
}
Rect.cs
public class Rect : Shape
{
public override Rectangle BoundingBox
{
get
{
int x = (int)Center.X, y = (int)Center.Y;
int diameter = (int)mRadius * 2;
return new Rectangle(
x, y,
x + diameter,
y + diameter
);
}
}
const float TestRectZCoordinate = 0;
const int NumVertices = 4;
public Rect(Vector3 center, Color[] colors, float radius)
: base(center, colors, NumVertices, radius)
{
if (colors.Length < NumVertices)
throw new IndexOutOfRangeException(string.Format("Color array passed to Rect constructor MUST have an element index size of 4. Current length passed is {0}", colors.Length));
mShader.VertexColorEnabled = true;
mVertexBuf.SetData<VertexPositionColor>(
new VertexPositionColor[]
{
new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[0]),
new VertexPositionColor(new Vector3(Center.X + mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[1]),
new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y - mRadius, TestRectZCoordinate), Colors[2]),
new VertexPositionColor(new Vector3(Center.X - mRadius, Center.Y + mRadius, TestRectZCoordinate), Colors[3])
}
);
mIndexBuf.SetData<short>(new short[] { 0, 1, 2, 3 });
}
public override void Update()
{
//TODO
}
public override void Draw()
{
mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
mShader.CurrentTechnique.Passes[0].Apply();
Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
Graphics.MainDevice.Indices = mIndexBuf;
Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, NumVertices, 0, 1);
}
}
更新
這是最新的Rect.Draw()實現。
public override void Draw()
{
Viewport viewport = Graphics.MainDevice.Viewport;
mShader.World = Matrix.CreateWorld(Center, Vector3.Forward, Vector3.Up);
mShader.View = Matrix.CreateLookAt(new Vector3(viewport.Width/2, viewport.Height/2, -5f), Center, Vector3.Up);
mShader.CurrentTechnique.Passes[0].Apply();
Graphics.MainDevice.SetVertexBuffer(mVertexBuf);
Graphics.MainDevice.Indices = mIndexBuf;
Graphics.MainDevice.DrawIndexedPrimitives(PrimitiveType.LineStrip, 0, 0, NumVertices, 0, 2);
}
您是否正確設置了相機,以便可以看到形狀? – PaulG
@保羅,我相信如此。我不太確定,如果我做得很好,或者我需要這樣做。這是一款2D遊戲 - 因此我需要*相機嗎? – zeboidlund
是的,這是必要的。請張貼您的投影矩陣/世界矩陣。 – Ani