8

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

Should I use 3d primitives or can I achieve it with a simple sprite?

adrin
  • 3,738
  • 8
  • 40
  • 60

3 Answers3

5

The user C3 on bitbucket has published a nice utility class to create 2D primitives. These 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 a Mercurial repository on Bitbucket! Now compatible with Windows Phones.

Function Reference

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

Public Methods

DrawArc

DrawCircle

DrawLine

DrawRectangle

FillRectangle

PutPixel

borrillis
  • 656
  • 4
  • 7
1

If it's a straight line (with simple 90 degree turns) then a sprite is probably the easiest.

(If you've got access to custom shaders via the HiDef profile then achieving dashed lines in shaders isn't too difficult if one keeps one's needs simple)

If it's twisting and turning in an irregular fashion then I would suggest using 3d primitives.

lzcd
  • 1,325
  • 6
  • 12
  • Thanks! I am not sure how can I draw a dashed line with a sprite though. Is there a way to clip a sprite drawn (have one long line and clip it accordingly?) Or do I have to draw multiple sprites(one per segment) and repeat them (seems like suboptimal ?) – adrin Mar 12 '13 at 16:22
  • The two options that occur to me are having an all white sprite and drawing it repeatedly with spacing ... or having one sprite with one or more spaces in the texture and scaling with the texture set to wrap. – lzcd Mar 12 '13 at 21:52
0

Break the problem into two parts.

1) Define the shape of the curve, if it isn't a straight line.

2) Along that curve, create a triangle strip with the texture you want of the dotted line.