Varcade Dev Blog

XNA Drawing 2D Lines, Shapes, and Laser Beams

by on Feb.26, 2011, under Coding, Games, XNA

I’ve been using this great XNA lib for drawing 2D lines, circles, and shapes for awhile. I’m using an older version that I cleaned up to minimize garbage collection ( to none! ), but the code is still being updated by CentauriBoy on the AppHub forums, and recently got anti-aliasing and filled shapes added.

Home thread on XNA forums:


Docs:  HOW_TO.txt

A simple example I use to draw a laser beam

beam = new BasicPrimitives(Game.GraphicsDevice);

beam.AddVector( this.position ); // Vector2
beam.AddVector( target.position ); // Vector2
beam.Colour = new Color(255, 255, 255, 180);
beam.Thickness = 1;

Notice the color in this example is slightly transparent. For my laser beams, I’m stacking 3 to 5 of these lines with slight offsets to get a glow effect. Also note that using ‘new Color’ in a draw loop is bad practice. You never want to be creating anything in your loops because it will later need to be removed by the garbage collector. To eliminate garbage collection, you would define your colors once in a constructor, and re-use them.

1 Comment for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!


A few highly recommended websites...