<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Transforming</title>
		<link>http://www.allegro.cc/forums/view/617602</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 31 Oct 2018 19:24:40 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hi! I&#39;ve recently been learning about basic physics in games and I&#39;m up to the point of angles/angular velocity etc. Basically I want to transform an al_rectangle to a specific angle. I know next to nothing about transformations even after reading doc files and a few tutorials. The best I can do at present is set the entire world to the desired angle. </p><p>[code]<br />float x=100,y=100,a=0.0f;<br />float x2=400,y2=100;</p><p>void Draw(){<br />    al_draw_filled_rectangle(x2,y2,x2+50,y2+50,al_map_rgb(255,255,0));</p><p>    //some transformation code here I&#39;m assuming to modify the box below<br />    al_draw_filed_rectangle(x,y,x+50,y+50,al_map_rgb(0,255,255));<br />   //then something to rotate the world back to the original position, maybe?</p><p>}<br />void Update(){<br />   a+=0.01;<br />}</p><p>[/code]</p><p>I&#39;ve been using ALLEGRO_TRANSFORM and have been getting some pretty crazy results. Can anybody modify the above code to change just one of the rectangles angles? Or explain why what I&#39;m doing won&#39;t work? <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" /> Thanks guys
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dakatt)</author>
		<pubDate>Tue, 30 Oct 2018 23:29:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>dakatt,</p><p>Please show more code. Please show us what you have or are trying right now, and tell us what you expect it to do.</p><p>You say you&#39;ve got the world rotated now. That&#39;s a good first step. So you know how to rotate. That&#39;s good.</p><p>To rotate a rectangle in place takes a little more work, but not much.</p><p>1. Get the current view matrix from allegro using <a href="https://liballeg.org/a5docs/trunk/transformations.html#al_get_current_transform">al_get_current_transform</a>.
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/ALLEGRO_TRANSFORM"><span class="a">ALLEGRO_TRANSFORM</span></a> t,original<span class="k2">;</span>
t <span class="k3">=</span> <span class="k3">*</span><a href="http://www.allegro.cc/manual/al_get_current_transform"><span class="a">al_get_current_transform</span></a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
original <span class="k3">=</span> t<span class="k2">;</span>
<span class="c">// save for later</span>
</pre></div></div><p>

2. Decide on a pivot for your shape. Usually it&#39;s the center. You will translate by this vector before doing anything else so the rotation will work properly.
</p><div class="source-code snippet"><div class="inner"><pre><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>t , <span class="k3">-</span>centerx , <span class="k3">-</span>centery<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

3. Now that you have made the center of your rectangle the origin (0,0) with al_translate_transform, you can safely rotate it around this pivot.
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/al_rotate_transform"><span class="a">al_rotate_transform</span></a><span class="k2">(</span><span class="k3">&amp;</span>t , <span class="n">45</span>.<span class="n">0</span><span class="k3">*</span>M_PI<span class="k3">/</span><span class="n">180</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span><span class="c">// rotate by 45 degrees</span>
</pre></div></div><p>

4. Now you have to move your shape back to where it was, so translate by the opposite of the first translation.
</p><div class="source-code snippet"><div class="inner"><pre><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>t , centerx , centery<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

5. Now use the new transform and you&#39;re ready to draw your rectangle.
</p><div class="source-code snippet"><div class="inner"><pre><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>t<span class="k2">)</span><span class="k2">;</span>
<span class="c">/// Draw a regular rectangle </span>
<span class="c">//void al_draw_rectangle(float x1, float y1, float x2, float y2,</span>
<span class="c">//                       ALLEGRO_COLOR color, float thickness)</span>

<a href="http://www.allegro.cc/manual/al_draw_rectangle"><span class="a">al_draw_rectangle</span></a><span class="k2">(</span><span class="n">0</span> , <span class="n">0</span> , rwidth , rheight , <a href="http://www.allegro.cc/manual/al_map_rgb"><span class="a">al_map_rgb</span></a><span class="k2">(</span><span class="n">0</span>,<span class="n">255</span>,<span class="n">0</span><span class="k2">)</span> , <span class="n">5</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

You draw the rectangle at (0,0,w,h) because your code translates by the rectangle&#39;s center so you don&#39;t have to draw it at its real position.</p><p>6. Now clean up after yourself.
</p><div class="source-code snippet"><div class="inner"><pre><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>original<span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 31 Oct 2018 00:34:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you Edgar, that&#39;s basically everything I needed. I&#39;m following a youtube series on physics engines which looks to be building up to something similar to box2D. I was understanding the physics side of things, but when they started using functions from their own language I got lost. I was stretching my skills writing a custom vector class, I think writing a transformation function would be a bit beyond my skill level <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (dakatt)</author>
		<pubDate>Wed, 31 Oct 2018 09:36:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey, my instructions were wrong.</p><p>You can either draw it at -width/2, -height/2 and skip step 2, or use step 2, and draw it at its normal coordinates.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Wed, 31 Oct 2018 19:24:40 +0000</pubDate>
	</item>
</rss>
