Moving Camera/Collision

Ahem. Good afternoon my fellow developers. I have a very important question, which hopefully can be solved.

For my game, I have a camera which moves with the arrow keys, and it works well; however, the collision objects for my sprites do not stay with the actual objects. When the camera moves, they remain in the same position in the camera view, rather than stay fixed onto the game object like they should. How can I fix this?

Thanks!

Can you please provide more information? Screenshots of your setup is usually good to see.

So this is the main screen. The game is heavily inspired by Europa Universalis, if you’re familiar with those games. Now, if I press the arrow keys, I can move the camera, like so:

But the thing is, I use collision objects to register if a sprite/gameobject has been “clicked,” like this:

04%20PM

But when the camera is moved, the sprites move across the screen and whatnot, but the collision objects remained fixed in the same position on the screen (so if the collision box was in the middle of the screen, no matter where you moved, the collision box would always register in the middle of the screen, even if its sprite was no longer visible on screen).

Are you moving the camera or are you moving everything else? If you’re moving the game objects and the collision objets are static then those will stay in place while the sprites and other components follow the game objects.

I just made a simple camera script.

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post("#camera", "acquire_camera_focus")
end

function on_input(self, action_id, action)
	local cam = go.get_position("camera")
	if action_id == hash("right") then
		go.set_position(cam + vmath.vector3(10,0,0), "camera")
	end
	if action_id == hash("left") then
		go.set_position(cam + vmath.vector3(-10,0,0), "camera")
	end
	if action_id == hash("up") then
		go.set_position(cam + vmath.vector3(0,10,0), "camera")
	end
	if action_id == hash("down") then
		go.set_position(cam + vmath.vector3(0,-10,0), "camera")
	end
end

Can you please provide screenshots of your setup showing where the collision objects components, camera and sprites are in the game object hierarchy?

The sprites are under the “provinces” game object. You can also see the collision objects (I forgot to show the sprite for the little army dude, but same concept applies).

Is your render script custom? You could share the project with someone on Defold team might get a solution faster.

Check out https://www.defold.com/community/projects/58981/ for drawing collision shapes closer to the province shape.

Feel free to share with me (bjorn.ritzl@king.com)

Thank you! And is there any render script code you do recommend?

I’d recommend to use either Defold-Orthographic or RenderCam (both in the Asset Portal). Both are camera solutions with accompanying render scripts that support zoom, projections etc. Defold-Orthographic is 2D only while RenderCam supports both 2D and 3D projections.

2 Likes