Jump to content
  • Advertisement
Sign in to follow this  
P0jahn

[2D]Make a camera follow multiple characters.

This topic is 1356 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

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

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

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

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
Try inverting the zoom division, dividing the rectangle width by the stage visible width etc.
It inverts the way that the zoom value changes.

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!