Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw simple circle in XNA

Tags:

c#

xna

I want to draw a 2d, filled, circle. I've looked everywhere and cannot seem to find anything that will even remotely help me draw a circle. I simply want to specify a height and width and location on my canvas.

Anyone know how?

Thanks!

like image 619
George Johnston Avatar asked Mar 25 '10 20:03

George Johnston


4 Answers

XNA doesn't normally have an idea of a canvas you can paint on. Instead you can either create a circle in your favorite paint program and render it as a sprite or create a series vertexes in a 3D mesh to approximate a circle and render that.

like image 157
Jake Pearson Avatar answered Nov 15 '22 21:11

Jake Pearson


You could also check out the sample framework that Jeff Weber uses in Farseer:
http://www.codeplex.com/FarseerPhysics

The demos have a dynamic texture generator that let's him make circles and rectangles (which the samples then use as the visualization of the physics simulation). You could just re-use that :-)

like image 22
Joel Martinez Avatar answered Nov 15 '22 21:11

Joel Martinez


Had the same problem, as others already suggested you need to draw a square or rectangle with a circle texture on it. Here follows my method to create a circle texture runtime. Not the most efficient or fancy way to do it, but it works.

Texture2D createCircleText(int radius)
{
    Texture2D texture = new Texture2D(GraphicsDevice, radius, radius);
    Color[] colorData = new Color[radius*radius];

    float diam = radius / 2f;
    float diamsq = diam * diam;

    for (int x = 0; x < radius; x++)
    {
        for (int y = 0; y < radius; y++)
        {
            int index = x * radius + y;
            Vector2 pos = new Vector2(x - diam, y - diam);
            if (pos.LengthSquared() <= diamsq)
            {
                colorData[index] = Color.White;
            }
            else
            {
                colorData[index] = Color.Transparent;
            }
        }
    }

    texture.SetData(colorData);
    return texture;
}
like image 8
Anon Avatar answered Nov 15 '22 22:11

Anon


Out of the box, there's no support for this in XNA. I'm assuming you're coming from some GDI background and just want to see something moving around onscreen. In a real game though, this is seldom if ever needed.

There's some helpful info here:

http://forums.xna.com/forums/t/7414.aspx

My advice to you would be to just fire up paint or something, and create the basic shapes yourself and use the Content Pipeline.

like image 3
BFree Avatar answered Nov 15 '22 20:11

BFree