Vineet
Vineet

Reputation: 47

Implementing Parallax on Z-axis in cocos2d-x V3.10

It's been not very long I'm using cocos2d-x. It's fun and is quite easy to get hands on. But recently, I'm stuck while implementing something. ( It's been a while now).

I have implemented Parllax on X and Y axis. Was a easy job. But when I try to implement it on Z- axis ( Imagine perspective view ), I just can't.Imagine this view

Please suggest something. Explanation in context to cocos2d-x will be much appreciated. I use it with C++. Thank You !

Upvotes: 0

Views: 195

Answers (1)

Meysam Mohammadi
Meysam Mohammadi

Reputation: 550

I wrote a code for infinite scrolling background in Z axis.
Most important tip is where I compute distance between background tiles. I use height of background image as distance between tiles but store it before I rotate sprite around X axis.

Here is variables defined in header file (LevelScene.h):

...
private:
   std::vector<Sprite*> mBackgrounds;  // background tiles 
   Layer* mlayer;                      // main layer where background tiles are in
   Camera* mMainCamera;                // main camera
   int count = 0;                      // number of tiles that fills distance between camera and its far plane
   float height = 1;                   // this is distance between each tile indeed
...

and LevelScene.cpp file contains:

bool LevelsScene::init()
{
   if( !Layer::init() )
   {
       return false;
   }

   auto visibleSize = Director::getInstance()->getVisibleSize();
   auto origin = Director::getInstance()->getVisibleOrigin();

   mlayer = Layer::create();
   mlayer->setCameraMask( (unsigned short)CameraFlag::USER2 );
   addChild( mlayer );
   float far = 3500;
   mMainCamera = Camera::createPerspective(80, (float)visibleSize.width/visibleSize.height,
                                        1.0, far);
   mMainCamera->setCameraFlag(CameraFlag::USER2);
   this->addChild(mMainCamera);

   auto bg = Sprite::create();
   bg->initWithFile("asphalt.png");
   //The line below is very important. We use height of image as distance between tiles. We store height before rotating sprite.
   height = bg->getBoundingBox().size.height;

   bg->setRotation3D( Vec3(-90,0,0) );
   bg->setPosition3D( Vec3(visibleSize.width/2,0,-200 ) );
   bg->setCameraMask( (unsigned short)CameraFlag::USER2 );
   mlayer->addChild( bg );

   mMainCamera->setPosition3D(bg->getPosition3D() + Vec3(0,150,300));
   mMainCamera->lookAt(bg->getPosition3D()+Vec3(0,100,0), Vec3(0.0,1.0,0.0));

   float lastZ = bg->getPositionZ() - height;
   count = ceil(far/height) + 2;

   mBackgrounds.push_back( bg );
   for( int i = 0; i< count; i++ )
   {
      auto bg = Sprite::create();
      bg->initWithFile("asphalt.png");
      bg->setRotation3D( Vec3(-90,0,0) );
      bg->setPosition3D( Vec3(visibleSize.width/2,0, lastZ) );
      bg->setCameraMask( (unsigned short)CameraFlag::USER2 );
      mlayer->addChild( bg );
      lastZ = bg->getPositionZ() - height;
      mBackgrounds.push_back( bg );
   }

   scheduleUpdate();
   return true;
}

void LevelsScene::update(float dt)
{
    mMainCamera->setPosition3D( mMainCamera->getPosition3D() +
                            Vec3( 0, 0, -10) );
    for( std::vector<Sprite*>::iterator bgit = mBackgrounds.begin();
     bgit != mBackgrounds.end(); bgit++ )
   {
       Sprite* bg = (Sprite*)*bgit;
       if( bg->getPositionZ() > mMainCamera->getPositionZ() )
       {
           bg->setPositionZ( bg->getPositionZ() - height * count );
       }
   }
}

hope this help :)

Upvotes: 1

Related Questions