Draw a circle, using to circle class

Hi all! I have fast question. How to draw a circle using to this class(struct) ?

Code: https://wklej.to/32vj7

I found it on forum and i want use it. I tried do this:
In constructor:
circle = new Circle(new Vector2(10,10), 100);
In Draw method:
spriteBatch.Draw(balltexture, circle, Color.Black);

Circle isn’t rectagle… so… How to draw this circle with my custom texture or with custom color ?

Sorry for my english, and thank you for help :wink:

You can’t use that Circle structure as if it were a rectangle.
Just make sure that your texture represents a circle and draw it like you would normally do:
spriteBatch.Draw(myCircleTexture, myRectangle, myColor);

So, I should load a file with my circle and use it as rectagle in spriteBatch.Draw() method and moving positions Circle struct with my rectagle to check collisions ?
It’s good idea or not?

You could let your Circle structure have a Boundary rectangle property.

public struct Circle
{
    ...
    public Rectangle Bounds 
    { 
        get { return new Rectangle(this.X, this.Y, 2 * this.Radius, 2 * this.Radius); } 
    }

Then you draw using the Bounds.

How to do?

Your code create new Rectagle, what has four “radius” how circle. How to check collision this circle and draw it ?

This code drawing rectangle:
spriteBatch.Draw(ball, new Rectangle(this.X, this.Y, 2 * this.Radius, 2 * this.Radius), Color.White);
It’s ok, becasue it’s rectangle…

How to do?

Easy:
spriteBatch.Draw(myCircleTexture, myCircle.Bounds, myColor);

Where Bounds is calculated like I said above.
Since your Bounds and your myCircle always have the same position, you can collision check with the Circle’s “Intersect” method.

Also, if you didn’t get it, the Bounds is a rectangle that is exactly as big as your circle’s diameter.

Ok, but this method is drawing rectangle, not circle. I’m must using texture(png) with my circle ?

It doesn’t matter if you draw a rectangle or a circle. What matters is that your texture is a circle.

Ok, thank you for help :wink:

@Luki - Not sure if this is of use to you as I’ve only glanced down this topic, but the below allows you to draw primitive shapes:

http://jcpmcdonald.com/libraries/2d-xna-primitive-shapes/

I think MonoGame.Extended also has some functionality for drawing primitives on the way: https://github.com/craftworkgames/MonoGame.Extended/pull/217

That’s great - Yesterday I had to change the source of my version of Primitive3d to update it to us .net 4.5+ with the new 3.5 monogame.

One other question - What is Monogame Extended? I’ve not heard of this.Is it part of the download of monogame files or do I have to download it from somewhere else?

Edit - Don’t worry, I’ve done some googling and found:

Create new page · craftworkgames/MonoGame.Extended Wiki · GitHub

@luki I’m answering the original question in regard to ShapeBatch since it’s been referenced in this thread.

The Circle struct you are talking about is an “implicit shape”; a shape defined by mathematical function. In this case the function is r^2 = x^2 + y^2, where r is the radius and x and y are the centre position of the circle.
To draw things in OpenGL, DirectX, etc you need to use an “explicit shape” which is defined by vertices with each a position attribute. These vertices need to be “uploaded” to the GPU using a vertex buffer and one of the primitive types which define the topology and ordering of vertices in the vertex buffer. So what you are asking for is the ability to go from an “implicit” shape to an “explicit” shape on the fly and draw the explicit shape to the screen; a not so easy task!

SpriteBatch can only do textured rectangles (quadrilaterals) on the fly using two triangles with PrimitiveType.TriangleList (4 vertices, 6 indices).

Here is a comment diagram paraphrased from SpriteBatcher.cs Line 153.

         * TL    TR
         *   0----1 Vertices: { 0, 1, 2, 3 }
         *   |   /| Triangle 1: 0-1-2 (Clockwise)
         *   |  / | Triangle 2: 1-3-2 (Clockwise)
         *   | /  |
         *   |/   |
         *   2----3
         *  BL    BR

To draw a circle on the fly you either need to use a texture of a circle and draw it as a sprite (textured quadrilateral) using SpriteBatch, or create the vertices for the circle and draw them using one of the GraphicsDevice.Draw...Primitives() methods. Unfortunately, the latter prevents you from using the SpriteBatch API for streaming dynamic textured vertices and the former does not have good performance if you want to draw different type of shapes beyond just circles and rectangles since you would need a separate texture for each shape. It’s for these reasons I am working on ShapeBatch over at MonoGame.Extended.

ShapeBatch will (eventually) be able to do everything SpriteBatch can do and more. It will be able to draw sprites, strings (from bitmap fonts too), rectangles, arcs, circles, polygons, etc.

Thank you very much for help! :wink:
I almost solved this problem, I create method, what create Texture circle and “form” circle with use class, what is at the beginning of the topic. I have problem with checking colisions yet, but I think that I to make it. I should have more time now, so I will try this make.

Sorry for my english :stuck_out_tongue: I still learn it.

Thank you for help, again! :wink:

i created a simple collision similar to how the rectangle class does using your idea

public struct Circle
{
    public Vector2 Position;
    public int Radius;
    public Circle(Vector2 position, int radius)
    {
         Position = position;
         Radius = radius;
    }
    public bool Intersects(Circle circle)
    {
         double x1 = Position.X;
         double y1 = Position.Y;
         double x2 = circle.Position.X;
         double y2 = circle.Position.Y;

         double distance = Math.Abs(Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2)));
         if (distance <= Radius + circle.Radius)
         {
             return true;
         }

         return false;
    }
}
1 Like