Why adding condition in IF change camera pos ?!

Hi,
I’m looking for changing camera pos, but there is a problem.
The game window is smaller than the screen (1920 vs 2560). And the map is… far bigger than the two.
I can move the map easily with the keys. Works well (in the camera script) :

	local position = go.get_position()
	local zoom = go.get("#camera","orthographic_zoom")
	-- moving camera with the keyboard
	if action_id == hash("right") then
		position.x = position.x + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("left") then
		position.x = position.x - (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("up") then
		position.y = position.y + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("down") then
		position.y = position.y - (5 / zoom)
		go.set_position(position)
	end

When I want to drag the screen with the mouse, I do :

		if action.x and action.y then
			local pos = vmath.vector3(action.x, action.y, 10)
			go.set_position(pos)
		end

Work well, too. even with the zoom.

But if I add some condition to the IF, like that :

		if action.x and action.y and (action_id == hash("wheelclick") or action_id == hash("e")) then
			local pos = vmath.vector3(action.x, action.y, 10)
			go.set_position(pos)
		end

Then the camera jump at another point, then works correctly. I first tough it could be the zoom, and still think, but it’s pretty hard to say
“e” is used only there.

Any idea how to make it to “not jump” ? And if somebody knew how to separate system and game cursor, it would help me, too.

For ref, here’s the zoom function :

	if action_id == hash("wheelup") and self.wheel < 1 then
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 1})
		zoom_state = zoom_state + 1
		if zoom_state > zoom_max then
			zoom_state = zoom_max
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	elseif action_id == hash("wheeldown") and self.wheel < 1  then
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 2})
		zoom_state = zoom_state - 1
		if zoom_state < zoom_min then
			zoom_state = zoom_min
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	end

And in Curseur.script :

In on_message

	if message_id == hash("wheel") then
		Wheel_Zoom(message["wheel"]) 
	end

and

function Wheel_Zoom(wheel)

		if wheel == 1 and zoom_state <= zoom_max then
		wheel = 0
		for i, id_instance in ipairs(systeme) do
			tmp = lua_lire_systeme(i,"id_instance")
			if zoom_state > 5 then
				sprite.set_constant(tmp.."#sprite_close", "tint", vmath.vector4(1, 1, 1, 1))
				sprite.set_constant(tmp.."#sprite", "tint", vmath.vector4(1, 1, 1, 0))
			else
				sprite.set_constant(tmp.."#sprite_close", "tint", vmath.vector4(1, 1, 1, 0))
				sprite.set_constant(tmp.."#sprite", "tint", vmath.vector4(1, 1, 1, 1))
			end
			go.set(tmp.."#sprite", "scale.y", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite", "scale.x", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite_close", "scale.y", 0.25*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite_close", "scale.x", 0.25*(math.pow(zoom_gap, -zoom_state)))
			go.set("/curseur#sprite", "scale.x", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set("/curseur#sprite", "scale.y", 1*(math.pow(zoom_gap, -zoom_state)))
		end
	elseif wheel == 2 and zoom_state >= zoom_min then
		wheel = 0
		for i, id_instance in ipairs(systeme) do
			tmp = lua_lire_systeme(i,"id_instance")
			if zoom_state < 6 then
				sprite.set_constant(tmp.."#sprite_close", "tint", vmath.vector4(1, 1, 1, 0))
				sprite.set_constant(tmp.."#sprite", "tint", vmath.vector4(1, 1, 1, 1))
			else
				sprite.set_constant(tmp.."#sprite_close", "tint", vmath.vector4(1, 1, 1, 1))
				sprite.set_constant(tmp.."#sprite", "tint", vmath.vector4(1, 1, 1, 0))
			end
			go.set(tmp.."#sprite", "scale.y", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite", "scale.x", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite_close", "scale.y", 0.25*(math.pow(zoom_gap, -zoom_state)))
			go.set(tmp.."#sprite_close", "scale.x", 0.25*(math.pow(zoom_gap, -zoom_state)))
			go.set("/curseur#sprite", "scale.x", 1*(math.pow(zoom_gap, -zoom_state)))
			go.set("/curseur#sprite", "scale.y", 1*(math.pow(zoom_gap, -zoom_state)))
		end
	end

	-- Signal to camera.script that Cursor changed stuff from zoom
	msg.post("/camera", "wheel_retour", {wheel_retour = 0})	

end

Edit : Spelling

Could you please show the complete on_input function with this condition added?

There the on_input function of the camera script :

function on_input(self, action_id, action)

	-- get camera position
	local position = go.get_position()
	local zoom = go.get("#camera","orthographic_zoom")


	-- moving camera with the mouse
	
		if action.x and action.y and (action_id == hash("wheelclick") or action_id == hash("e")) then
			local pos = vmath.vector3(action.x, action.y, 10)
			go.set_position(pos)
		end
	
	-- moving camera with the keyboard
	if action_id == hash("right") then
		position.x = position.x + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("left") then
		position.x = position.x - (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("up") then
		position.y = position.y + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("down") then
		position.y = position.y - (5 / zoom)
		go.set_position(position)
	end

	-- zooming
	if action_id == hash("wheelup") and self.wheel < 1 then
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 1})
		zoom_state = zoom_state + 1
		if zoom_state > zoom_max then
			zoom_state = zoom_max
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	elseif action_id == hash("wheeldown") and self.wheel < 1  then
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 2})
		zoom_state = zoom_state - 1
		if zoom_state < zoom_min then
			zoom_state = zoom_min
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	end
	
end

I would start by making everything a single block of if-elseif-end instead of three different sections of conditional checks.

Done. The orders are obeyed smoother, but the mouse still jump

	if action.x and action.y and (action_id == hash("wheelclick") or action_id == hash("e")) then
	-- moving camera with the mouse
		local pos = vmath.vector3(action.x, action.y, 10)
		go.set_position(pos)
	elseif action_id == hash("right") then
	-- moving camera with the keyboard
		position.x = position.x + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("left") then
		position.x = position.x - (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("up") then
		position.y = position.y + (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("down") then
		position.y = position.y - (5 / zoom)
		go.set_position(position)
	elseif action_id == hash("wheelup") and self.wheel < 1 then
	-- zooming
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 1})
		zoom_state = zoom_state + 1
		if zoom_state > zoom_max then
			zoom_state = zoom_max
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	elseif action_id == hash("wheeldown") and self.wheel < 1  then
		self.wheel = 1
		msg.post("/curseur#curseur", "wheel", {wheel = 2})
		zoom_state = zoom_state - 1
		if zoom_state < zoom_min then
			zoom_state = zoom_min
		end
		go.set("#camera", "orthographic_zoom", 1*(math.pow(zoom_gap, zoom_state)))
	end

I suggest that you print what action.x and y is here. It sounds like the values might be 0,0

Found something that seem to work there. Weirdly enough, it’s not far from what I tried first, but without the Jump. I probably messed between the screen and word coordinates :

https://github.com/britzl/publicexamples/blob/master/examples/drag_to_scroll/drag_to_scroll/drag_to_scroll.script

Modified like that :

	local position = go.get_position()
	local zoom = go.get("#camera","orthographic_zoom")

	if action.pressed  and (action_id == hash("wheelclick") or action_id == hash("e")) then
		self.wheelclick = true
		self.wheelclick_pos = vmath.vector3(action.x, action.y, 10)
		self.wheelclick_camera_pos = go.get_position()
	elseif action.released then
		self.wheelclick = false
	end
	
	if self.wheelclick == true then
		-- moving camera with the mouse
		local pos = self.wheelclick_camera_pos + (self.wheelclick_pos - vmath.vector3(action.x, action.y, 0)) / zoom
		go.set_position(pos)
	elseif  *other actions*

Now, going to make the zoom pinpoint the cursor position (I WILL start the game, at some point. Yep. Sure. Totally. Stop looking at me).

Ok, good to hear that you solved it!

1 Like