<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>ALLEGRO_TRANSFORM and Movement</title>
		<link>http://www.allegro.cc/forums/view/615771</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 29 Sep 2015 21:21:06 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I put a camera scrolling feature into my game which has the camera following the player movement. <br />I did this using the ALLEGRO_TRANSFORM and these lines:</p><p>al_identity_transform(&amp;CameraTransform);<br />al_translate_transform(&amp;CameraTransform, -(m_XPosition), -(m_YPosition));<br />al_use_transform(&amp;CameraTransform);</p><p>Now the follow camera works fine when I use my keyboard controls, but it broke my mouse movement I had in my game. I move the player by mouse clicks as well, by finding the position of the mouse click and moving the player to that position.</p><p>I do not fully understand what the allegro transform/translate are doing to the screen and maybe this is somehow messing with the x / y click positions? The mouse movement seems to work on the left top half of the screen but not where else. Hopefully this makes sense and someone has a suggestion. Thanks.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Shpinxis)</author>
		<pubDate>Sun, 27 Sep 2015 02:26:47 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mkay, the way an <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a></span> works is that whatever you draw something, it will be moved <i>in the direction</i> of the transform.</p><p>So if you were to draw a box at <span class="source-code"><span class="k2">(</span><span class="n">20</span>, <span class="n">30</span><span class="k2">)</span></span> while an <span class="source-code"><a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a></span> of <span class="source-code"><span class="k2">(</span><span class="n">100</span>, <span class="n">400</span><span class="k2">)</span></span> is in effect, then it will be drawn at <span class="source-code"><span class="k2">(</span><span class="n">120</span>, <span class="n">430</span><span class="k2">)</span></span>.</p><p>Simple enough.</p><p>However, it doesn&#39;t transform the position of your mouse cursor, which will always be given in screen coordinates.  So for that, you&#39;ll want to use <span class="source-code"><a href="http://www.allegro.cc/manual/al_transform_coordinates"><span class="a">al_transform_coordinates</span></a><span class="k2">(</span><span class="k2">)</span></span>.</p><p>So grab your mouse coordinates when you catch the <span class="source-code">ALLEGRO_MOUSE_EVENT</span>:</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">float</span> mouse_on_screen_x <span class="k3">=</span> current_event.mouse.x<span class="k2">;</span>
<span class="k1">float</span> mouse_on_screen_y <span class="k3">=</span> current_event.mouse.y<span class="k2">;</span>
</pre></div></div><p>

Then later in your drawing code:</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// build your transform here</span>
<a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a> camera_transform<span class="k2">;</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>camera_transform, <span class="k3">-</span>camera_x, <span class="k3">-</span>camera_y<span class="k2">)</span><span class="k2">;</span>

<span class="c">// get convert the mouse position to world coordinates</span>
<span class="k1">float</span> mouse_in_world_x <span class="k3">=</span> mouse_on_screen_x<span class="k2">;</span>
<span class="k1">float</span> mouse_in_world_y <span class="k3">=</span> mouse_on_screen_y<span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_transform_coordinates"><span class="a">al_transform_coordinates</span></a><span class="k2">(</span><span class="k3">&amp;</span>camera_transform, <span class="k3">&amp;</span>mouse_in_world_x, <span class="k3">&amp;</span>mouse_in_world_y<span class="k2">)</span><span class="k2">;</span>

<span class="c">// Now you can use mouse_in_world_x and mouse_in_world_y as the cursor on your screen</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>camera_transform<span class="k2">)</span><span class="k2">;</span>
<a href="http://www.allegro.cc/manual/al_draw_circle"><span class="a">al_draw_circle</span></a><span class="k2">(</span>mouse_in_world_x, mouse_in_world_y, <span class="n">5</span>.<span class="n">0</span>, <a href="http://www.allegro.cc/manual/al_color_name"><span class="a">al_color_name</span></a><span class="k2">(</span><span class="s">"dodgerblue"</span><span class="k2">)</span>, <span class="n">1</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

[edit] I made a few edits for clarity.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mark Oates)</author>
		<pubDate>Sun, 27 Sep 2015 04:47:41 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you for the reply and help.</p><p>I have it mostly working how I want with the suggestions you posted, the only problem is that after the transform_coordinates the values do not seem to change. Also, the numbers over the initial mouse coordinates are crazy negative numbers, that probably doesn&#39;t matter. Not sure if there would be a reason that you would know why it seems that the transform_coordinates isn&#39;t changing the numbers.</p><p>float TempMouseXCoordinate = m_MouseXCoordinate;<br />float TempMouseYCoordinate = m_MouseYCoordinate;</p><p>al_transform_coordinates(&amp;CameraTransform, &amp;TempMouseXCoordinate, &amp;TempMouseYCoordinate);
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Shpinxis)</author>
		<pubDate>Tue, 29 Sep 2015 11:07:03 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The numbers appearing as random negatives at the beginning looks to me as variables not correctly initialized.</p><p>Instead of having <span class="source-code"><span class="k1">int</span> foo<span class="k2">;</span></span> consider using <span class="source-code"><span class="k1">int</span> foo <span class="k3">=</span> <span class="n">0</span><span class="k2">;</span></span>. That way the variable doesn&#39;t start its life with the previous data that was held on that position.</p><p>The second issue you have is happening maybe because you are only updating once in the program&#39;s lifetime the mouse coordinates. Try to put it somewhere logical on you main loop. A good position is on the <span class="source-code">ALLEGRO_EVENT_TIMER</span> event block, so that it updates once per frame.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Rodolfo Lam)</author>
		<pubDate>Tue, 29 Sep 2015 21:21:06 +0000</pubDate>
	</item>
</rss>
