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 7915723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:33:11+00:00 2026-06-03T14:33:11+00:00

I want to detect collision between two bodies, one body has circle shape and

  • 0

I want to detect collision between two bodies, one body has circle shape and another one has convex body (by using vertexes and number of vertexes), and I use contact listener class to detect collision between bodies. I tried detect collision between two circle shapes and it succeeded, but when I tried to detect collision between two bodies (one has convex shape and another one has circle shape) and it failed! even I use tags to identify these bodies.

How can I detect two bodies which have two different shapes (circle and convex)?

ContactListener.h code:

#import "Box2D.h"

class ContactListener : public b2ContactListener 
{
private: 
    void BeginContact(b2Contact* contact);
    void EndContact(b2Contact* contact);
};

ContactListener.mm code:

#import "ContactListener.h"
#import "SimpleAudioEngine.h"
#import "cocos2d.h"

void ContactListener::BeginContact(b2Contact* contact) {

    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();

    CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
    CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

    if (spriteA != NULL && spriteB != NULL) {
        if (spriteA.tag == 1 && spriteB.tag == 50) { // this is work (two shapes are circles

            [[SimpleAudioEngine sharedEngine] playEffect:@"Pin.wav"];
            NSLog(@"Contact With Pin is Beginning");
        }

        if (spriteA.tag == 1 && spriteB.tag == 51) { // this is not work (one is circle shape and another one is convex shape)

            [[SimpleAudioEngine sharedEngine] playEffect:@"Jump.wav"];
            NSLog(@"Contact With Barrier is Beginning");
        }
    }
}

void ContactListener::EndContact(b2Contact* contact) {

    b2Body* bodyA = contact->GetFixtureA()->GetBody(); 
    b2Body* bodyB = contact->GetFixtureB()->GetBody(); 

    CCSprite* spriteA = (CCSprite*)bodyA->GetUserData();
    CCSprite* spriteB = (CCSprite*)bodyB->GetUserData();

    if (spriteA != NULL && spriteB != NULL) {

    }
}

Tags on above code:

  • tag of ball (main sprite of game) shape is 1
  • tag of circle shape is 50
  • tag of convex shape is 51

Edit:

This is code of object’s bodies creation:

#define pinSpriteTag 50
#define barrierSpriteTag 51
#define ballSpriteTag 1

// Creating ball of game (has b2CircleShape)
-(void) createBallOfGameWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

        // Put ball on screen
        ballOfGameSprite = [CCSprite spriteWithFile:@"GameBall-1-ipad.png"];
        ballOfGameSprite.position = ccp(x, y);
        [self addChild:ballOfGameSprite z:1 tag: ballSpriteTag];

        // Create body of ball
        b2BodyDef ballOfGameBodyDef;
        ballOfGameBodyDef.type = b2_staticBody;
        ballOfGameBodyDef.position.Set(ballOfGameSprite.position.x/PTM_RATIO, ballOfGameSprite.position.y/PTM_RATIO);
        ballOfGameBodyDef.userData = ballOfGameSprite;
        ballOfGameBody = world->CreateBody(&ballOfGameBodyDef);

        // Create Physics properties of ball
        b2CircleShape ballOfGameShape;
        ballOfGameShape.m_radius = radius/PTM_RATIO;

        // Create fixture of ball
        b2FixtureDef ballOfGameFixtureDef;
        ballOfGameFixtureDef.shape = &ballOfGameShape;
        ballOfGameFixtureDef.density = 0.9f;
        ballOfGameFixtureDef.friction = 1.0f;
        ballOfGameFixtureDef.restitution = 0.9f;
        ballOfGameBody->CreateFixture(&ballOfGameFixtureDef);

}

// Creating pin of game (has b2CircleShape)
-(void) createNormalPinOnScreenWithPositionX:(int)x yPosition:(int)y radius:(float)radius {

        // Put ball on screen
        CCSprite *pin = [CCSprite spriteWithFile:@"GamePinNormal-ipad.png"];
        pin.position = ccp(x, y);
        [self addChild:pin z:2 tag:pinSpriteTag];

        // Create body of ball
        b2BodyDef pinBodyDef;
        pinBodyDef.type = b2_staticBody;
        pinBodyDef.position.Set(pin.position.x/PTM_RATIO, pin.position.y/PTM_RATIO);
        pinBodyDef.userData = pin;
        b2Body *pinBody = world->CreateBody(&pinBodyDef);

        // Create Physics properties of ball
        b2CircleShape pinShape;
        pinShape.m_radius = radius/PTM_RATIO;

        // Create fixture of ball
        b2FixtureDef pinFixtureDef;
        pinFixtureDef.shape = &pinShape;
        pinFixtureDef.density = 0.9f;
        pinFixtureDef.friction = 1.0f;
        pinFixtureDef.restitution = 0.85f;
        pinBody->CreateFixture(&pinFixtureDef);
}

// Creating Barrier of game (has b2PolygonShape)
-(void) createBarrierOnScreenPositionX:(int)x yPosition:(int)y imageName:(NSString *)imageName verts:(b2Vec2*)verts verNum:(int)verNum {

    CCSprite *BarrierOfGameSprite = [CCSprite spriteWithFile:imageName];
    BarrierOfGameSprite.position = ccp(x, y);
    [self addChild:BarrierOfGameSprite z:1 tag:barrierSpriteTag];

    b2BodyDef BarrierOfGameBodyDef;
    BarrierOfGameBodyDef.type = b2_staticBody;
    BarrierOfGameBodyDef.position.Set(BarrierOfGameSprite.position.x/PTM_RATIO, BarrierOfGameSprite.position.y/PTM_RATIO);
    BarrierOfGameBodyDef.userData = BarrierOfGameSprite;

    b2Body *BarrierOfGameBody = world->CreateBody(&BarrierOfGameBodyDef);

    b2PolygonShape BarrierOfGameShape;
    BarrierOfGameShape.Set(verts, verNum);

    b2FixtureDef BarrierOfGameFixtureDef;
    BarrierOfGameFixtureDef.shape = &BarrierOfGameShape;    
    BarrierOfGameFixtureDef.density = 0.9f;
    BarrierOfGameFixtureDef.friction = 1.0f;
    BarrierOfGameFixtureDef.restitution = 0.0f;
    BarrierOfGameBody->CreateFixture(&BarrierOfGameFixtureDef); 

}
  • 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-03T14:33:16+00:00Added an answer on June 3, 2026 at 2:33 pm

    Do not detect the collision between two shape.
    The best way is to set the object as the userData of the body.

    @implemention RigidBody
    -(id)init{
        ...
        self.body->userData = self;
    }
    

    And then compare the class and address

    void beginContact(b2contact contact){
        RigidBody *A = (RigidBody*)contact->GetFixtureA()->GetBody()->GetUserData();
        if([A isKindOfClass:[RigidBody class]]){
    
        }
        //  Or
        if(self == A){
    
        }
    

    I hope it ‘s helpful for you.

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

Sidebar

Related Questions

I want to detect if one canvas object for example - circle crosses another
Is there a built-in function to detect collision between two circles? I have used
In cocos2d, i've got two objects that I want to detect collision with. Im
I want to detect one kind of object such as person in the picture,
I want to detect the presence of a scroll bar in a DIV using
I want to detect events like Video is playing Video has been paused Video
I want to detect when the document has just started to load, so that
I want to improve a collision system. Right now I detect if 2 irregular
I'm developing a simple game in Android and I want to detect a collision
I want to detect whether a file is locked, using python on Unix. It's

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.