Change camera Z position

How do I change the z position of the camera.
I made a script for camera, changing the X and Y position were fine. But if I change the Z value then it will render a black screen.
I need to change the Z position because I need it zoom out a little bit.
Please help, thanks

If you are using an orthographic camera (which is in the default render script) changing Z will not impact in a zoom way whatsoever. An orthographic camera will have the exact field of view independent of the Z position. The reason you are seing a black screen is probably because the near and far render is -1 and 1.

What you want to do is either:

  1. Switch to a perspective camera.

  2. Fiddle with the set_projection values (what will be projected on screen). You could do something like this:

    render.set_projection(vmath.matrix4_orthographic(
    -render_w * cam.scale + cam.pos.x,
    render_w * cam.scale + cam.pos.x,
    -render_h * cam.scale + cam.pos.y,
    render_h * cam.scale + cam.pos.y,
    -1, 1))

On the above cam is a table carrying the position of the camera and also the zoom scale. Render_w is half of the full render width which will make the cam position appear in the CENTER of the screen. It’s just an example and I think you can do a lot with your things with this. I have personally found that it’s easier to work with different screen ratios if the camera is in center instead of bottom left corner.

3 Likes

Thanks so much, but how to switch to a perspective camera. I can’t find it anywhere in Defold document, not much information in that.

Isn’t it supposed to be vmath.matrix4_perspective?

Or rather, if you have a camera component you should store its projection as received in the render script and use that instead. This is described in the camera manual.

I created an example that sets up a perspective camera with a custom render script that uses the projection provided by the camera.

CODE: https://github.com/britzl/publicexamples/tree/master/examples/perspective_camera
DEMO: http://britzl.github.io/publicexamples/perspective_camera/index.html

2 Likes

Thanks britzl, I work with it

:slight_smile: Nah, this was if he preferred Orthographic so you just shrinked the orth projection view. Ah I actually totally forgot about the camera component (!) as I’m mostly create my own camera solution.

1 Like

Ah, sorry, I misunderstood.