Draw simple circle in XNA

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 :-)


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.


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;
}

Tags:

C#

Xna