Jump to content

  • Log In with Google      Sign In   
  • Create Account


[2D]Make a camera follow multiple characters.

  • You cannot reply to this topic
18 replies to this topic

#1 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 24 August 2014 - 03:47 PM

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, 24 August 2014 - 03:51 PM.


Sponsor:

#2 HappyCoder   Members   -  Reputation: 2565

Like
10Likes
Like

Posted 24 August 2014 - 05:47 PM

First you create a bounding box of your level that contains all of your characters plus a bit of a margin so a character isn't right on the edge.

 

You then compare the aspect ratio of this bounding box to the aspect ratio of the screen. If the bounding box has a larger aspect ratio, then you base the zoom of the camera on screen.width / boundingBox.width, otherwise you use screen.height / boundingBox.height. The center point of the camera is simply the midpoint of the bounding box.



#3 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 26 August 2014 - 09:26 AM

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.



#4 HappyCoder   Members   -  Reputation: 2565

Like
3Likes
Like

Posted 26 August 2014 - 04:21 PM

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, 26 August 2014 - 06:00 PM.


#5 Dragonsoulj   Crossbones+   -  Reputation: 2087

Like
3Likes
Like

Posted 26 August 2014 - 06:09 PM

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).



#6 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 27 August 2014 - 01:10 AM

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.

#7 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 27 August 2014 - 10:33 AM

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).



#8 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 27 August 2014 - 04:56 PM

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

#9 Orymus3   Crossbones+   -  Reputation: 7668

Like
0Likes
Like

Posted 29 August 2014 - 08:09 AM

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.



#10 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 31 August 2014 - 03:42 PM

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):

 

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, 31 August 2014 - 03:43 PM.


#11 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 31 August 2014 - 05:53 PM

The video illustrates the problem fine, but I would need to see the programming involved in it.

#12 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 31 August 2014 - 06:43 PM

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.



#13 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 01 September 2014 - 03:26 AM

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.

#14 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 01 September 2014 - 12:47 PM

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, 01 September 2014 - 12:48 PM.


#15 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 01 September 2014 - 03:58 PM

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.

#16 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 01 September 2014 - 06:22 PM

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.



#17 Kryzon   Prime Members   -  Reputation: 2934

Like
0Likes
Like

Posted 01 September 2014 - 08:23 PM

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 ) )


#18 P0jahn   Members   -  Reputation: 263

Like
0Likes
Like

Posted 02 September 2014 - 01:07 PM

Tried that among a bunch of other conditions. Wont work :(



#19 P0jahn   Members   -  Reputation: 263

Like
1Likes
Like

Posted 10 September 2014 - 10:28 AM

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, 10 September 2014 - 05:36 PM.






PARTNERS