Shryder
Shryder

Reputation: 53

2D XNA - Draw a Circle

Yo, Hi everybody

Is there any Way to draw a circle? I don't want to use a Texture/sprite to draw a circle Because the Player is the Circle so the Circle Should move ... and also I'm Trying to make it so the Player/Circle's Size gets bigger and bigger When he eats some food blablabla...

anyways, if anybody knows how to do it please Tell me.

OTHERWISE : IS THERE A WAY TO CHANGE A TEXTURE HEIGHT / WIDTH , THEN I WILL MAKE A SIMPLE CIRCLE TEXTURE AND CHANGE HEIGHT / WIDTH OF IT.

Thanks.

Upvotes: 0

Views: 2155

Answers (2)

Bashn
Bashn

Reputation: 314

You can use 3D primitives like 'Someone' :> already posted or use the C3.XNA.Primitives2D libary where you can use a extension for SpriteBatch to draw a circle

public static void DrawCircle(this SpriteBatch spriteBatch, Vector2 center, float radius, int sides, Color color, float thickness);

If you use the same value for radius and thickness the circle appears filled. I didn't find the offizial download link, but there are also uploads at sourceforge.

Also you can generate a circle dynamically via code like:

public static Texture2D GenerateCircleTexture(GraphicsDevice graphicsDevice, int radius, Color color, float sharpness)
    {
        int diameter = radius * 2;
        Texture2D circleTexture = new Texture2D(graphicsDevice, diameter, diameter, false, SurfaceFormat.Color);
        Color[] colorData = new Color[circleTexture.Width * circleTexture.Height];
        Vector2 center = new Vector2(radius);
        for (int colIndex = 0; colIndex < circleTexture.Width; colIndex++)
        {
            for (int rowIndex = 0; rowIndex < circleTexture.Height; rowIndex++)
            {
                Vector2 position = new Vector2(colIndex, rowIndex);
                float distance = Vector2.Distance(center, position);

                // hermite iterpolation
                float x = distance / diameter;
                float edge0 = (radius * sharpness) / (float)diameter;
                float edge1 = radius / (float)diameter;
                float temp = MathHelper.Clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
                float result = temp * temp * (3.0f - 2.0f * temp);

                colorData[rowIndex * circleTexture.Width + colIndex] = color * (1f - result);
            }
        }
        circleTexture.SetData<Color>(colorData);

        return circleTexture;
    }

Sharpness below 1f blurs the circle.

Upvotes: 1

elldur
elldur

Reputation: 2103

The only way to draw primitives (e.g. circles) is in 3D: https://msdn.microsoft.com/en-us/library/bb196414.aspx

Or you can load a texture that is 1*1, stretch it into a line and then use a bunch of those lines to make a circle. Use:

public void Draw (
         Texture2D texture,
         Vector2 position,
         Nullable<Rectangle> sourceRectangle,
         Color color,
         float rotation,
         Vector2 origin,
         Vector2 scale,
         SpriteEffects effects,
         float layerDepth
)

to stretch the texture.

Or you can just use a circle texture and stretch it.

If you are making something like agar.io then you might want to use a texture combined with the circle primitive so you can make the circle 'wobbly'.

Upvotes: 0

Related Questions