在cocos2d-x V3.10中实现Z轴上的视差

时间:2016-06-30 14:54:39

标签: cocos2d-x cocos2d-x-3.0 cocos2d-x-3.x

我使用cocos2d-x的时间不长。它很有趣,很容易掌握。但最近,我在实施某些事情时陷入困境。 (现在已经有一段时间了)。

我在X和Y轴上实现了Parllax。这是一件轻松的工作。但是当我尝试在Z轴上实现它时(想象一下透视图),我就是不能。Imagine this view

请提出建议。将非常感谢cocos2d-x的上下文解释。我在C ++中使用它。谢谢!

1 个答案:

答案 0 :(得分:1)

我在Z轴上编写了无限滚动背景的代码 最重要的提示是我计算背景图块之间的距离。我使用背景图像的高度作为瓷砖之间的距离,但在我围绕X轴旋转精灵之前存储它。

以下是头文件( 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
...

LevelScene.cpp 文件包含:

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 );
       }
   }
}

希望这有帮助:)