Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I would like to be able to draw arbitary lines in my sprite based 2D XNA game. How can I draw a simple line on screen in XNA without dealing with vertex arrays or shaders.

share|improve this question
    
Hey Ken, take a look at my little drawinghelper library: drippingflames.com/xna It uses vertex arrays in the background, so no 1x1 white texture. –  Luis Estrada Nov 19 '12 at 14:05

2 Answers 2

up vote 8 down vote accepted

You can draw a line using sprites. SpriteBatch.Draw(...) helpfully allows us to strech and rotate a sprite(texture).

In this code, we take a 1x1 pixel texture, strech it (by defining a rectangle of the correct shape and rotate it so it looks like a line.

    Texture2D t; //base for the line texture


    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        // create 1x1 texture for line drawing
        t = new Texture2D(GraphicsDevice, 1, 1);
        t.SetData<Color>(
            new Color[] { Color.White });// fill the texture with white
    }


    protected override void Draw(GameTime gameTime)
    {


        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        DrawLine(spriteBatch, //draw line
            new Vector2(200, 200), //start of line
            new Vector2(100, 50) //end of line
        );
        spriteBatch.End();



        base.Draw(gameTime);
    }


    void DrawLine(SpriteBatch sb, Vector2 start, Vector2 end)
    {
        Vector2 edge = end - start;
        // calculate angle to rotate line
        float angle =
            (float)Math.Atan2(edge.Y , edge.X);


        sb.Draw(t,
            new Rectangle(// rectangle defines shape of line and position of start of line
                (int)start.X,
                (int)start.Y,
                (int)edge.Length(), //sb will strech the texture to fill this rectangle
                1), //width of line, change this to make thicker line
            null,
            Color.Red, //colour of line
            angle,     //angle of line (calulated above)
            new Vector2(0, 0), // point in line about which to rotate
            SpriteEffects.None,
            0);

    }
share|improve this answer
1  
Something I want to note about this sort of line code is that it can need a little adjustment if you use a width greater than one. Because we're defining the height relative to the starting coordinates, using a length of more than 1 will make the line extend down, and if you have, for example, two separate 3-pixel lines with one rotated 180 degrees, one will extend up and one down. To fix this you can adjust the starting X coordinate based on the width of the line, like start.X - width / 2 –  ssb Nov 19 '12 at 13:03

This library: http://sourceforge.net/projects/primitives2d/ has worked well for me in the past.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.