Sign in to follow this  
P0jahn

[2D]Make a camera follow multiple characters.

Recommended Posts

P0jahn    307

I am developing a 2D game(platformer) where to top-left corner is x:0,y:0. As you move rightwards, the x coordinate increase and y increasing when moving downwards(in other words, standard coordinate system for 2D games).

 

As in every platformer, the camera is focused on the main character, following her/him. Some games however have more than one main character. In these games, the camera would zoom out if the characters are to far away from each other and at the same time "follow" them.

 

I need to implement this in my game but not sure how. It need to support 1-4 characters.

 

The variables translateX, translateY and scale(where 1.0 is the normal size of the world) need to be set.

 

Language can be either Java, C#, C, C++ or anything that look alike.

Edited by P0jahn

Share this post


Link to post
Share on other sites
P0jahn    307

Thanks for the help.

 

I guess the bounding box is the total size of a stage?

 

How do I compare the aspect ratio? Sorry, bad at math.

Share this post


Link to post
Share on other sites
HappyCoder    5053

The bounding box is the smallest box that contains a list of points. You can represent a bounding box by the location top, bottom, left, and right edges. The top and bottom only need to store a y coordinate value, right and left store an x coordinate. To calculate the bounding box, you loop through all points and for each one, you extend the bounds of the box if the point isn't contained in the bounding box. However, the first point in the list needs to initialize the bounding box.

 

 

Calculate the bounding box

boundingBox.left = points[0].x
boundingBox.right = points[0].x
boundingBox.top = points[0].y
boundingBox.bottom = points[0].y

// set i to 1 to skip the first point since it was used to initialize the bounding box
for (int i = 1; i < points.count; ++i)
{
   if (points[i].x < boundingBox.left)
      boundingBox.left = points[i].x

   if (points[i].x > boundingBox.right)
      boundingBox.right = points[i].x

   if (points[i].y < boundingBox.top)
      boundingBox.top = points[i].y

   if (points[i].y > boundingBox.bottom)
      boundingBox.bottom = points[i].y
}

The aspect ratio is just the width divided by the height. where the width of the bounding box is boundingBox.right - boundingBox.left and the height is boundingBox.bottom - boundingBox.top

Edited by HappyCoder

Share this post


Link to post
Share on other sites
Dragonsoulj    3212

I feel the bounding box should fit all the characters with that bit of padding around all sides so they are not on the edge. Fit the largest dimension (width or height) of the bounding box to fill the screen's matching dimension (width or height) -- this is your zoom -- and keep your screen's dimensions, expanding the smaller bounding box dimension to match the screen's ratio (this may not be necessary if you just focus on the center of the bounding box and use a zoom factor).

Share this post


Link to post
Share on other sites
Kryzon    4630

How do I compare the aspect ratio? Sorry, bad at math.

Considering the units as pixels, the entire 2D world needs to be scaled so that the biggest dimension of the rectangle that surrounds the characters matches in pixel length the parallel dimension of your game screen.
 
// Calculate the bounding box as demonstrated previously.

// Compute the zoom (world scale) factor based on the biggest dimension of the box.

Local zoomScale:Float

If boundingBox.width > boundingBox.height Then
	
	// The width of the box is larger than the height.
	zoomScale = gameView.width / boundingBox.width

Else
	// The opposite.
	zoomScale = gameView.height / boundingBox.height

EndIf

gameEngine.scaleWorld( zoomScale )
If the biggest dimension of the bounding box is contained in the same dimension of the game screen, the other dimension is also contained since it's shorter.

Share this post


Link to post
Share on other sites
P0jahn    307

Thanks all.

 

The translate part is done but I am a bit stuck on the zooming part.

The framework I am using(LibGDX) is a bit odd regarding the zoom.

1.0 is default zoom(100%). Increasing it actually zooms out instead of in. And decreasing the value zooms in.

			float left 	= focusObjs.get(0).X;
			float right = focusObjs.get(0).X;
			float top 	= focusObjs.get(0).Y;
			float bottom = focusObjs.get(0).Y;
			
			for(int i = 1; i < focusObjs.size(); i++)
			{
				GameObject focus = focusObjs.get(i);
				
				if (focus.X < left)
					left = focus.X;

				if (focus.X > right)
					right = focus.X;
				
				if (focus.Y < top)
					top = focus.Y;

				if (focus.Y > bottom)
					bottom = focus.Y;
			}
			
			float width = right - left;
			float height = bottom - top;
			
			//... Code that sets translateX and translateY
			
			//Set the zoom
                        //visibleWidth and height is the size of the game window.
			if(width > height)
				zoom = stage.visibleWidth / width;
			else
				zoom = stage.visibleHeight / height;

So basically, the zoom value is incorrect. Its around >2 when the entities are close to each other(it should be 1 if they are close).

Share this post


Link to post
Share on other sites
Orymus3    18822

I would've created a ref that makes an average of all character positions (camera focus) and then an identifier of spread to determine the zoom (aka field of view).

These are the only 2 things you should know in order to do it efficiently.

Share this post


Link to post
Share on other sites
P0jahn    307

Try inverting the zoom division, dividing the rectangle width by the stage visible width etc.
It inverts the way that the zoom value changes.

 

Thanks, that worked, for the most part. The zoom works 50% of the time now and I havent had any success resolving it. 

 

I made a short video clip showing the bug. Watch(the camera is focused on the white and red characters):

https://www.youtube.com/watch?v=M_RfzouUk5k&feature=youtu.be

 

As you can see, both of the characters are visible about 50% of the time(sure there are no margin at the moment, I handle that later).

When the glitch occurs, both of the characters disappears(out of focus).

Edited by P0jahn

Share this post


Link to post
Share on other sites
P0jahn    307

Rightsmile.png

			//The bounding box
			float left   = focusObjs.get(0).x;
			float right  = focusObjs.get(0).x + focusObjs.get(0).getWidth();
			float top    = focusObjs.get(0).y;
			float bottom = focusObjs.get(0).y + focusObjs.get(0).getHeight();

			for(int i = 1; i < focusObjs.size(); i++)
			{
				GameObject focus = focusObjs.get(i);
				
				if (focus.x < left)
					left = focus.x;

				if (focus.x + focus.getWidth() > right)
					right = focus.x + focus.getWidth();
				
				if (focus.y < top)
					top = focus.y;

				if (focus.y + focus.getHeight() > bottom)
					bottom = focus.y + focus.getHeight();
			}
			
			float width = right - left;
			float height = bottom - top;

			/* Set the zoom */
			
			if(stage.visibleWidth > width && stage.visibleHeight > height) //Avoid modify the zoom value if the entities are close to each other
				zoom = 1;
			else if(width > height)
				zoom = width / stage.visibleWidth;
			else
				zoom = height / stage.visibleHeight;
			
			/* Translate tx and ty */

			float middleX = left + (width  / 2);
			float middleY = top  + (height / 2);
			float marginX = stage.visibleWidth / 2;
			float marginY = stage.visibleHeight / 2;
			
			if(marginX > middleX)
				tx = marginX;
			else if(middleX > stage.width - marginX)
				tx = stage.width - marginX;
			else
				tx = middleX;
			
			if(marginY > middleY)
				ty = marginY;
			else if(middleY > stage.height - marginY)
				ty = stage.height - marginY;
			else
				ty = middleY;

Note, visibleWidth and visibleHeight is the size of the view, which is 800x600.

Share this post


Link to post
Share on other sites
Kryzon    4630
I'm not sure how that 'margin' and the 'tx' and 'ty' are used, but I would make these (untested) changes:
 
			/* 1) Compute the bounding box. */
			
			float boxX	= focusObjs.get( 0 ).x;
			float boxY	= focusObjs.get( 0 ).y; 
			float boxWidth	= boxX + focusObjs.get( 0 ).getWidth();
			float boxHeight	= boxY + focusObjs.get( 0 ).getHeight();

			for(int i = 1; i < focusObjs.size(); i++)
			{
				GameObject focus = focusObjs.get(i);
				
				boxX = Math.Min( boxX, focus.x );
				boxY = Math.Min( boxY, focus.y );
				
				boxWidth  = Math.Max( boxWidth, focus.x + focus.getWidth() );
				boxHeight = Math.Max( boxHeight, focus.y + focus.getHeight() );
			}
			
			/* 2) Apply a padding so that none of the focus objects are on the edges of the viewport.
			   
			   Probably better to use a 'static final' member of some important class.
			   
			   The padding should be a value in pixels (e.g. '20') of the thickness of 
			   the border that insets from the viewport. */
			
			boxX -= gameEngine.CAMERA_FOCUS_PADDING;
			boxY -= gameEngine.CAMERA_FOCUS_PADDING;
			boxWidth  += gameEngine.CAMERA_FOCUS_PADDING * 2;
			boxHeight += gameEngine.CAMERA_FOCUS_PADDING * 2;

			/* 3) Constrain the padded bounding-box to the stage. */
	
			boxX = Math.Max( boxX, 0 );
			boxX = Math.Min( boxX, stage.width - boxWidth ); 			

			boxY = Math.Max( boxY, 0 );
			boxY = Math.Min( boxY, stage.height - boxHeight );
			
			/* 4) Set the zoom. */

			if ( boxWidth > boxHeight )
				zoom = boxWidth / stage.visibleWidth;
			else
				zoom = boxHeight / stage.visibleHeight;

			zoom = Math.Max( zoom, 1.0f ); // Don't let zoom factor be less than 1.0f.

			/* 5) Final step, compute the center of the box. */

			float middleX = boxX + ( boxWidth / 2 );
			float middleY = boxY + ( boxHeight / 2 );
The priority is making sure that the camera can view every focus object.

Share this post


Link to post
Share on other sites
P0jahn    307

Thanks. The code piece did not work at all first. The bounding box was always wrong, being way to large.

I checked it out and noticed two missing lines after the for-loop:

 

boxWidth = boxWidth - boxX;
boxHeight = boxHeight - boxY;

 

That fixes the bounding box. However, the results are almost identical to the previous code. The bug showed in the video still occurs.

I am suspecting boxWidth > boxHeight is not enough, but dunno.

Edited by P0jahn

Share this post


Link to post
Share on other sites
Kryzon    4630

Thanks. The code piece did not work at all first. The bounding box was always wrong, being way to large.
I checked it out and noticed two missing lines after the for-loop:

boxWidth = boxWidth - boxX;
boxHeight = boxHeight - boxY;
That makes sense.

I would test some more by turning off all sorts of camera constraining being used (such as the one on step no. 3). This should have the camera centering the characters freely right at the start of gameplay.

According to the LibGDX source, the zoom feature of the orthographic camera is linear, it just multiplies the viewport dimensions when setting up the projection:
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/OrthographicCamera.java#L56

This means that the zoom logic is most likely correct.

Share this post


Link to post
Share on other sites
P0jahn    307

I tried removing some code, such as padding and camera camera constraining. No luck.

 

However, I added some debug code after the zoom is set:

if(isKeyPressed(Keys.E))	
	zoom = boxHeight / stage.visibleHeight;

So basically, I pressed 'E' every time the zoom messed up and it was corrected every time. So I am fairly sure that the if-statement boxWidth > boxHeight is the issue.

Not sure how to resolve it though.

Share this post


Link to post
Share on other sites
Kryzon    4630
Alright, try adding the padding and the size constraint back (making everything as it should be), but change that zoom comparison to this:

if ( (float)( boxWidth / boxHeight ) > (float)( stage.visibleWidth / stage.visibleHeight ) )

Share this post


Link to post
Share on other sites
P0jahn    307

EDIT: Actually, the code snippet posted above by Kryzon works fine, thanks again for all the help.

The problem was in the casting. For example, (float)( stage.visibleWidth / stage.visibleHeight), these are two ints that are divided. The results, which is also an int, is then casted to a float, which is wrong.

Edited by P0jahn

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this