<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Mouse Aiming</title>
		<link>http://www.allegro.cc/forums/view/591216</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Wed, 02 May 2007 05:42:50 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hey,</p><p>I&#39;m trying to get a sprite to always face the mouse. How to I do this? Is there a formula I need to calculate the angle based on the mouse position? If so, what is it?</p><p>Thanks,<br />KG
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevlar Games)</author>
		<pubDate>Tue, 01 May 2007 06:46:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Depends a lot on your game. If this is some top down angle where the camera is perfectly perpendicular to the playfield, easy as pie (your alliteration skill increases by 1). If not, it gets stickier ...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (23yrold3yrold)</author>
		<pubDate>Tue, 01 May 2007 07:04:29 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here you go:<br />_______________________________________<br />include math.h
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;math.h&gt;</span>
</pre></div></div><p>
_______________________________________<br />define pi.
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#ifndef PI</span>
<span class="p">#define PI 3.1415927 //++</span>
<span class="p">#endif</span>
</pre></div></div><p>
_______________________________________<br />you need a bitmap...
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/BITMAP" target="_blank"><span class="a">BITMAP</span></a> <span class="k3">*</span>test<span class="k2">;</span>
</pre></div></div><p>
_______________________________________<br />you need to store a value..
</p><div class="source-code snippet"><div class="inner"><pre><span class="k1">double</span> player_angle<span class="k2">;</span>
</pre></div></div><p>
_______________________________________<br />you need some logic.
</p><div class="source-code snippet"><div class="inner"><pre>player_angle <span class="k3">=</span> <span class="k2">(</span><a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a> <span class="k3">-</span> pY, <a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a> <span class="k3">-</span> pX<span class="k2">)</span><span class="k3">*</span><span class="n">128</span><span class="k3">/</span>PI<span class="k2">)</span> <span class="k3">+</span> V<span class="k2">;</span>
</pre></div></div><p>

mouse_y is the vertical screen position of the mouse pointer.<br />you can figure mouse_x out yourself.</p><p>pY is the second point of which is needed for the calculation of the degree, and which i suggest is you bitmaps rotation point is.</p><p>V is the additional degrees added to the calculated direction, you want to adjust this until it looks good <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />. NOTE: instead of a 360 degree, use 256.</p><p>_______________________________________<br />and you need to draw the sprite. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/pivot_sprite" target="_blank"><span class="a">pivot_sprite</span></a><span class="k2">(</span>buffer, test, pX, pY, cX, cY, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span>player_angle<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

pX and pY is the posision of the sprite.<br />and cX and xY is from which point the sprite should be rotated.</p><p>enjoy!</p><p>this should work, i think..
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Tue, 01 May 2007 07:51:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Albin said:</div><div class="quote"><p>

#ifndef PI<br />#define PI 3.1415927 //++<br />#endif
</p></div></div><p>
What is the //++ for?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (James Stanley)</author>
		<pubDate>Tue, 01 May 2007 18:06:33 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">James Stanley said:</div><div class="quote"><p>
What is the //++ for?
</p></div></div><p> It was my extremely bad way of saying: &quot;you can use more decimals if you want&quot;. &gt;&lt; <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Tue, 01 May 2007 18:14:55 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why not just use M_PI?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
and you need to draw the sprite. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/pivot_sprite" target="_blank"><span class="a">pivot_sprite</span></a><span class="k2">(</span>buffer, test, pX, pY, cX, cY, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span>player_angle<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div></div><p>
Slight correction
</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/pivot_sprite" target="_blank"><span class="a">pivot_sprite</span></a><span class="k2">(</span>buffer, test, pX, pY, cX, cY, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span>player_angle <span class="k3">*</span> <span class="k2">(</span><span class="n">256</span> <span class="k3">/</span> <span class="n">360</span>.<span class="n">0f</span><span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
But personally I would leave the number in radians, which would result in this (slightly simpler) code:
</p><div class="source-code snippet"><div class="inner"><pre>player_angle <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_50.html" target="_blank">atan2</a><span class="k2">(</span><a href="http://www.allegro.cc/manual/mouse_y" target="_blank"><span class="a">mouse_y</span></a> <span class="k3">-</span> pY, <a href="http://www.allegro.cc/manual/mouse_x" target="_blank"><span class="a">mouse_x</span></a> <span class="k3">-</span> pX<span class="k2">)</span> <span class="k3">+</span> V<span class="k2">;</span>

<a href="http://www.allegro.cc/manual/pivot_sprite" target="_blank"><span class="a">pivot_sprite</span></a><span class="k2">(</span>buffer, test, pX, pY, cX, cY, <a href="http://www.allegro.cc/manual/itofix" target="_blank"><span class="a">itofix</span></a><span class="k2">(</span>player_angle <span class="k3">*</span> <span class="k2">(</span><span class="n">128</span> <span class="k3">/</span> M_PI<span class="k2">)</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 02 May 2007 04:42:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>What is M_PI? -.-
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Albin Engström)</author>
		<pubDate>Wed, 02 May 2007 04:50:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Its a constant that represents Pi.  Part of some standard and included with your compiler via cmath or math.h
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ImLeftFooted)</author>
		<pubDate>Wed, 02 May 2007 04:53:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Oops. Should have read. <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Ceagon Xylas)</author>
		<pubDate>Wed, 02 May 2007 05:00:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks, everybody. I wasn&#39;t expecting this many replies! I&#39;ll try those methods and tell you how they worked.</p><p>EDIT: Well, they were a bit confusing at first, but I got them to work! Now, is there any way to rotate a sprite without distortion?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kevlar Games)</author>
		<pubDate>Wed, 02 May 2007 05:42:50 +0000</pubDate>
	</item>
</rss>
