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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:45:05+00:00 2026-06-13T10:45:05+00:00

Ball is not getting bounced what am I doing wrong ? here is the

  • 0

Ball is not getting bounced what am I doing wrong ?

here is the class

   #include "GameLayer.h"

USING_NS_CC;

GameLayer::GameLayer(){
    mWorld = NULL;
    mBody=NULL;
    mBall=NULL;

}

GameLayer::~GameLayer(){

       delete mWorld;
       mBody=NULL;
        delete mBall;
            mBall=NULL;
}

bool GameLayer::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    // Create sprite and add it to the layer
    mBall = CCSprite::spriteWithFile("Ball.jpg",CCRectMake(0,0,52,52));
    mBall->setPosition(ccp(100,100));
    addChild(mBall);

    // Create a world
    b2Vec2 gravity;
    gravity.Set(0.0f,-10.0f);

    mWorld = new b2World(gravity);

    // Do we want to let bodies sleep?
   // mWorld->SetAllowSleeping(true);

   // mWorld->SetContinuousPhysics(true);

    //Definition of the body
    // body definition to specify initial properties of the body such as position or velocity.
    b2BodyDef  groundBodyDef;
    groundBodyDef.position.Set(0,0);

    // use the world object to create a body object by specifying the body definition.
    b2Body *groundBody = mWorld->CreateBody(&groundBodyDef);

    //Create the shape as needed   (for ground all the sides)
    b2EdgeShape groundEdge;
    groundEdge.Set(b2Vec2_zero,b2Vec2(winSize.width/PTM_RATIO,0));
    groundBody->CreateFixture(&groundEdge,0);
    groundEdge.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&groundEdge,0);
    groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
                        b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
    groundBody->CreateFixture(&groundEdge,0);
    groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO,
                               winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
    groundBody->CreateFixture(&groundEdge,0);

    // Create the another body
        //1. body defn
        //2. create body from game world
        // 3. define shape and create fixtures
     b2BodyDef ballBodyDef;
     ballBodyDef.position.Set(100/PTM_RATIO,100/PTM_RATIO);
     // applying sprite as userdata to the bodydef
     ballBodyDef.userData = mBall;
     mBody = mWorld->CreateBody(&ballBodyDef);

     // created different shapes as you want
     b2CircleShape circle;
     circle.m_radius = 26.0/PTM_RATIO;

     //create the fixturedef
     b2FixtureDef ballShapeDef;
     ballShapeDef.shape = &circle;
     ballShapeDef.density=1.0f;
     ballShapeDef.friction=0.2f;
     ballShapeDef.restitution=0.8f;

     mBody->CreateFixture(&ballShapeDef);

     setAccelerometerEnabled(true);

    scheduleUpdate();

    return true;
}


void GameLayer::update(float dt){

    mWorld->Step(dt,10,10);

     for(b2Body *b = mWorld->GetBodyList(); b; b=b->GetNext()) {
            if (b->GetUserData() != NULL) {
                CCSprite *ballData = (CCSprite *)b->GetUserData();
                ballData->setPosition(ccp(b->GetPosition().x * PTM_RATIO,
                        b->GetPosition().y * PTM_RATIO));
                ballData->setRotation(-1 * CC_RADIANS_TO_DEGREES(b->GetAngle()));

            }
        }

}

void GameLayer::didAccelerate(CCAcceleration* pAccelerationValue)
{

    b2Vec2 gravity(-pAccelerationValue->y * 15,pAccelerationValue->x *15);
       mWorld->SetGravity(gravity);


}
  • 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-13T10:45:06+00:00Added an answer on June 13, 2026 at 10:45 am

    b2BodyDef has b2_staticBody type by default. Try to set

    ballBodyDef.type = b2_dynamicBody;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This class use to draw ball but its onDraw() function not call so ball
Say I want to draw a Ball in the scene and here are two
My goal is getting the ball to be centered at the paddle even if
I hope You are more 'on the ball' than I am. I remember getting
This basic menu works perfectly in all modern browsers but is not playing ball
I have a string bat and ball not pen or boat not phone I
I'm not sure how to call this. Basically I want to make a class
I just get the beach ball all day long (it's been doing nothing for
I am having a problem getting my application to draw a Ball when i
I'm writing a pong game and I have a ball class that has velocity

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.