How to draw a dashed line in MonoGame / XNA

What is the easiest way to draw a dashed line in MonoGame / XNA 4?

Should I use 3D primitives or can I achieve this with a simple sprite?

+8
c # xna monogame
source share
3 answers

C3 user on bitbucket has published a nice utility class for creating 2D primitives. They add extension methods to the SpriteBatch class. The repository is here: https://bitbucket.org/C3/2d-xna-primitives/wiki/Home


Welcome

Welcome to the new home of the 2D XNA Primitives project, now using the Mercurial repository on Bitbucket! Now compatible with Windows phones.

Feature Reference

All public methods provided by this library are extension methods, which means that you can access them directly through your own SpriteBatch instance, as if these methods were built into the SpriteBatch class. If you call methods this way, you can ignore the first parameter for all of the following calls.

General methods

Drawarc

Drawcircle

Drawline

DrawRectangle

Fillillectangle

Putpixel

+5
source share

If this is a straight line (with simple 90-degree turns), then the sprite is probably the easiest.

(If you have access to custom shaders through the HiDef profile, then getting dashed lines in the shaders is not too complicated if you need a simple one)

If it twists and turns irregularly, I would suggest using 3d primitives.

+1
source share

Divide the problem into two parts.

1) Determine the shape of the curve if it is not straight.

2) Along this curve, create a triangular strip with the texture you want from the dashed line.

0
source share

All Articles