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

  • Home
  • SEARCH
  • 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 6106453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:04:52+00:00 2026-05-23T14:04:52+00:00

i am using cocos2d and box2d, with contact listener, and lets say i have

  • 0

i am using cocos2d and box2d, with contact listener, and lets say i have a body that can hit a number of other bodies, BUT each one of them is turn on the contact listener.
so how can i know who hit who ?

i have this in my tick :

for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) 
{
    MyContact contact = *pos;
    b2Body *bodyA=contact.fixtureA->GetBody();
    b2Body *bodyB=contact.fixtureB->GetBody();

    //check if collision between to bodies
    if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL)    //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
    {
        NSLog(@"Ball hit bottom!");
    }

thanks a lot .

  • 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-05-23T14:04:52+00:00Added an answer on May 23, 2026 at 2:04 pm

    while creating the body set userdata like this

    CCSprite *red=[CCSprite spriteWithFile:@”red.png”];

    red.tag=3;
    
    [self addChild:red];
    

    b2BodyDef bd;

    bd.type=b2_dynamicBody;
    
    bd.position.Set(w/PTM_RATIO,h/PTM_RATIO);   
    
    bd.userData=red;
    
    
    for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) 
    {
        MyContact contact = *pos;
        b2Body *bodyA=contact.fixtureA->GetBody();
        b2Body *bodyB=contact.fixtureB->GetBody();
    
        //check if collision between to bodies
        if( bodyA->GetUserData() !=NULL && bodyB->GetUserData() !=NULL)    //if ((contact.fixtureA == _bottomFixture && contact.fixtureB == _ballFixture) ||(contact.fixtureA == _ballFixture && contact.fixtureB == _bottomFixture))
        {
            so here 
    
    
           CCSprite *actor = (CCSprite*)bodyA->GetUserData();
    
    
            if ([actor tag] == 3) {
                 //red box
    
            }
    
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a platformer for the iPhone that is using Box2D and cocos2D.
I'm using cocos2d with objective C. I have a class called CrystalineBubble that is
I am using Box2D with Cocos2D in my game. I am aware that Cocos2D
I'm making an ipad game using cocos2d and box2d. Among other elements, there's a
I have two classes, using Box2d,Cocos2d. Construct.mm and Level1.mm Construct contains all the methods
im using xcode4 and box2d/cocos2d. Im having some strange difficulties with runtime errors that
I am using Box2d and Cocos2d to develop an iPhone game. I have a
I am writing an iphone game using cocos2d and box2d engines. I have a
I am using the cocos2d-iphone + Box2d engine, I have a simple call in
I have sprites(which have bodies) that can fall down into a box, in a

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.