<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>2d camera</title>
		<link>http://www.allegro.cc/forums/view/618665</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 20 May 2022 20:08:36 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi all, I have people requesting a 2d camera system in my game framework, which is powered by allegro. I was wondering if you could give me advance on implementing this and/or point me to some resources, etc.</p><p>camera should have a target, offset, rotation and zoom. I&#39;m thinking I should be able to implement these using transformations, correct?</p><p>Thanks</p><p>Jarrod
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tinyBigGAMES)</author>
		<pubDate>Wed, 18 May 2022 00:29:44 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I have a viewport class that holds the information about the viewport.</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">struct</span> viewport
<span class="k2">{</span>
<span class="k1">float</span> x, y<span class="k2">;</span> <span class="c">//viewport starting x and y on the screen (e.g. x is larger if you're starting for a right-side of a split screen viewport)</span>
<span class="k1">float</span> ox, oy<span class="k2">;</span> <span class="c">//offset x/y (this is the movement of the 'camera')</span>
<span class="k1">float</span> w, h<span class="k2">;</span> <span class="c">// width / height of viewport</span>
<span class="k2">}</span>
</pre></div></div><p>

when you draw you do this:
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>x <span class="k3">+</span> v.x <span class="k3">-</span> v.ox, y <span class="k3">+</span> v.y <span class="k3">-</span> v.oy, ...<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

If the ox, and oy move up, the &quot;camera&quot; is scrolling down and to the right. So we then subtract that value from the drawn sprite routines.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 18 May 2022 00:59:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Same as above.</p><p>Also, you can move the camera every time player moves. This keeps the player always in the center.</p><p>I like a buffer. I&#39;ll create an imaginary rectangle slightly smaller than viewport. Maybe 100 on each side. I&#39;ll only move the camera when the player goes outside of the smaller rectangle. This allows free movement inside the rectangle.</p><p>When you draw your sprites, as stated before, adjust the xy amount by the camera xy.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DanielH)</author>
		<pubDate>Wed, 18 May 2022 01:35:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sweet! I see.</p><p>So, if I defined a huge virtual world, say 20000x20000, then moved the camera around in this world, the objects positions are normalized to the camera position, then rendered.</p><p>What about rotation and zooming?</p><p>I was thinking about something like:</p><p>camera2d_start(cam)<br />// all drawing between start/end will be affected by the camera transform<br />camera2d_end(cam)</p><p>And you do not have to manually update each object, allegro will take care of it. Plus you get your zoom and rotation too. Is this not possible?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tinyBigGAMES)</author>
		<pubDate>Wed, 18 May 2022 01:56:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Note: you don&#39;t have to update objects with changing camera position. You just add the camera position when you draw at the end.</p><p>Rotation and zoom can be done different ways depending on your game. You can zoom &quot;out&quot; (and in) by drawing to a larger bitmap, and then scaling it down and rotating it when drawing it to the screen.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Wed, 18 May 2022 03:21:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I got AllegroFlare::Placement2D for ya:</p><ul><li><p><a href="https://github.com/allegroflare/allegro_flare/blob/master/include/AllegroFlare/Placement2D.hpp">AllegroFlare/Placement2D.hpp - Header</a>
</p></li><li><p><a href="https://github.com/allegroflare/allegro_flare/blob/master/src/AllegroFlare/Placement2D.cpp">AllegroFlare/Placement2D.cpp - Source</a></p></li></ul><p>Placement2D is typically used for positioning objects on a screen (sprites), but it can also work as a camera.  It has the properties <span class="source-code">position</span>, <span class="source-code">size</span>, <span class="source-code">align</span>, <span class="source-code">scale</span>, and <span class="source-code">anchor</span>. All of these are vec2d(x, y). It also has rotation (float) and <span class="source-code">flip</span>, also vec2d(x, y).</p><p>You would typically position objects (sprites, text) by:
</p><div class="source-code snippet"><div class="inner"><pre>Placement2D place<span class="k2">;</span>
place.start_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <span class="c">// do your drawing here, as if the orientation is 0, 0</span>
place.restore_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

For a Camera, rather than using <span class="source-code">start_transform<span class="k2">(</span><span class="k2">)</span></span>, you would use <span class="source-code">start_reverse_transform<span class="k2">(</span><span class="k2">)</span></span>, and have these calls wrap around the objects when they are being drawn in the scene.</p><p>So,</p><div class="source-code snippet"><div class="inner"><pre>Placement2D camera<span class="k2">;</span>
camera.start_reverse_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  <span class="c">// draw all your entities here using their usual world coordinates</span>
camera.restore_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

By default, Placement2D has an alignment of (0.5, 0.5), so your camera will zoom in and out, and be pointed such that the position of the camera (position.x, position.y) is at the center of the screen.</p><p>So, if your character sprite is at (300, 200), and your camera is at position (300, 200), then the character will be at the center of the screen. And, when you zoom in/out, the character will still be at the center.</p><p>Note that you can nest multiple Placement2Ds inside each other and they will retain the transform from the previous element.</p><div class="source-code snippet"><div class="inner"><pre>Placement2D camera<span class="k2">;</span>
camera.start_reverse_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
  Placement2D sprite_placement<span class="k2">;</span>
  sprite_placement.start_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>bmp, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
  sprite_placement.restore_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>

  Placement2D sprite_placement2<span class="k2">;</span>
  sprite_placement2.start_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
     <a href="http://www.allegro.cc/manual/al_draw_bitmap"><span class="a">al_draw_bitmap</span></a><span class="k2">(</span>bmp2, <span class="n">0</span>, <span class="n">0</span>, <span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
  sprite_placement2.restore_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
camera.restore_transform<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Wed, 18 May 2022 03:41:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you, kind sir, this is what I am after. I will check it out.</p><p>And thanks to all that helped with this.</p><p>May your game dev journey be fruitful.</p><p>Cheers! 🥂</p><p>NOTE: I&#39;ve been trying to post this thank you for almost an hour. Serious issues with this site/forum.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tinyBigGAMES)</author>
		<pubDate>Wed, 18 May 2022 05:28:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>edit: Note to have the align (0.5, 0.5) work as expected, you will need to set the <span class="source-code">camera.size.x</span> and <span class="source-code">camera.size.y</span> to the width and height of your display.</p><p>There are some caveats. For example, if your display width/height is undetermined (as if you are creating a fullscreen display with ALLEGRO_FULLSCREEN_WINDOW, and will ultimately may have an undetermined resolution), you may have some quirks to work out. I recommend setting up a projection on your display bitmap with <span class="source-code">al_use_projection_transform</span> so that your virtual resolution remains consistent.</p><p>Also, when using Placement2D as a camera with <span class="source-code">start_reverse_transform<span class="k2">(</span><span class="k2">)</span></span>, the <span class="source-code">scale</span> property is inverted.  For example, when placing sprites and using <span class="source-code">start_transform<span class="k2">(</span><span class="k2">)</span></span>, a scale of 2.0 will show the sprite as being twice as big on the screen.  However, when using a <span class="source-code">start_reverse_transform<span class="k2">(</span><span class="k2">)</span></span>, a scale of 2.0 will shrink the zoom by half, because the scale is (1 / 2.0).</p><p>I haven&#39;t thought this through, it might be a good idea to eventually have the reverse transform invert the scale so it&#39;s more intuitive. However as it is now, it&#39;s mathematically symmetrical so I&#39;m not 100% sure which path is the best.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Wed, 18 May 2022 07:43:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi, ok, I was wondering why they were so HUGE, <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=";D" border="0" />. Can you explain using placement a bit more? I got the camera working as I need. I tried the placement, but it did not seem like it was working. I do understand now why it was so large.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (tinyBigGAMES)</author>
		<pubDate>Wed, 18 May 2022 10:38:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>is it wrong do this way?</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>    <span class="k1">typedef</span> <span class="k1">struct</span> CAMERA2D <span class="k2">{</span>
<span class="number">  2</span>        <span class="k1">float</span> x<span class="k2">;</span>
<span class="number">  3</span>        <span class="k1">float</span> y<span class="k2">;</span>
<span class="number">  4</span>        <span class="k1">int</span> w<span class="k2">;</span>
<span class="number">  5</span>        <span class="k1">int</span> h<span class="k2">;</span>
<span class="number">  6</span>        <a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a> camera_transform<span class="k2">;</span>
<span class="number">  7</span>    <span class="k2">}</span>CAMERA2D<span class="k2">;</span>
<span class="number">  8</span>
<span class="number">  9</span>    <a href="http://www.allegro.cc/manual/al_identity_transform"><span class="a">al_identity_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>c-&gt;camera_transform<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 10</span>    <a href="http://www.allegro.cc/manual/al_translate_transform"><span class="a">al_translate_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>c-&gt;camera_transform, <span class="k3">-</span>c-&gt;x, <span class="k3">-</span>c-&gt;y<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span>    <a href="http://www.allegro.cc/manual/al_scale_transform"><span class="a">al_scale_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>c-&gt;camera_transform, scale_x,scale_y<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 12</span>    <a href="http://www.allegro.cc/manual/al_use_transform"><span class="a">al_use_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>c-&gt;camera_transform<span class="k2">)</span><span class="k2">;</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ryonagana)</author>
		<pubDate>Fri, 20 May 2022 17:13:00 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Transformations are the easiest way to do a camera.</p><p>Basically, you translate to the negative camera position, scale and rotate, and then un-translate the camera position.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 20 May 2022 20:08:36 +0000</pubDate>
	</item>
</rss>
