Trouble with camera. How to change values of default camera

I know the default camera is perceptive camera. I want to implement parallax on Z-axis. Imagine this -

I want to implement thisscene - A guy walking towards me and sprites spawning randomly. And road seeming going backward. Simple gameplay ( Kind of 2-d Temple run ) ?

I want a perspective view. I used camera. But when I add camer, Two views are created.

When I try to mask default camera. It compiles but crashes. Suggestions needed fast !

I think you’ve asked a question without trying very hard to fix the problem. Also your explanation of what you’re trying to achieve is vague at best.

@UKDeveloper99

UK developer. Thanks for showing interest. But I think the problem is pretty much mentioned.

I need a perspective view. Tried using Parallax on Z-axis. Failed. Not failed but wasn;t realstic.

Studied camera. Applied. Two views were created. When I tried to mask the default camera. Game crashes ! What’s there not to understand ?

The last sentence made absolutely no sense at all.
You mask the sprites not the camera.
So you mean you made 2 cameras? Why do you need 2 camera’s for?
Game crashes, maybe link the errors or a screenshot? Simple enough?

@ UKDeveloper99

I don’t need 2 camera’s. I need a perspective camera. I create one. It total makes 2 cameras.

  1. Deafult
  2. The one I created.

I want to eliminate/shut/mask default camera as there are two view then.

And the new camera I created, I want to work with that. And disable default one. I’m not able to accomplish that. And unfortunately there are no good sources on internet to understand this.

Is it helping ?

Here, I want to apply z-axis scrolling like temple run without using 3-D. Like road scrolling in perspective view.

How do we accomplish that ?

With camera or without camera ?

Okay starting to make some sense now.

I’ve never tried to remove the default camera myself. People tend to put their UI stuff on the default camera. You could use it for that. Basically when you set the camera mask for any sprites that you want to draw, those sprites will only be visible from the view of that camera. So if you set all the sprites to use the camera mask of your new camera (e.g. Camera1). Then it will effectively be the same as having no default camera. It exists in memory but no sprites are drawing on the default camera, it’s unused.

In regards to your other question. There are probably a few ways to achieve that affect. If you want to use z-axis with that 2D background, you will run in the problem where as you move the sprites backwards on the z-axis they will go behind the background and out of sight. You can scale the sprites and move them over time toward the middle of the screen. Without knowing the exact details of the game it’s hard to say.
If you want to go ahead with the 3d approach. Then it is possible to draw the background to a separate render layer and everything else to another layer. Then put the two together. Try making the game without the background first.

@VineetAvasthi Please don’t tag users in the subject of a post. It is inconsiderate to that person’s time and conveys a sense of urgency that isn’t there.

I also think that at @UKDeveloper99 is providing great advice. A perspective camera is the default camera that is created. You mask objects, not the camera itself.

@UKDeveloper99

Okay, I got the concept of masking now. You let’s say I create a new perceptive camera. There’s a sprite called abc.png. Now, with respect to default camera, that sprite can be masked and with respect to other camera, It should be visible ? Am I right ?

Also the default camera is perspective, How can I just make changes in it. Like getting the default camera and change the values of X,Y,Z rather than creating a new camera and masking things ?

@slackmoehrle

I didn’t know. I’ll not repeat it in future.

Try this,

Camera *cam = Camera::getDefaultCamera();
cam->setPosition3D(Vec3(X, Y, Z));

You can always search forum, or check out cpp-tests

Nope, That didn’t work. Tried chaning values. But no results.

My suggestion :

  • Use default camera as it is for your HUD layer UI/Menu components.

  • Instantiate a new layer, add a new camera (set its position, FOV as per your need) to it and set camera mask for the new layer. Whatever gets rendered on the new layer is fully controlled now as you can change the camera as per your need.

This is required as i understand you want to do a scrolling in Z axis ?

@kiddev

Yes. Can you give an example ? I’m a beginner and there are no good sources. Cocos documentation sucks !

How would yo instantiate a new layer ? And add camera to it ?

Please appreciate whatever you are getting for free. Cocos documentation is good enough for anyone to start and get hold of things. You can start learning from there.

Here is a snippet for your help, try to understand it:

Layer* newLayer = Layer::create(); // Add this layer to your scene

Camera* newCamera = Camera::createPerspective(60, (GLfloat)winSize.width/winSize.height, 1, 1000);
newLayer->addChild(newCamera);

newCamera->setCameraFlag(CameraFlag::USER1);
newCamera->setPosition3D(Vec3(0, 2, 2)); // This is just some random position i have put here.

newLayer->setCameraMask((unsigned short)CameraFlag::USER1);

As the author of a lot of our documentation, tell me where it sucks, specifically, please.

@slackmoehrle

Don’t take it personally but it’s really hard finding pieces of your framework. I’m not judging the framework. I’m just saying it comes with not very good user manual. I don’t know from where to read about camera, There was a page previously but even that wasn’t providing sufficient information. Also, it’s not been updated regularly.

rather hard not to take it personally when you outright say it sucks.

So tell me me where There was a page previously but even that wasn’t providing sufficient information

Its working fine for me.
You should dig into more.
Try to see other methods like lookAt of camera
Also you will find Camera3DTest in cpp-tests
Take help of CCLOG print before changing values and after changing values.

2 Likes