<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>luminance &#187; camera</title>
	<atom:link href="http://www.luminance.org/tag/camera/feed" rel="self" type="application/rss+xml" />
	<link>http://www.luminance.org/blog</link>
	<description>Programming and Game Development - Kevin Gadd&#039;s Blog</description>
	<lastBuildDate>Sun, 02 Oct 2011 00:15:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Camera Constraints</title>
		<link>http://www.luminance.org/blog/gruedorf/2009/07/15/camera-constraints</link>
		<comments>http://www.luminance.org/blog/gruedorf/2009/07/15/camera-constraints#comments</comments>
		<pubDate>Wed, 15 Jul 2009 10:02:36 +0000</pubDate>
		<dc:creator>Kael</dc:creator>
				<category><![CDATA[Gruedorf]]></category>
		<category><![CDATA[camera]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[xna]]></category>

		<guid isPermaLink="false">http://www.luminance.org/?p=559</guid>
		<description><![CDATA[As the level I&#8217;m currently building has gotten larger, it&#8217;s become obvious that I need a way to control the behavior of the camera &#8211; simply centering it on the player isn&#8217;t sufficient. While I already had some usable support for panning the camera to show points of interest, and locking it in place while [...]]]></description>
			<content:encoded><![CDATA[<p>As the level I&#8217;m currently building has gotten larger, it&#8217;s become obvious that I need a way to control the behavior of the camera &#8211; simply centering it on the player isn&#8217;t sufficient. While I already had some usable support for panning the camera to show points of interest, and locking it in place while the player performs an action like climbing onto a surface, I didn&#8217;t have anything in place for more advanced control of the camera, like constraining it to a region of the level.</p>
<p>So, the first approach I took was simple: I placed a rectangle around the entire level to constrain the camera. For smaller, rectangular levels, this worked good enough &#8211; all I had to do was place the rectangle so that you could see all the important parts of the level, and make sure I filled the entire space with tiles so the player didn&#8217;t see any ugly empty space while the camera panned around.</p>
<p>But to be honest, that kind of sucks. It means I have to waste time filling in the entire rectangle with tiles, and the camera doesn&#8217;t do anything to help give the player a sense of the layout of the environment. Ideally, the camera should be able to automatically position itself so that you can see the important parts of the area around you, and not show you unimportant sections of the level that you might have already passed through, or might be encountering later. Furthermore, it should avoid showing you empty/boring space whenever possible &#8211; no point in showing boring repeated tiles to the player when we could be showing interesting parts of the level instead.</p>
<p>The first approach I tried was a relatively simple one: I placed points of interest throughout the level, and then drew lines to connect them using the editor. After that, I assigned a radius to each point, controlling how far the camera would be allowed to &#8216;drift&#8217; away from the point. The end result was that I had a series of &#8216;rails&#8217; the camera could follow through the level, of varying thickness depending on the radius of each point.</p>
<p><span id="more-559"></span></p>
<p>While this approach provided very smooth, organic-feeling camera motion, it was extremely difficult to get it to do exactly what I wanted it to do. It&#8217;s hard to visualize how the camera will behave when following rounded paths through the level, and it was difficult to get the camera to transition between rails at exactly the times I wanted &#8211; if two rails were close to each other, the camera would often jump between them unpredictably, and it was hard to prevent that without introducing nasty side effects.</p>
<p>So, I moved on to a more complex approach: Instead of creating &#8216;rails&#8217; for the camera to follow, I instead defined a set of rectangular camera regions, covering all the parts of the level I wanted the camera to focus on. The idea was to have the camera automatically focus on the most interesting parts of the level, and as a secondary effect, have it avoid showing empty parts of the screen.</p>
<p>Once I had all the regions set up, I wrote some simple code to have the camera perform some simple collision detection, between the four edges of the camera (top, left, bottom, right) and the camera regions, so that it would automatically pan around to follow the player, but stop when hitting the edge of the visible regions. While I eventually got this approach to work, it was difficult to use it to hide empty parts of the level &#8211; most of the camera regions ended up being smaller than the screen, and when dealing with regions smaller than the screen, it&#8217;s impossible to get the camera to automatically show you the &#8216;right&#8217; parts of the level, since the camera algorithm has to choose between multiple equally acceptable positions for the camera.</p>
<p>The final approach I settled on was a refined version of the previous one. Instead of simply defining a set of rectangles, I built on that approach: I placed rectangles to define regions or &#8216;rooms&#8217; within the level, and assigned each room a set of constraints to apply to the camera. At runtime, the camera code selects a region based on the player&#8217;s current position, and then applies the constraints from that region to control the motion of the camera. In most cases, I only use one or two constraints on a given region, but if I want to, I can use all four of them, constraining the minimum/maximum position of the camera along both axes.</p>
<p><object width="640" height="385" data="http://www.youtube-nocookie.com/v/asuK-y5YCKs&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" type="application/x-shockwave-flash"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube-nocookie.com/v/asuK-y5YCKs&amp;hl=en&amp;fs=1&amp;rel=0&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;hd=1" /><param name="allowfullscreen" value="true" /></object></p>
<p>This gives me most of what I need to make the camera behave in a pleasing way: While the player&#8217;s in a long corridor, I can lock the camera&#8217;s Y axis and make it follow the player along the corridor, but stop when it hits the edges of the corridor. When the player&#8217;s ascending a long vertical shaft, I can lock the camera horizontally but have it follow him vertically. And in cases where the player is moving along both axes, I can just lock it to a rectangular region so it attempts to follow the player, but avoids wasting onscreen space.</p>
<p>Of course, there&#8217;s one obvious flaw with this approach, once you prototype it: Since the player can only be in one camera region at a time, you get an abrupt jump when the player moves from one region to another. Luckily, there&#8217;s a relatively simple way to solve this problem: Interpolation. Unfortunately, the simplest possible approach here won&#8217;t work right: If you just interpolate between regions, the camera ends up bouncing around, traveling outside the constraints set by the camera regions. The solution I ended up applying works like this: When the player enters a new camera region, I slowly ramp up the effect of the new region, while leaving the previous region&#8217;s constraints operating at full strength. Once the new region&#8217;s constraints are in full effect, I then ramp down the effect of the previous region. This ensures that the camera is fully constrained at all times, but still provides a relatively smooth transition between regions. It&#8217;s not perfect, but in practice it&#8217;s relatively easy to set up regions so the camera smoothly tracks the player as he travels between them.</p>
<p>Overall, I&#8217;m pleased with how this turned out &#8211; I only had to write around a hundred lines of code to implement the final version of my camera constraint system, even though it took me around 8 hours to arrive at my final implementation. It&#8217;s unfortunate that it took so long to get it right, but considering that I had no idea how to solve this problem when I started, things went pretty well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.luminance.org/blog/gruedorf/2009/07/15/camera-constraints/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

