• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
P0jahn

[2D]Make a camera follow multiple characters.

18 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

0

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

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

0

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

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.

0

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

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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 ) )
0

Share this post


Link to post
Share on other sites

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
1

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  
Followers 0