Setting up camera to follow

Hi,

I am new to cocos2d-x and I am having trouble setting up the camera to follow a node. I am using 3.0rc0

    Size visibleSize = Director::getInstance()->getVisibleSize();

    auto sprite = Node::create();
    auto circlebody = PhysicsBody::createCircle(50 / 2);
    sprite->setPhysicsBody(circlebody);
    sprite->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    this->addChild(sprite);

    auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    auto edgeNode = Node::create();
    edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    edgeNode->setPhysicsBody(body);
    this->addChild(edgeNode);

    this->runAction(Follow::create(sprite));

Thanks in advance for your help!

1 Like

Same problem…
try to log the sprite’s position in update.
it print strange number…

…
480.000000,-6584051.500000
480.000000,-6584983.500000
480.000000,-6585915.500000
480.000000,-6586847.500000
480.000000,-6587779.500000
…

I think some physics body’s position is problem.

I really need to follow sprite as camera is panning.
It was really easy when I used version 2.2x, by getCamera()
so I’ve been easily panning and zooming camera.

How can I do that? in 3.0rc1

What is happening is that when the camera (the Layer) shifts position, the sprite is shifting as well and so the follow is rendered useless.

In my cocos2d-iphone project, I am able to have the sprite centered with the following

        _physicsWorld = [CCPhysicsNode node];
...
        [_physicsWorld addChild:_sprite];
        [_physicsWorld runAction: [CCActionFollow actionWithTarget:_sprite]];

That works, and [self _runAction:] (Scene) works as well. Can anyone shed some light?

Same problem…

same problem :frowning:

Same problem…

I want to move a sprite on top of a static physics body. facing the same issue. physics bodies are not taken by Follow i suppose. I tried using ActionCamera but the physics body just stays work. Is there any way to get Follow like effect or Camera effect like hill climb game on any physics Sprite?

Come up with a temporary idea, Move the entire scene. Then all will move along with it. sad part is that everything else is also moved, including buttons hehe… Move the running scene only when your physics sprite is moved.

I’m using cocos2d-3.14.1. I think it’s necessary to add a physical body to the sprite.
sprite->addComponent(physicsBody);
It worked for me. You can also try add Node to the sprite sprite->addComponent(edgeNode);, but I did not try.
Also, do not add a physical body with this->addChild(edgeNode);

1 Like