I've looked at a few threads regarding this, but to be honest, when it comes to primitives, I havent a clue (I'm a 2D relic), now loading images etc etc is pretty simple stuff, I looked in the docs and found an example that is pretty much overkill to my needs, and its just a jumble.

All I'm after is how to draw a line from x,y to destinationX, destinationY. Can someone run through it for me, maybe explain a little whats going on, as I'd like to know, it should be simple, but alas, its confusing the life out of me! :D

Thanks in advance


Re: XNA Game Studio Express Drawing a line?

Jim Perry

Check here.

Re: XNA Game Studio Express Drawing a line?

Re: XNA Game Studio Express Drawing a line?


Here's what I use for drawing lines...
This one takes a sprite and scales (and colors) it to draw it between two points. Try passing a sprite that's a 4x4 white block. This is probably the easiest to get going. It just uses a Texture2D and a SpriteBatch.

void DrawLine(Texture2D spr, Vector2 a, Vector2 b, Color col)
Vector2 Origin = new Vector2(0.5f, 0.0f);
Vector2 diff = b - a;
float angle;
Vector2 Scale = new Vector2(1.0f, diff.Length()/spr.Height);
angle = (float)(Math.Atan2(diff.Y, diff.X))-MathHelper.PiOver2;
sprBatch.Draw(spr, a, null, col, angle, Origin, Scale, SpriteEffects.None, 1.0f);
This one uses an image to step along a line. My lineSprite is just a small circular dot that's white on the inside with a black outline so I can see it on any colour background. You could use a small line segment, rotate it to match the angle of the line and the spacing so it looks like a continous line.
void RenderLine(SpriteBatch batch, Vector2 a, Vector2 b, Color col)
Vector2 diff = a - b;
float len;
len = diff.Length() / 8;
for (int i = 0; i < len; i++)
batch.Draw( lineSprite, a, col );
a.X -= diff.X / len;
a.Y -= diff.Y / len;