0

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:

enter image description here

NickLokarno
  • 300
  • 6
  • 20

1 Answers1

1

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();
}
Foggzie
  • 9,691
  • 1
  • 31
  • 48
  • 1
    Thank you for the response, but I'm using XNA 4.0, this version of BasicEffect doesn't have that method. – NickLokarno Mar 04 '13 at 01:31
  • 2
    turns out that Begin() and End() aren't part of BasicEffect anymore, instead you just type: pass.apply(), and don't worry about ending anything. Thanks for your response though, that set me on the right track! I edited your response to reflect v4.0, so my previous comment can be ignored. – NickLokarno Mar 04 '13 at 02:07
  • Looks like you edited it back. This might be effective in XNA 3.0, but not in 4.0. In 4.0, you just call pass.Begin(), but you don't need basicEffect.Begin(), or basicEffect.End() – NickLokarno Apr 05 '13 at 23:25
  • @NickLokarno Odd... I haven't touched your edit. It's possible your edit wasn't approved via the review process, otherwise it would have an "Edited by" mark. Seems strange :/ – Foggzie Apr 11 '13 at 20:45