I'm working on an A* algorithm, and I'd like to be able to draw lines between the nodes in the path-finding graph, especially those leading to the exit. The environment I'm working in is 3D. I just couldn't figure out why my code wasn't displaying the lines, so I simplified it so that It'd render just one line. Now I can see a line, but its in screen space, instead of world space. Is there an easy way to draw lines in world coordinates in XNA?
Here's the code:
_lineVtxList = new VertexPositionColor[2];
_lineListIndices = new short[2];
_lineListIndices[0] = 0;
_lineListIndices[1] = 1;
_lineVtxList[0] = new VertexPositionColor(new Vector3(0, 0, 0), Color.MediumPurple);
_lineVtxList[1] = new VertexPositionColor(new Vector3(100, 0, 0), Color.MediumPurple);
numLines = 1;
....
BasicEffect basicEffect = new BasicEffect(g);
basicEffect.VertexColorEnabled = true;
basicEffect.CurrentTechnique.Passes[0].Apply();
basicEffect.World = Matrix.CreateTranslation(new Vector3(0, 0, 0));
basicEffect.Projection = projMat;
basicEffect.View = viewMat;
if (_lineListIndices != null && _lineVtxList != null)
{
// g.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _lineVtxList, 0, 1);
g.DrawUserIndexedPrimitives<VertexPositionColor>(
PrimitiveType.LineList,
_lineVtxList,
0, // vertex buffer offset to add to each element of the index buffer
_lineVtxList.Length, // number of vertices in pointList
_lineListIndices, // the index buffer
0, // first index element to read
numLines // number of primitives to draw
);
}
The matrices projMat and viewMat are the same view and projection matrices I use to render everything else in the scene. It doesn't seem to matter whether I assign them to basicEffect or not. Here's what the scene looks like:
You aren't beginning or ending your BasicEffect
so the projection and view matricies won't be applied to DrawUserIndexedPrimitives
. Try encompasing your DrawUserIndexedPrimitives
call with this:
if (_lineListIndices != null && _lineVtxList != null)
{
basicEffect.Begin();
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passses)
{
pass.Begin();
g.DrawUserIndexedPrimitives<VertexPositionColor>(...); // The way you have it
pass.End();
}
basicEffect.End();
}