我想在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