Line drawing

I know lines in XNA and Mono seem to be a problem, but I’d love to find a solution to the idea of drawing a kind of lightning strike from point a to point b. All 2D

The idea is I’d divide the vector into 10 and randomise the drawing points along the line by a few pixels on each update producing a dancing lightning effect.

The only way I know to do this is create a colour matrix and draw the line pixel by pixel, this just has too much overhead to be a possibility.

Does anyone know how else this might be achieved without tying up too much processing time?

Thanks.

1 Like

Get a 1 pixel texture, and simply just draw it.

You mean run the code under the draw procedure and draw each pixel in the line?

The SpriteBatch.Draw function contains Rectangle property, which gives you possibility to scale the 1x1 pixel texture, so you’ll draw the texture only once. See this: https://msdn.microsoft.com/en-us/library/ff433986.aspx

I implemented a feature in MonoGame.Extended recently that draws primitive shapes, including lines using a SpriteBatch. Here’s the relevant code that should give you everything you need.

private static Texture2D _texture;
private static Texture2D GetTexture(SpriteBatch spriteBatch)
{
    if (_texture == null)
    {
        _texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
        _texture.SetData(new[] {Color.White});
    }

    return _texture;
}

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f)
{
    var distance = Vector2.Distance(point1, point2);
    var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
    DrawLine(spriteBatch, point1, distance, angle, color, thickness);
}

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f)
{
    var origin = new Vector2(0f, 0.5f);
    var scale = new Vector2(length, thickness);
    spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, 0);
}

I wouldn’t say it’s the most efficient way to draw line. But it’ll work just fine if you’re only drawing a few lines every now and then. I normally use this stuff for debugging but it’s also very effective for simple effects like this or prototyping.

An alternative method would be to use a BasicEffect. The Farseer Physics library has a nice implementation of PrimitiveBatch that does something like this. We’ve been thinking about adding this to MonoGame.Extended also.

2 Likes

I suggest you use a library for that, like this one Primitives2D.

No need to implement that on your own. It works like the code in the answer of craftworkgames (which is cool, by the way), is fast, just a single static class and provides more than just drawing a simple line (and it’s free).

A lightning strike, on the other hand, is more complicated.
You won’t be able to pull of a decent effect for that one without a bit of reading.
I find this technique intriguing, although I didn’t test that one. I went with this one here.

Hope that helps,
Psilo

@throbax The implementation in MonoGame.Extended is in fact the same code as the Primitives2D library (with permission from the author of course).

Well, it started out that way, but we’ve refactored the code a bit and fixed a couple of bugs. If you do use the Primitives2D library be careful drawing lots of different sized circles. It caches the circles indefinitely and never clears the cache creating a memory leak. We’ve intentionally removed the caching for this reason.

And yeah, I agree with using one of those tutorials for implementing the lightning effect. That’s the way I would do it.

Nice work. Thx for the information.
Psilo

btw, I sent you a pm on Facebook. Hope you got it. Just checking.

Just to add more mud to the water :smiley:
You can also check out the article I wrote about creating lightning by rendering lines and bluring them :smiley: