## How to draw squares on the outside of a circle?

Discuss issues specific to miscellaneous ports of Box2D
tfarmer4
Posts: 2
Joined: Fri Nov 30, 2012 1:35 pm

### How to draw squares on the outside of a circle?

I'm building a Rube Goldberg machine in Box2D and I need to have a "gear" catch a ball in its teeth. I have a circle shape and wish to draw 8 boxes at even intervals over the whole circle for the ball to fall into but I cannot figure out how to do this mathematically. Can someone help me out with the math? RW2PW(float) function just converts pixels from render world to meters in physics world. Below code is my circle. Thanks ahead for your help. I've been stressing over this for hours on end.

Code: Select all

`b2CircleShape circle1;circle1.m_radius = RW2PW(64.0f);b2BodyDef bd1;bd1.type = b2_dynamicBody;bd1.position.Set(x, y);b2Body* body1 = g_b2dPhysicsWorld.CreateBody(&bd1);pGameObject1->SetPhysicsBody(body1); body1->CreateFixture(&circle1, 5.0f);`

tfarmer4
Posts: 2
Joined: Fri Nov 30, 2012 1:35 pm

### Re: How to draw squares on the outside of a circle?

I solved this by drawing rectangles that go through the circle with the code below. Can't believe I didn't think of this earlier...

Code: Select all

`            float my_angle = b2_pi/4;                                b2BodyDef bd;            bd.type = b2_dynamicBody;            bd.position.Set(x, y);            b2Body* body = m_world->CreateBody(&bd);            body->CreateFixture(&circle1, 2.0f);            int k = 0;            while(k<4){               b2PolygonShape polygon2;               polygon2.SetAsBox(0.1f, 1.0f, b2Vec2(circle1.m_p.x,circle1.m_p.y),my_angle);               my_angle += (b2_pi/4.0f);               body->CreateFixture(&polygon2, 2.0f);               k++;            }`

genbox
Posts: 69
Joined: Mon Jun 15, 2009 4:34 pm

### Re: How to draw squares on the outside of a circle?

You are building a Rube Goldberg machine, you are supposed to think outside the box2d (phun).

In Farseer Physics Engine, I have some code to create gears as a single polygon. This allows for a larger variety of gears, but they are all concave, so they will need to be decomposed into convex polygons.

Writte in C#:

Code: Select all

`public static Vertices CreateGear(float radius, int numberOfTeeth, float tipPercentage, float toothHeight){    Vertices vertices = new Vertices();    float stepSize = MathHelper.TwoPi / numberOfTeeth;    tipPercentage /= 100f;    MathHelper.Clamp(tipPercentage, 0f, 1f);    float toothTipStepSize = (stepSize / 2f) * tipPercentage;    float toothAngleStepSize = (stepSize - (toothTipStepSize * 2f)) / 2f;    for (int i = numberOfTeeth - 1; i >= 0; --i)    {        if (toothTipStepSize > 0f)        {            vertices.Add(                new Vector2(radius *                            (float)Math.Cos(stepSize * i + toothAngleStepSize * 2f + toothTipStepSize),                            -radius *                            (float)Math.Sin(stepSize * i + toothAngleStepSize * 2f + toothTipStepSize)));            vertices.Add(                new Vector2((radius + toothHeight) *                            (float)Math.Cos(stepSize * i + toothAngleStepSize + toothTipStepSize),                            -(radius + toothHeight) *                            (float)Math.Sin(stepSize * i + toothAngleStepSize + toothTipStepSize)));        }        vertices.Add(new Vector2((radius + toothHeight) *                                    (float)Math.Cos(stepSize * i + toothAngleStepSize),                                    -(radius + toothHeight) *                                    (float)Math.Sin(stepSize * i + toothAngleStepSize)));        vertices.Add(new Vector2(radius * (float)Math.Cos(stepSize * i),                                    -radius * (float)Math.Sin(stepSize * i)));    }    return vertices;}`