How to use new Camera system?

How to use new Camera system?
0.0 0

#1

Hi,I’m now develop game runner and trying to implement new camera system.
I’m using this Camera with following code

    _camera = Camera::create();
    this->addChild(_camera);
    this->setCameraMask(1);

And I use this to move the camera.

_camera->setPositionX(_camera->getPositionX()+(dt*30));

Now, if I do this, the HUD Layer, with scene as parent and Main Layer as siblings, is also moving.
How to make HUD Layer static? Earlier version will work with this concept. How to implement it correctly?


Box2d problem with perfomance
#2

How it works? I’m so confused. It’s really different this new system.

girl = Sprite3D::create(filepath);
girl->setCameraMask((unsigned short)CameraFlag::USER1);
this->addChild(girl)

camera = Camera::create();
this->addChild(_camera);
camera->setCameraFlag(CameraFlag::USER1);
camera->lookAt(girl->getPosition3D(),Vec3(0,0,0));

Here it’s still does nothing. I just want a simple 2d Camera.


#3

You should set camera eye position at first.

A simplest example would be as follow:

    auto sprite = Sprite::create("HelloWorld.png");
    auto spritePos = Vec3(visibleSize.width/2 + origin.x,
                        visibleSize.height/2 + origin.y,
                        0);
    
    // position the sprite on the center of the screen
    sprite->setPosition3D(spritePos);
    //this is the layer, when adding camera to it, all its children will be affect only when you set the second parameter to true
    this->setCameraMask((unsigned short)CameraFlag::USER2, true);
    // add the sprite as a child to this layer
    this->addChild(sprite);
    
    auto camera = Camera::createPerspective(60, (float)visibleSize.width/visibleSize.height, 1.0, 1000);
    camera->setCameraFlag(CameraFlag::USER2);
   //the calling order matters, we should first call setPosition3D, then call lookAt.
    camera->setPosition3D(spritePos + Vec3(0,0,800));
    camera->lookAt(spritePos, Vec3(0.0,1.0,0.0));
    this->addChild(camera);

CCTexture2d getName access violation
#4

thanks, with this, I’m able to move the camera, without moving HUDLayer :smile:


#5

In fact you’d better have two cameras,one is HUD-Layer-Camera(you can use default camera), another one is Main-Layer-Camera(or scene-camera). the object who is in the default camera will be drawn at the end, if you moved your Main-Layer-Camera you will not affect Main-Layer’s object.


Particles Follow my vehicles Velocity ? Why?
#6

Doesn’t the code above has give me two camera? CameraFlag::USER2 and CameraFlag::Default? I don’t have to specify the default camera do I?


#7

default camera is created at Director::setProjection()


#8

I see, so we can have multiple camera, and each of them give their point of view to the screen. That’s very convenient.

Thanks :smiley:


#9

This just moves camera top sprite, if i move sprite, camera doesn’t move. How to make camera follow sprite ? Thanks dude


#10

I’ve tried using it, and I set camera->setPositionX(40) and there are now 2 of my characters… the camera cloned him.

Also, should my main view be a Scene?


#11

I had same issue, complete duplication all all scene sprites that follow camera. Crazy :frowning:
I’m just hoping the issue vanishes when i go back