Camera bug when moving?

Edit moved to C++ as this is happening with non-Cocos Studio sprites.

I made a test project with 50 sprites, most of which are off the screen to the right side of the layer.

When I move the scene camera to the right, the sprites on the right are not visible. However, if I move the rootNode to the left, then the sprites are visible.

Is this a bug or does something else need to be done to use the camera?

Sounds like a bug, which version of cocos2d-x are you using?

Can you create a issue for this? It will help us make sure it’s fixed.

@nite maybe this is related to this issues I have mentioned before? cocos2d-x auto-culling or a bug?

@Javy what version of cocos2d-x you use? Could you please post your code as well? Do you use ActionCamera or something else?

I’m using 3.4 final. My code in the init of HelloWorld:

auto spritebatch = SpriteBatchNode::create("MySprites.png");
addChild(spritebatch);
auto spriteCache = SpriteFrameCache::getInstance();
spriteCache->addSpriteFramesWithFile("MySprites.plist");

Then I create the sprites and add them to the layer. In the onEnter:

void HelloWorld::onEnter()
{
    Layer::onEnter();

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Camera *camera = Director::getInstance()->getRunningScene()->getCameras().front();
    auto move = MoveBy::create(35.0, Vec3(10 * visibleSize.width, 0, 0));
    camera->runAction(move);
}

When the app runs, I can see the sprites. But when it scrolls to the right, the other sprites are missing.

I can file a bug, but I want to make sure it is a bug first.

@Javy So this in not the ActionCamera, but the scene camera. I guess, this is a real 3D camera, right?

I’m guessing so. I opened a new issue here:
https://github.com/cocos2d/cocos2d-x/issues/10487

I’ve experienced this issue too in cocos2d-x 3.4. It seems as if culling is not taking into account the camera movement, but only the original screen bounds. (Looking at Renderer::checkVisibility() seems to confirm this. I can’t see the camera being used in the visibility calculations).

For the time being, I’m setting #define CC_USE_CULLING 0 in ccConfig.h as a workaround. (Which by the way, creates a compilation error in Label::draw() since the code references a local variable only defined within the #if CC_USE_CULLING ... #endif block. I juste moved that line above the #if. I guess that’s ok, I haven’t tried any labels).

@ArtCoder thanks for the workaround. I changed my project to moving a few layers, but I might use this fix in future projects if they don’t get to this bug in time. Much easier to move a camera instead of several layers.

Yeah, moving the camera is much more convenient! Plus I’ve tried moving the layer, but it seems there’s some weird behavior when you combine that with physics without autoStep. Apparently (and I haven’t spent much time testing this), since I’m stepping the physics world at a different rate than the scene graph tick, the bodies lose synchronization with the nodes they’re attached to.

This problem still persists in 3.6.

I fixed camera movement in my commit to my pull request. Feel free to use it if you want to move cocos2d::Camera around. In this commit Sprites check for visibility every frame and camera position is being taken into account when checking for visibility.
https://github.com/elvman/cocos2d-x/commit/8f2e35c1415b83928e999895a322f98cecbdc903

2 Likes

I see, that cocos2d-x team has fixed the visibility checking code (Camera::getVisitingCamera()->projectGL). But the Sprite’s and Label’s visibility is still checked only if the FLAGS_TRANSFORM_DIRTY flag is set. Could you please remove the dirty check, so that labels and sprites are visible after the camera has been moved?