Camera and aspect ratio

If we use the camera and fix they aspect ratio by script or checkbox in the camera properties panel we are having correct camera’s zoom only if screen.width>screen.height (0:00-0:20)

So we need correct the camera zoom for portrait screens, where screen.width<screen.height:
in video: 0:45-1:10

My code for this (see windowevent function):

local campos = vmath.vector3()
local nearz = .1
local farz = 2000
local initial_z = 680

function init(self)
	msg.post("#camera", "acquire_camera_focus")
	campos=go.get_position()
	campos.z=initial_z
	go.set_position(campos)
	msg.post("#camera", "set_camera", {aspect_ratio = 1.0, fov = 45, near_z = nearz, far_z = farz})
	window.set_listener(windowevent)
	windowevent(self,window.WINDOW_EVENT_RESIZED, {width=render_helper.window_res.x,height=render_helper.window_res.y})
end

function final(self)
	msg.post("#camera", "release_camera_focus")
end

--########################################  Window Event  ########################################
function windowevent(self, event, data)
	if event == window.WINDOW_EVENT_RESIZED then
		print("Window resized: ", data.width, data.height)
		msg.post("#camera", "set_camera", {aspect_ratio = data.width/data.height, fov = 45, near_z = nearz, far_z = farz})
		if data.width<data.height then
			campos.z=initial_z*data.height/data.width
			go.set_position(campos)
		else
			campos.z=initial_z
			go.set_position(campos)
		end
	end
end

Is it right or I’m missing something in camera initialization, project settings, etc?

2 Likes

UPD.
this camera zoom script works nice at any resolution:

but now I got new issue with blink some sprite (z-order of card’s back sprite is -0.1, hidden sprite -shield has 0.0) :smiley:

p.s perhaps I need to rename this topic to dev diary )

2 Likes

What camera you are using for ur game? ortho or prespective? How are you zooming and focusing the screen centre on HTML5 build…

this is exactly what I’ve been searching for, but I’ve copied this script to my camera’s script and got

ERROR:SCRIPT: /main/cam/cam.script:13: attempt to index global 'render_helper' (a nil value)

I guess I’m missing another step, may I ask for a beginner friendly instruction of using this camera script please?

render_helper - this module contains game window resolution in table variable “window_res”. And init script use it for beginning initialisation. You can change it to your own variables.

well I’ve just started with defold and I don’t have any idea what should my variable be for this one, should I just define a variable with that name or there’s more to it?

@ddtnfm Another option would be one of the already available camera solutions on the Asset Portal.

oh, I didn’t knew about the asset portal, thanks :slight_smile:

1 Like

Yeah, we probably need to push for and link to it more @Axel and @samuel.nystedt. Top level links instead of hidden under community.

2 Likes

Yep, agreed! We’re booting up post-holidays, but this is one of the things we plan to do soon.

2 Likes