<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Position of point when rotated</title>
		<link>http://www.allegro.cc/forums/view/586404</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 11 Jul 2006 20:38:46 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Given the distance of a point from a rotation axis in coordinates (for example, point A is 10 <i>x</i> units and -20 <i>y</i> units from axis) when the rotation angle is 0, how do I calculate the position of the point for any rotation angle (in radians)?</p><p>I&#39;ve tried &quot;x_pos = axis_x + x_distance * cos(angle); y_pos = axis_y + y_distance * -sin(angle);&quot; but this only seems to work in some cases <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 08:22:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I do it this way: (probably not the best method, but it works)</p><p>I use the vector length of pivot.x,pivot.y -&gt; blit_point.x,blit_point_y and the angle of the vector (8.0 and 4.0 dec degree in that case)</p><p>let&#39;s say &quot;18&quot;.</p><p>head_pos_ is the coordinate of the pivot of the head bitmap.<br />head_angle is the angle of rotation.</p><p>eyes_x and y are the &quot;blitting point&#39;s&quot; coord</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">float</span> eyes_x <span class="k3">=</span> head_pos_x <span class="k3">+</span> <span class="k1">int</span><span class="k2">(</span><span class="n">18</span>.<span class="n">0</span> <span class="k3">*</span> cosinus<span class="k2">(</span>head_angle <span class="k3">-</span><span class="n">90</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">)</span>
<span class="k3">+</span> <span class="k1">int</span><span class="k2">(</span><span class="n">8</span>.<span class="n">0</span> <span class="k3">*</span> cosinus<span class="k2">(</span>head_angle<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
<span class="k1">float</span> eyes_y <span class="k3">=</span> head_pos_y <span class="k3">+</span> <span class="k1">int</span><span class="k2">(</span><span class="n">18</span>.<span class="n">0</span> <span class="k3">*</span> sinus<span class="k2">(</span>head_angle <span class="k3">-</span><span class="n">90</span>.<span class="n">0</span><span class="k2">)</span><span class="k2">)</span>
<span class="k3">+</span> <span class="k1">int</span><span class="k2">(</span><span class="n">4</span>.<span class="n">0</span> <span class="k3">*</span> sinus<span class="k2">(</span>head_angle<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Epsi)</author>
		<pubDate>Tue, 11 Jul 2006 10:11:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Assuming 0 degrees is &#39;right&#39;,<br />x = cos(rotation) * distance + offsetamount_x<br />y = sin(rotation) * distance + offsetamount_y</p><p>+Rotation would be counter-clockwise.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Tue, 11 Jul 2006 10:36:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I had this code right for a long time, but didn&#39;t really understand how radians worked. Remember to increment in decimals and add in conditions such as
</p><div class="source-code snippet"><div class="inner"><pre>rotation<span class="k3">+</span><span class="k3">=</span><span class="n">0</span>.<span class="n">0036</span><span class="k2">;</span>

<span class="k1">if</span><span class="k2">(</span>rotation&gt;M_PI<span class="k3">*</span><span class="n">2</span><span class="k2">)</span> rotation<span class="k3">=</span><span class="n">0</span><span class="k2">;</span>
<span class="k1">if</span><span class="k2">(</span>rotation<span class="k3">&lt;</span><span class="n">0</span><span class="k2">)</span> rotation<span class="k3">=</span>M_PI<span class="k3">*</span><span class="n">2</span><span class="k2">;</span>
</pre></div></div><p>

Without limiters your line or whatever you&#39;re drawing/calculating goes CRAZY =P</p><p>And, Epsi, what&#39;s that -90.0 for? To get it into the &#39;right&#39; plane? I mean, so that 0 isn&#39;t pointing right, but instead, point up?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Tue, 11 Jul 2006 11:19:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
And, Epsi, what&#39;s that -90.0 for? To get it into the &#39;right&#39; plane? I mean, so that 0 isn&#39;t pointing right, but instead, point up?
</p></div></div><p>

yup, or at least if I remember correctly. What I did was a little dirty hack (the angle is not even the same on x/y axis), so I&#39;ll be happy if another solution shows up <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Epsi)</author>
		<pubDate>Tue, 11 Jul 2006 11:30:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>For rotation of a point <img class="math" src="http://www.allegro.cc/images/tex/4/9/49b311da0ed4baee4da4dd66e37ef59c-96.png" alt="&lt;math&gt;(x, y)&lt;/math&gt;" /> around the origin over an angle <img class="math" src="http://www.allegro.cc/images/tex/9/1/910c5697e4086f751246eed11bf19a50-96.png" alt="&lt;math&gt;\vartheta&lt;/math&gt;" />, use<br /><img class="math" src="http://www.allegro.cc/images/tex/c/d/cd35a4f4939cefe2b5d568705e56b70e-96.png" alt="&lt;math&gt;\begin{pmatrix} x&#39;\\ y&#39; \end{pmatrix} = \begin{pmatrix} \sin(\vartheta) &amp; \cos(\vartheta) \\ -\cos(\vartheta) &amp; \sin(\vartheta)\end{pmatrix} \begin{pmatrix} x\\ y \end{pmatrix}.&lt;/math&gt;" /></p><p>To rotate around an arbitrary point, translate to that point first, then rotate, then translate back. It wasn&#39;t entirely clear to me what you wanted to do, hope this helps.</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
if(rotation&gt;M_PI*2) rotation=0;<br />if(rotation&lt;0) rotation=M_PI*2;
</p></div></div><p>
That&#39;s quite wrong: there&#39;s nothing wrong with angles outside this range.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Evert)</author>
		<pubDate>Tue, 11 Jul 2006 11:49:07 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Mine should be correct:</p><p>For 0 degrees is &#39;up&#39;<br />x = sin(rotation) * distance + offsetamount_x<br />y = cos(rotation) * distance + offsetamount_y</p><p>+Rotation would be clockwise --&gt; or just make the &#39;distance&#39; negative in the x calculation for counter-clockwise.</p><p>For 0 degrees is &#39;down&#39;<br />x = sin(rotation) *  distance + offsetamount_x<br />y = cos(rotation) * -distance + offsetamount_y</p><p>Default: clockwise</p><p>For 0 degrees is &#39;left&#39;<br />x = cos(rotation) * -distance + offsetamount_x<br />y = sin(rotation) *  distance + offsetamount_y</p><p>Default: clockwise
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Tue, 11 Jul 2006 11:58:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you for all your responses, I&#39;ll try them later today. But there is one problem with most of your solutions, that I have to calculate the distance, and for that I have to use sqrt() which is slow. Evert&#39;s solution doesn&#39;t use the distance but I&#39;ve never done matrix multiplications <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 16:34:09 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You mean use <tt>sqrt()</tt> for acquiring the magnitude?<br /><span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_738.html" target="_blank">sqrt</a><span class="k2">(</span>x<span class="k3">+</span>x<span class="k3">*</span>y<span class="k3">+</span>y<span class="k2">)</span>?</span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Tue, 11 Jul 2006 16:47:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Evert&#39;s matrix is just 1337. It&#39;s ok to use following:</p><p>x&#39; = x*cos(v) - y*sin(v)<br />y&#39; = x*sin(v) + y*cos(v)</p><p>I usually reinvent this wheel by dividing the point (x, y) to two &quot;vectors&quot; (x, 0) and (0, y). <br />Then I rotate these separately. <br />First becomes (x*cos(v), x*sin(v)) and the second becomes (-y*sin(v), y*cos(v)).<br />Then I add them and I get the formulae above. <br />This all is for a coordinate system where y grows upwards and v grows anti-clockwise, zero being x-axis direction.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Johan Halmén)</author>
		<pubDate>Tue, 11 Jul 2006 17:04:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
sqrt(x+x*y+y)?
</p></div></div><p>

You fail it. sqrt(x*x + y*y).
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Tue, 11 Jul 2006 17:04:57 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Epsi&#39;s code is the &quot;right&quot; way to multiply by Evert&#39;s matrix, but it mixes up sine &amp; cosine by ignoring the fact that <span class="source-code"> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>a <span class="k3">-</span> <span class="n">90</span>.<span class="n">0</span> degrees<span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>a<span class="k2">)</span></span>  and <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>a <span class="k3">-</span> <span class="n">90</span>.<span class="n">0</span><span class="k2">)</span> <span class="k3">=</span><span class="k3">=</span> <span class="k3">-</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>a<span class="k2">)</span></span> because the sine &amp; cosine of an angle are 90 degrees out of phase.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Matt Smith)</author>
		<pubDate>Tue, 11 Jul 2006 17:07:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Evert&#39;s method is the best. You should always use vectors and not angles. Ever wondered why phisics is mainly based in vectors? Because they work very well. You can also include the translations in Evert&#39;s matrix, making it a 3x3 matrix. I would write how, but I don&#39;t know how to use the mock up well...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 18:03:32 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p><img class="math" src="http://www.allegro.cc/images/tex/d/9/d97b4443ba1f158d1c01ca2058dfdac1-96.png" alt="&lt;math&gt;\begin{pmatrix} x&#39;\\ y&#39; \\ w&#39; \end{pmatrix} = \begin{pmatrix} \sin(\vartheta) &amp; \cos(\vartheta) &amp; t_x \\ -\cos(\vartheta) &amp; \sin(\vartheta) &amp; t_y \\ 0 &amp; 0 &amp; 1\end{pmatrix} \begin{pmatrix} x\\ y \\ w\end{pmatrix}&lt;/math&gt;" /></p><p>Note: <a href="http://en.wikipedia.org/wiki/Homogeneous_coordinates">homogeneous coordinates</a>.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Tue, 11 Jul 2006 18:09:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The w should be 1 <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 18:42:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well I tried the matrix multiplication but it didn&#39;t work. Maybe it&#39;s because the coordinate and/or angle system is different? Mine is like this:<br />http://mega.ist.utl.pt/~vbfr/coord.png
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 19:10:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Aye, you&#39;re inverting the y axis, due to screen mapping, just switch the y by -y.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 19:19:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I tried that and it still didn&#39;t work <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /> x and y go to strange places...</p><p>EDIT: The actual code I have is like this:
</p><div class="source-code snippet"><div class="inner"><pre>x <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>angle<span class="k2">)</span> <span class="k3">*</span> xPos <span class="k3">+</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>angle<span class="k2">)</span> <span class="k3">*</span> <span class="k3">-</span>yPos <span class="k3">+</span> OFFSET_X<span class="k2">;</span>
y <span class="k3">=</span> <span class="k3">-</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_113.html" target="_blank">cos</a><span class="k2">(</span>angle<span class="k2">)</span> <span class="k3">*</span> xPos <span class="k3">+</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_728.html" target="_blank">sin</a><span class="k2">(</span>angle<span class="k2">)</span> <span class="k3">*</span> <span class="k3">-</span>yPos <span class="k3">+</span> OFFSET_Y<span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 19:25:50 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You need this:<br />x = (sin(angle) * (xPos-offset_X)) + (cos(angle) * (-y-offset_Y)) + offset_x;<br />Where offset_x is centerOfRotation(x);
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 19:40:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Still doesn&#39;t work... I feel that there is something very wrong because the position of the point changes much more than it should (it goes all over the screen when it should be near the center).</p><p>Anyway, OFFSET_X should be the x distance of the point to the axis right? And what you said is to change xPos (the x position of the axis) to (xPos - OFFSET_X) and yPos to (yPos - OFFSET_Y) right?</p><p>Maybe the problem has to do with the way I&#39;m doing things. What I&#39;m trying to do is set the position of a gun on a ship. The center of the ship is the rotation axis, and when the screen is rendered, everything is rotated except the ship, that is always motionless. I can&#39;t just set the gun coordinates because it&#39;s rotated, so I have to compensate for that rotation so the gun is always at the same position on the ship.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 19:58:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The offsext_x I meant was the center of the ship, the rotation point.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 20:00:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>
</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
The w should be 1
</p></div></div><p>

That depends on whether you are transforming a point or a direction.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (X-G)</author>
		<pubDate>Tue, 11 Jul 2006 20:00:54 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Felipe Maia said:</div><div class="quote"><p>
The offsext_x I meant was the center of the ship, the rotation point.
</p></div></div><p>
So in that case, what&#39;s xPos? The position of the point I want to rotate, or the distance of that point to the axis (in x)?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 20:09:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Let&#39;s say that you want to rotate the square attached using the blue point as the rotation center.<br />You have to:<br />for each point:<br />T(x,y) = (x&#39;,y&#39;);<br />x&#39; = ((sin(angle) * (x-blueCenterX)) + (cos(angle) * (y-blueCenterY))) + blueCenterX;</p><p>[edited}
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 20:10:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
So in that case, what&#39;s xPos?
</p></div></div><p>
I&#39;m guessing it&#39;s the position of the other object...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Tue, 11 Jul 2006 20:10:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thank you, it works perfectly now <img src="http://www.allegro.cc/forums/smileys/cheesy.gif" alt=":D" /></p><p>I just had to change &quot;x-blueCenterX&quot; to &quot;-(y-blueCenterX)&quot; and &quot;y-blueCenterY&quot; to &quot;x-blueCenterX&quot;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 20:24:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m really glad that you acknowledged everyone who tried to help!</p><p>[edit]<br />Sarcasm taken back!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Archon)</author>
		<pubDate>Tue, 11 Jul 2006 20:28:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I just had to change &quot;x-blueCenterX&quot; to &quot;-(y-blueCenterX)&quot; and &quot;y-blueCenterY&quot; to &quot;x-blueCenterX&quot;.
</p></div></div><p> This doesn&#39;t make sense to me, but glad to help.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Felipe Maia)</author>
		<pubDate>Tue, 11 Jul 2006 20:35:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Archon said:</div><div class="quote"><p>
I&#39;m really glad that you acknowledged everyone who tried to help!
</p></div></div><p>
I wasn&#39;t sure if I should&#39;ve acknowledged everyone that actually helped, or everyone that tried to help. You&#39;re right, I should acknowledge everyone that tried, because I do appreciate any help even if it wasn&#39;t part of the solution I used. Now I can&#39;t change it <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /> Sorry for that, it won&#39;t happen again.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Vasco Freitas)</author>
		<pubDate>Tue, 11 Jul 2006 20:38:46 +0000</pubDate>
	</item>
</rss>
