Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8531431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:28:53+00:00 2026-06-11T09:28:53+00:00

I’m working on a game in Cocos2d with Box2D. Currently I have a system

  • 0

I’m working on a game in Cocos2d with Box2D. Currently I have a system set up where there are some boundaries. They are for a ball. I want meteors to come in from off the screen and smack the ball.

The boundaries are defined in my init method as follows:

    // Define the ground body.
    b2BodyDef groundBodyDef;
    groundBodyDef.type = b2_staticBody;
    groundBodyDef.position.Set(0, 0); // bottom-left corner

    // Call the body factory which allocates memory for the ground body
    // from a pool and creates the ground box shape (also from a pool).
    // The body is also added to the world.
    b2Body* groundBody = world->CreateBody(&groundBodyDef);

    // Define the ground box shape.
    b2PolygonShape groundBox;       

    // bottom
    groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

    // top
    groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
    groundBody->CreateFixture(&groundBox,0);

    // left
    groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
    groundBody->CreateFixture(&groundBox,0);

    // right
    groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundBox,0);

    //Collision filter stuff here. Not working.
    b2Filter filter;
    filter.groupIndex = -2;
    groundBody->GetFixtureList()[0].SetFilterData(filter);

My ball is defined as follows:

    //Physics object.
    b2BodyDef ballBodyDef;
    ballBodyDef.position.Set(ball.position.x/PTM_RATIO, ball.position.y/PTM_RATIO);
    ballBodyDef.type = b2_dynamicBody;
    ballBodyDef.userData = ball;
    ballBodyDef.bullet = true;
    ballBody = world->CreateBody(&ballBodyDef);

    b2CircleShape ballShape;
    ballShape.m_radius = 10.0/PTM_RATIO;
    b2FixtureDef ballFix;
    ballFix.restitution = 1.1f;
    ballFix.density  = 0.0f;
    ballFix.shape = &ballShape;
    ballBody->CreateFixture(&ballFix);

And the meteors are made using two methods, the shorter method places the meteors. The longer one actually creates them using the points given from the shorter one:

-(void)createMeteorAtPoint:(CGPoint)point {
    //Create particle sprite representation.
    CCParticleMeteor *meteor = [[CCParticleMeteor alloc] initWithTotalParticles:200];
    meteor.gravity = ccp(-200,0);
    meteor.life = 0.5;
    meteor.lifeVar = 0.25;
    meteor.position = point;
    meteor.tag = 2;
    meteor.startSize = 5.0;
    meteor.startSizeVar = 3.0;
    [self addChild:meteor];

    //Make meteor physics body.    
    b2BodyDef meteorBodyDef;
    meteorBodyDef.position.Set(point.x/PTM_RATIO, point.y/PTM_RATIO);
    meteorBodyDef.type = b2_dynamicBody;
    meteorBodyDef.userData = meteor;
    b2Body *meteorBody = world->CreateBody(&meteorBodyDef);
    meteorBody->SetBullet(true);

    b2CircleShape meteorShape;
    meteorShape.m_radius = 7.5/PTM_RATIO;
    b2FixtureDef meteorFix;
    meteorFix.shape = &meteorShape;
    meteorFix.density = 1;

    //Give it the same negative group index of the boundaries to prevent collision.
    //Not working.
    meteorFix.filter.groupIndex = -2;

    meteorBody->CreateFixture(&meteorFix);

    //Give it a motion.
    b2Vec2 F;
    F.Set(CCRANDOM_0_1()*2, 0);
    meteorBody->SetLinearVelocity(F);
}

//Create a bunch of meteors that will sweep from left to right.
-(void)createMeteors {
    for (int i = 0; i < 8*40; i += 20) {
        [self createMeteorAtPoint:ccp(-40.0f, i + 2)];
    }
    NSLog(@"HERE");
}

I can see physics objects pile up on the side of the screen because I have debug flags set up. But one: They are not getting past the boundary despite being the same negative group index. And two: the ball decides it doesn’t like the right wall, and goes through it.

If you believe I am missing something crucial please tell me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:28:53+00:00Added an answer on June 11, 2026 at 9:28 am

    Only one of the boundary fixtures is being given the group index of -2 (the bottom). You can loop over all fixtures of a body like this:

    for (b2Fixture* f = body->GetFixtureList(); f; f = f->GetNext())
    {
        //do something with the fixture 'f'
        f->SetFilterData(...);
    }
    

    I’m not sure why the ball would be ignoring any walls though (unless you are moving it by creating a mouse joint between the ball and the ground body that the walls belong to, and the joint is set to not collide connected bodies… but in that case the ball would ignore all walls, and only while being moved).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.