Make the camera follow multiple characters

Currently, I use the following code to make the screen follow the main character:

				//focus is the entity the camera should focus on.
				float focusX = focus.currX + focus.width  / 2,
					   focusY = focus.currY + focus.height / 2;
				
				//visibleWidth and visibleHeight is the size of the container/window.
				//width and height is the size of the entire map
				translateX = Math.min(stage.width  - stage.visibleWidth,   Math.max(0, focusX - stage.visibleWidth  / 2)); 
				translateY = Math.min(stage.height - stage.visibleHeight,  Math.max(0, focusY - stage.visibleHeight / 2));
				//scaleX and scaleY are untouched

This code works fine as long as there is one entity to focus on.

I want a code that supports 2-4 objects. I have seen games that have it. Basically, when the entities are to far from each other, the scaleX and scaleY are decreased to zoom out.
But I am not sure how to code this.

Some help?

I would take the distances between all Objects and see which of them is the biggest, then scale the whole scene for that distance (or a bit higher so that both characters would still fit on the screen without getting cut in half) and position it so that the character with the lowest x value is at the left side (for example allways 50 px away from the end of the visible display) and the character with the heighest x value is at the right side of the screen (again with 50 px distance to the end of the display) and do the same with the y coordinates (but maybe try to keep the relations of the Screen scaling the same cause it’ll look weird otherwise)


			// Camera Setup
			// X
			float minx = e1.x, maxx = e1.x;
			if (e2.x < minx) {
				minx = e2.x;
			}
			if (e3.x < minx) {
				minx = e3.x;
			}
			if (e4.x < minx) {
				minx = e4.x;
			}
			if (e2.x > maxx) {
				maxx = e2.x;
			}
			if (e3.x > maxx) {
				maxx = e3.x;
			}
			if (e4.x > maxx) {
				maxx = e4.x;
			}
			float difx = (maxx - minx) + 100;

			GL11.glScalef(800 / difx, 800 / difx, 1);
			GL11.glTranslatef(-minx + 50, 0, 0);

Does the Job for 4 Entities (Screen 800x600) but this is only based on x coordinates, so none of the entities can go out of the screen left or right, they still can go out top or bot. Called before rendering the entities

Time to implement this.

Seiya02: Do you know how to use your code with OrthogonalCamera(LibGDX)?
Also, to add support for an Y axis, just copy the code and change the x variables with y?

I’m not working with LibGDX sorry :s
and to add y support you should check if x or y is needed for the scale (if you scale with x and they have a huge difference on y they won’t be visible so you have to scale with y)

and the scaling with y is as you said: just change x to y :slight_smile: