How does CCCamera work?

How does CCCamera work?
0.0 0


I am trying to relocate the camera’s distance from a scene to make it fit the screen without having to scale each image. I tried the following from my Game object, which subclasses CCLayer:

this->getParent()->getCamera()->getCenter(x, y, z); this->getParent()->getCamera()->setCenter(*x, *y, *z + 10);

I also tried changing the x and y values, but each time, everything just disappeared.


oh, no, you can’t add a such large number: 10.
you can play with this

    float x=0, y=0, z=0;
    this->getCamera()->getCenterXYZ(&x, &y, &z);
    this->getCamera()->setCenterXYZ(x, y+0.0000001, z);

Have fun :slight_smile:


wow, that’s a small number. thank you!


I tried playing with the z value, but I noticed that the image would mirror itself, rather than zoom out. I need it to zoom out.


Try this

    float x=0, y=0, z=0;
    this->getCamera()->getEyeXYZ(&x, &y, &z);
    this->getCamera()->setEyeXYZ(x, y, 1000);

It works for your requirement.
You can search the GLES document for gluLookAt function. It does the magic behind CCCamera.


oh… I made the mistake of thinking that center was the camera, and eye was where the camera was pointed. hehehe, thanks again!


If you’d like to follow an object, you’re better off by using CCFollow :slight_smile:


Actually, I have this pool table, and I just wanted the camera to zoom out so that the table could be seen in its entirety. I figured that if I used the camera, everything else would scale automatically. Then I found out I could just add the balls to the table, and they would scale automatically.


How I can turn a screen point to its equivalent in the
I have a sprite that is painted outside the texture, and I want to keep to a position within the texture.

EDITED: I’ve added two images.
In “eyexyz.png”, the sprite without setEyeXYZ ().
In “eyexyz2.png” with the setEyeXYZ (x,–0.0000001f,z).

I need the algorithm to calculate the position of any point inside the texture.
The upper left corner is (0,149) (anchor [0,1]).
In the Sprite this point is (61,126).

I’ve tried a lot of things, but none solved the problem.

Please, help!


Zhe Wang wrote:

Try this
float x=0, y=0, z=0;
>getCamera()->setEyeXYZ(x, y, 1000);

this Code is for Zoom out but i need to Zoom-IN? plz help


Zoom In using CCCamera…. this code will work fine in cocos2dx

float x=0,y=0,zxs=300;
>getCamera()->setEyeXYZ(x, y, zxs);


how this can be done for Cocos2dx v. 3.4?
I tried following code but image is disappearing, I am trying to understand new Camera class in v3 but not able get it work.

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto camera = Camera::createPerspective(10, visibleSize.height/visibleSize.width, 1, 1000);
camera->setPosition3D(Vec3(0.0f, 0.0f, 0.0f)); // these values I am not able to figure out
camera->lookAt(Vec3(1.0f, 1.0f, -1), Vec3(0.0f, 1.0f, 0.0f));// these values I am not able to figure out

then for the default coco image

    // add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
sprite->setCameraMask((unsigned short)CameraFlag::USER1);

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(sprite, 0);

but it not working out, image is not visible on screen.
Any help or guidance will be helpful.



Just rotate the image, use setRotation3D


@paresh maybe this will help.

This sets the x,y,z of where the camera is in your world, just like positioning a camera that you could hold in the the real world.

The objX,objY,objZ are the object that the camera is looking at.
The second vector, upX,upY,upZ define effectively what the normalized up vector of the camera is. The default values most often used is 0.0,1.0,0.0, making the the positive Y axis point up on your screen.
camera->lookAt(Vec3(objX,objY,objZ), Vec3(upX,upY,upZ))

Another important thing to understand is the frustum you create with the following method. Do some research to see how an OpenGL frustum works. It will help a lot.
auto camera = Camera::createPerspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane);

The director defaults for this where size is the screen size in pixels:
Camera::createPerspective(60, (GLfloat)size.width/size.height, 10, zeye+size.height/2)

The above default sets a field of view of 60 degrees, an aspect ratio of screen width / screen height, a near plan of 10 pixels and a far plane of zeye+size.height/2. Not 100% how zeye is determined initially.

But, as an example to learn from, you could set the near plane to be 10 and the far plane to be 10,000.
Then you could put game objects in your world, maybe along a background plane of z = 0 and use the setPosition3D to position the camera and you can then zoom the camera in and out by adjusting the z value of the camera in the camera’s setPosition3D method. If your camera is more than 10,000 (the far plane setting) away from what it is looking at (i.e z=-11,000), the camera will not see what you tell it to look at. So your effective zoom range would be maybe from z=-9500 to z=-1000, depending on how far you want to zoom in and out on your 2d world. If the camera zooms in past the z=0, then the mirror image along the up axis will be seen (i.e camera z value set to +2000).

Sprite disappears!

I am not able to access getcamera option is there any problem with it ?