Moving Platforms Using cocos2d-x and box2d

  • i_am_kk
    Likes 0

    Problem Description

    Let me explain how I tried to move platforms from right to left of the screen.
    First, I created an array of 5 box2d bodies and set their initial position. Then, I called a method in which I applied velocity to the bodies.
    But all the 5 sprites didn’t move.

    Sample Code:
     

    for(int i=0;i<5;i++){

    Move[i]->MoveObj();

    }

    void Move::MoveObj(){

    body->SetLinearVelocity(b2Vec2(-60.0f,0.0f));

    sprite->SetPosition(body->GetPosition().x,body->GetPosition().y);

    }

     

    The problem is all the 5 sprites were created and placed at their initial positions but not moving.

    Can you please help me in solving this issue?


  • Sonar Systems admin
    Likes 0

    Can you get a single object moving?

  • i_am_kk
    Likes 0

    yes, a single object moves but multiple objects don’t..

  • Sonar Systems admin
    Likes 0

    Show us the code for moving a single object.

  • i_am_kk
    Likes 0

    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !Layer::init() )
        {
            return false;
        } 

        b2Vec2 gravity;
        gravity.Set(0,0);
        world = new b2World(gravity);    
        world->SetContinuousPhysics(true);
        Size visibleSize = Director::getInstance()->getVisibleSize();
        Point origin = Director::getInstance()->getVisibleOrigin();
        
        fly = new Fly(this);     //When the fly constructor is called The sprite is created and position is set.
        fly->body1 = world->CreateBody(&(fly->ballBodyDef));
          
        this->schedule(schedule_selector(HelloWorld::FlyImage));
        return true;
    }


    void HelloWorld::FlyImage(float dt)
    {    
        fly->FlyImage();
        world->Step(dt,8,3);
    }


    void Fly::FlyImage()
    {   
        body1->SetLinearVelocity(b2Vec2(-60.0f,0.0f));
        sprite->setPosition(body1->GetPosition().x,body1->GetPosition().y);
    }

  • Sonar Systems admin
    Likes 0

    What was Move[i] in Move[i]->MoveObj();

     

    Is it a object?

  • i_am_kk
    Likes 0

    yes..

    According to the code..it’s the fly object…

    For Single Object I used: fly->FlyImage();

    For Multiple Objects I used: fly[i]->flyImage();

  • Sonar Systems admin
    Likes 0

    What’s the fly object, the problem?

  • i_am_kk
    Likes 0

    There are two methods in the Fly class.
    1) Constructor- where i set the sprite and the initial position.

    ​2) FlyImage – where the sprite is made to move by applying velocity.

    The fly object is for calling the method FlyImage of the Fly class from HelloWorld class.

    The problem is when this FlyImage method is called once, single sprite moves but when an array of fly object is created and the FlyImage method is called multiple times, the sprites dont move.


  • Sonar Systems admin
    Likes 0

    How are you creating the array of flying objects?

  • i_am_kk
    Likes 0

    Creating an array of 5 fly objects.

    for( int i =0;i<5i++){

         fly[i] = new Fly(this);    
         fly[i]->body1 = world->CreateBody(&(fly[i]->ballBodyDef));

    }

  • Sonar Systems admin
    Likes 0

    How are you declaring the fly array object?

  • i_am_kk
    Likes 0

    Fly *fly[5];

  • Sonar Systems admin
    Likes 0

    What’s the code for the  constructor       of the fly object.

     

    Also for code, try using the code feature, see screenshot below

  • i_am_kk
    Likes 0

    -----------------------HelloWorld.cpp---------------------------------------
    
    
    
    #include "HelloWorldScene.h"
    #include "Box2D\Box2D.h"
    #include "Fly.h"
    
    USING_NS_CC;
    
    Scene* HelloWorld::createScene()
    {
        // 'scene' is an autorelease object
        auto scene = Scene::create();
        
        // 'layer' is an autorelease object
        auto layer = HelloWorld::create();
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !Layer::init() )
        {
            return false;
        } 
    
    	b2Vec2 gravity;
    	gravity.Set(0,0);
    	world = new b2World(gravity);	
    	world->SetContinuousPhysics(true);
        Size visibleSize = Director::getInstance()->getVisibleSize();
        Point origin = Director::getInstance()->getVisibleOrigin();
    	
    	for(int i=0;i<5;i++){
    		fly[i] = new Fly(this);
    		fly[i]->body1 = world->CreateBody(&(fly[i]->ballBodyDef));
    	}
    	
    	this->schedule(schedule_selector(HelloWorld::FlyImage));
    	   
        return true;
    }
    
    
    void HelloWorld::FlyImage(float dt)
    {		
    	for(int i=0;i<5;i++){
    		fly[i]->FlyImage();
    	}
    	world->Step(dt,8,3);
    	CCLOG("Done");
    }
    

     

  • i_am_kk
    Likes 0

    #include "Fly.h"
    #include "Box2D\Box2D.h"
    
    USING_NS_CC;
    
    Fly::Fly(cocos2d::Layer *layer)
    {
    	visibleSize = Director::getInstance()->getVisibleSize();
    	origin = Director::getInstance()->getVisibleOrigin();
    
    	//dupSprite = Sprite::create("rock.png");
    	CCLOG("Creating");
    		dupSprite = Sprite::create("rock.png");
    		auto random = CCRANDOM_0_1()*200;
    		auto random1 = CCRANDOM_0_1()*5;
    		this->posX = 400 + random1 * random;
    		this->posY = random;
    		dupSprite->setPosition(posX,posY);
    		CCLOG("X: %d Y: %d",posX,posY);
    		layer->addChild(dupSprite);
    		ballBodyDef.type = b2_kinematicBody;
    		ballBodyDef.position = b2Vec2((dupSprite->getPosition().x),(dupSprite->getPosition().y));
    		ballBodyDef.angle = 0;
    		this->ballBodyDef.userData = dupSprite;
    		circle.m_radius = (dupSprite->getContentSize().width/2)/32;
    		ballFixDef.shape = &circle;
    		ballFixDef.density = 10;
    		ballFixDef.friction = 0.5f;
    		ballFixDef.restitution = 0.8f;
    		//body1->CreateFixture(&ballFixDef);
    
    }
    
    void Fly::FlyImage()
    {
    	body1->SetLinearVelocity(b2Vec2(-60.0f,0.0f));
    	dupSprite->setPosition(body1->GetPosition().x,body1->GetPosition().y);
    	
    }
    

     

  • i_am_kk
    Likes 0

    #include "Fly.h"
    #include "Box2D\Box2D.h"
    
    USING_NS_CC;
    
    Fly::Fly(cocos2d::Layer *layer)
    {
    	visibleSize = Director::getInstance()->getVisibleSize();
    	origin = Director::getInstance()->getVisibleOrigin();
    
    	//dupSprite = Sprite::create("rock.png");
    	CCLOG("Creating");
    		dupSprite = Sprite::create("rock.png");
    		auto random = CCRANDOM_0_1()*200;
    		auto random1 = CCRANDOM_0_1()*5;
    		this->posX = 400 + random1 * random;
    		this->posY = random;
    		dupSprite->setPosition(posX,posY);
    		CCLOG("X: %d Y: %d",posX,posY);
    		layer->addChild(dupSprite);
    		ballBodyDef.type = b2_kinematicBody;
    		ballBodyDef.position = b2Vec2((dupSprite->getPosition().x),(dupSprite->getPosition().y));
    		ballBodyDef.angle = 0;
    		this->ballBodyDef.userData = dupSprite;
    		circle.m_radius = (dupSprite->getContentSize().width/2)/32;
    		ballFixDef.shape = &circle;
    		ballFixDef.density = 10;
    		ballFixDef.friction = 0.5f;
    		ballFixDef.restitution = 0.8f;
    		//body1->CreateFixture(&ballFixDef);
    
    }
    
    void Fly::FlyImage()
    {
    	body1->SetLinearVelocity(b2Vec2(-60.0f,0.0f));
    	dupSprite->setPosition(body1->GetPosition().x,body1->GetPosition().y);
    	
    }
    

     

  • Sonar Systems admin
    Likes 0

    Is body1 a variable in the Fly class?

  • i_am_kk
    Likes 0

    Yes, Its a b2Body variable.

    I declared it in Fly.h as b2Body *body1;

  • Sonar Systems admin
    Likes 0

    has it been initialised?

  • i_am_kk
    Likes 1

            fly[i]->body1 = world->CreateBody(&(fly[i]->ballBodyDef));

    This is how it was initilased in HelloWorld.cpp. Refer the HelloWorld.cpp code above.

  • Sonar Systems admin
    Likes 0

    So if you don’t use an array and just use a single fly  object does it work?

  • i_am_kk
    Likes 0

    Yes, it works for single object.

  • Sonar Systems admin
    Likes 0

    Try 5 objects but not as an array so 5 separate objects.

Login to reply