0
votes

Hi all im working on an ipad app using cocos2d objective c and box2d. my question is in relation to box2d and collisions with curves. basically i have circles within a larger circle arena. the smaller circles collide with each other fine but i want to know how to make them collide with the edges of the larger arena circle correctly so that they dont leave the arena and bounce back in. any ideas on how to approach this problem would be great cheers

2

2 Answers

0
votes

ok i got it working but im not sure if its the correct way to do it

int sides = 50;

    float x_coordPREV=([Arena contentSize].width/2)*cos(0.0);
    float y_coordPREV=([Arena contentSize].width/2)*sin(0.0);
    x_coordPREV += Arena.position.x;
    y_coordPREV += Arena.position.y;

    b2BodyDef arenaBodyDef;
    arenaBodyDef.position.Set(0,0);
    b2Body *arenaBody = _world->CreateBody(&arenaBodyDef);
    b2PolygonShape arenaBox;
    b2FixtureDef arenaShapeDef;
    arenaShapeDef.shape = &arenaBox;

    for(float angle=0.0f;angle<(2*M_PI);angle+=(2*M_PI)/sides){
        float x_coord=([Arena contentSize].width/2-10)*cos(angle);
        float y_coord=([Arena contentSize].width/2-10)*sin(angle);
        x_coord += Arena.position.x;
        y_coord += Arena.position.y;
        arenaBox.SetAsEdge(b2Vec2(x_coordPREV/PTM_RATIO,y_coordPREV/PTM_RATIO),b2Vec2( x_coord/PTM_RATIO,y_coord/PTM_RATIO ));
        arenaBody->CreateFixture(&arenaShapeDef);
        x_coordPREV = x_coord;
        y_coordPREV = y_coord;
    }

this creates a circle out of 50 sides and appears to be doing what i want. if anyone has a better way please let me know ty

0
votes

not sur eif this i what you are looking for but i saw this on a forum:

b2BodyDef edgedef;
edgedef.position.Set(0.f,10.f);
b2Body* edge = world->CreateBody(&edgedef);

b2Vec2 vertices[2];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(10.0f, 0.0f);
int32 count = 2;

b2PolygonShape polygon;
polygon.Set(vertices, count);

b2FixtureDef edgefixtureDef;
edgefixtureDef.shape = &polygon;

edge->CreateFixture(&edgefixtureDef);

you could see how you could add more points by sending a larger array