I'm still a little new to this, and I was wondering if there was an easier way to draw 2D lines. I'm making a 2D shooter, and all the sample codes and tutorials I've found so far seem very complicated for just drawing a simple 2D line on the screen (for things like user-interface purposes). So the gist of it, as I understand it so far is :

Create a List<Vector2>
Create a VertexBuffer
Create an IndexBuffer
Setup transformation matrices (really needed for just 2D )
Call DrawIndexedPrimitves

Correct If not, what am I doing wrong Any easier solutions


Re: XNA Game Studio Express 2D Lines

Shawn Hargreaves - MSFT

The DrawUserPrimitives is better for this kind of thing: it lets you draw directly from a vertex array without having to bother with vertex and index buffers.

Re: XNA Game Studio Express 2D Lines


I got my 2D lines working, but now they won't change colors. I'm just gonna post my code, and if anyone sees why I can't get my lines to be anything other than black, or if you see anything else I can improve on, I would appreciate it .

public class Renderer {

GraphicsDeviceManager graphics;
public SpriteBatch spriteBatch;
public Texture2D Epoch1, Epoch2, BlackBird, Player;
public Vector2 testSpritePosition = new Vector2(300.0f, 300.0f);
public Vector2 BlackbirdPosition = new Vector2(0.0f, 0.0f);

// UI stuff
//Matrix UIProjection = Matrix.Identity, UIView = Matrix.Identity;
Vector3 leftpos1 = new Vector3(-3500.0f, 3000.0f, 0.0f);
Vector3 leftpos2 = new Vector3(-3500.0f, -3000.0f, 0.0f);
Vector3 rightpos1 = new Vector3(3500.0f, 3000.0f, 0.0f);
Vector3 rightpos2 = new Vector3(3500.0f, -3000.0f, 0.0f);
VertexPositionColor[] leftUIBorder = new VertexPositionColor[2];
VertexPositionColor[] rightUIBorder = new VertexPositionColor[2];
public BasicEffect effect;
Matrix UIProjection = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 2, 1680.0f/1050.0f, 0.1f, 1000.0f);
Matrix UIView = Matrix.CreateLookAt(new Vector3(0, 0, -50), Vector3.Zero, Vector3.Up);

public Renderer( GraphicsDeviceManager device ) {
graphics = device;
// UI initializing
leftUIBorder[0] = new VertexPositionColor(leftpos1, Color.Red);
leftUIBorder[1] = new VertexPositionColor(leftpos2, Color.Red);
rightUIBorder[0] = new VertexPositionColor(rightpos1, Color.Pink);
rightUIBorder[1] = new VertexPositionColor(rightpos2, Color.Pink);


public void ClearScreen() {;

public void DrawTestSprite() {
if (Player.Equals(Epoch2)) {
spriteBatch.Draw(Epoch1, testSpritePosition, Color.White);
Player = Epoch1;
else {
spriteBatch.Draw(Epoch2, testSpritePosition, Color.White);
Player = Epoch2;

public void DrawBlackbirdSprite(Texture2D sprite) {
spriteBatch.Draw(BlackBird, BlackbirdPosition, Color.White);

public void DrawUILine() {
effect.VertexColorEnabled = true;
effect.World = UIView * UIProjection;
//effect.World = Matrix.Identity;
effect.Projection = UIProjection;
effect.View = UIView;
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
foreach (EffectPass pass in effect.CurrentTechnique.Passes) {
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, leftUIBorder, 0, 1);
graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, rightUIBorder, 0, 1);


Re: XNA Game Studio Express 2D Lines


You should be able to change the color using effect.DiffuseColor I think, been a while since I used BasicEffect.

Re: XNA Game Studio Express 2D Lines

Shawn Hargreaves - MSFT

Or just leave lighting turned off entirely - it doesn't really make a lot of sense to enable this since you don't have normals in your line vertices.

Re: XNA Game Studio Express 2D Lines


Not sure what you are using the lines for, but there was an wonderful article posted at on creating vector graphic style lines. The benefit of drawing lines in a similar fashion to this over the built in DX line drawing class, is that the lines will look good on TV and all resolutions. Just thought you might be interested in the article. Plus Mike has some other great XNA articles worth checking out.

- Bill