<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Need help with Joystick implementation</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/1154</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 01:44:10 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/1154" rel="self" type="application/rss+xml" />

		<item>
			<title>sfj on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-28880</link>
			<pubDate>Fri, 05 Mar 2010 15:47:07 +0000</pubDate>
			<dc:creator>sfj</dc:creator>
			<guid isPermaLink="false">28880@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>The functionality of the joystick when using staticCenter was incomplete; the velocity upon initialization was not (0,0) and the joystick did not reset after touchesEnded.  To fix this I changed -(void)setStaticCenter to:<br />
<pre><code>-(void)setStaticCenter:(float)x y:(float)y
{
	staticCenterPoint = CGPointMake(x, y);
	center = staticCenterPoint;
	curPosition = staticCenterPoint;
	staticCenter = YES;
}</code></pre>
<p>And also -(bool)touchesEnded to:<br />
<pre><code>-(bool)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	if (!active) return NO;
	NSArray *allTouches = [touches allObjects];
	for (UITouch* t in allTouches) {
		if ((int)t == touchAddress) {
			active = NO;
			if (!staticCenter) {
				center = CGPointMake(0,0);
				curPosition = CGPointMake(0,0);
			}
			else if (staticCenter) {
				center = staticCenterPoint;
				curPosition = staticCenterPoint;
			}
			return YES;
		}
	}
	return NO;
}</code></pre>
<p>Also, must add this in the interface declarations<br />
<code>CGPoint staticCenterPoint;</code>
</p></description>
		</item>
		<item>
			<title>nozzer007 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-21623</link>
			<pubDate>Wed, 23 Dec 2009 12:55:14 +0000</pubDate>
			<dc:creator>nozzer007</dc:creator>
			<guid isPermaLink="false">21623@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Thanks, I now get the following warning's when I try to compile.</p>
<p>mStickLeft = [[[Joystick alloc] init:0 y:0 w:320 h:480] retain]; WARNING - No '-init:y:w:h:' method found</p>
<p>[layer addChild:mStickLeft z:3]; - WARNING 'Incompatible Objective-C types 'struct Joystick *' expect 'struct CocosNode *' when passing argument 1 of addChild:z' from distinct Objective-C type.</p>
<p>Can anyone help?
</p></description>
		</item>
		<item>
			<title>jd on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-21066</link>
			<pubDate>Tue, 15 Dec 2009 18:16:23 +0000</pubDate>
			<dc:creator>jd</dc:creator>
			<guid isPermaLink="false">21066@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Just like Java, you have to declare the type of variable.  Therefore, Joystick *mStick = [[[Joystick alloc] init:0 y:0 w:320 h:480] retain]; unless you previously declared Joystick *mStick.
</p></description>
		</item>
		<item>
			<title>nozzer007 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-21065</link>
			<pubDate>Tue, 15 Dec 2009 17:52:10 +0000</pubDate>
			<dc:creator>nozzer007</dc:creator>
			<guid isPermaLink="false">21065@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi,</p>
<p>I'm too an ultra noob and would like a little bit of help implementing the joystick controls.  I have got the latest code from the SVN repository for Joystick and have successfully built it.  However i'm having problems initialising the Joystick.  I have only one joystick in the game to control a Ball.  I found some code on a forum relating to this that initialises the mStick like this :</p>
<p>mStick = [[[Joystick alloc] init:0 y:0 w:320 h:480] retain];</p>
<p>If I put this in the (id)init{... I get 'mStick' undeclared.</p>
<p>I'm having trouble as to where I should put this in the code, Should it be in the Ball Layer? and whereabouts in the code.</p>
<p>The code I used following this to detect touches is in the BallLayer.m code as follows but each of the mStick references give compilation errors 'mstick' undeclared.</p>
<p>-(bool)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event<br />
{<br />
  [mStick touchesBegan:touches withEvent:event];<br />
  return kEventHandled;<br />
}</p>
<p>-(bool)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event<br />
{<br />
  [mStick touchesMoved:touches withEvent:event];<br />
  return kEventHandled;<br />
}</p>
<p>-(bool)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event<br />
{<br />
 [mStick touchesEnded:touches withEvent:event];<br />
 return kEventHandled;<br />
}</p>
<p>Think im finding the transition form Java to XCode harder than anticipated.</p>
<p>Any help much appreciated.
</p></description>
		</item>
		<item>
			<title>jd on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-20621</link>
			<pubDate>Wed, 09 Dec 2009 20:41:31 +0000</pubDate>
			<dc:creator>jd</dc:creator>
			<guid isPermaLink="false">20621@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p><a href="http://www.cocos2d-iphone.org/forum/topic/2551#post-15896" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/2551#post-15896</a>
</p></description>
		</item>
		<item>
			<title>gregkdunn on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-20619</link>
			<pubDate>Wed, 09 Dec 2009 20:36:06 +0000</pubDate>
			<dc:creator>gregkdunn</dc:creator>
			<guid isPermaLink="false">20619@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I am getting build errors trying to port my game to the 0.9 alpha release. The last 2 errors are in Joystick.h. from the extra's directory.</p>
<p>Any help would be appreciated.</p>
<p>-------</p>
<p>/Joystick.m: In function '-[Joystick touchesBegan:withEvent:]':</p>
<p>/Joystick.m:43: warning: 'CCDirector' may not respond to '-convertCoordinate:'</p>
<p>/Joystick.m:43: warning: (Messages without a matching method signature</p>
<p>/Joystick.m:43: warning: will be assumed to return 'id' and accept</p>
<p>/Joystick.m:43: warning: '...' as arguments.)</p>
<p>/Joystick.m:43: error: invalid initializer</p>
<p>/Joystick.m: In function '-[Joystick touchesMoved:withEvent:]':</p>
<p>/Joystick.m:67: warning: 'CCDirector' may not respond to '-convertCoordinate:'</p>
<p>/Joystick.m:67: error: incompatible types in assignment</p>
<p>-------
</p></description>
		</item>
		<item>
			<title>riq on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13615</link>
			<pubDate>Mon, 28 Sep 2009 13:47:28 +0000</pubDate>
			<dc:creator>riq</dc:creator>
			<guid isPermaLink="false">13615@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/47'>slipster216</a>: ok.
</p></description>
		</item>
		<item>
			<title>PhilM on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13545</link>
			<pubDate>Sun, 27 Sep 2009 22:12:31 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">13545@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thanks for the commit. The proximity manager also seems pretty sweet, I like the simple cache idea as well, could come in useful for a future project were planning.</p>
<p>Thanks!
</p></description>
		</item>
		<item>
			<title>slipster216 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13542</link>
			<pubDate>Sun, 27 Sep 2009 21:29:25 +0000</pubDate>
			<dc:creator>slipster216</dc:creator>
			<guid isPermaLink="false">13542@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Ok, it's in the trunk under an extra's folder, along with my proximity manager. Hopefully riq doesn't mind as I think these don't belong as core cocos things, but are obviously very useful.
</p></description>
		</item>
		<item>
			<title>slipster216 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13537</link>
			<pubDate>Sun, 27 Sep 2009 20:49:58 +0000</pubDate>
			<dc:creator>slipster216</dc:creator>
			<guid isPermaLink="false">13537@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>No, it's not a singleton, because you can have multiple joysticks on screen and it needs to hold state for each one. I'm going to submit a version of this to the trunk soon under an extra's folder.
</p></description>
		</item>
		<item>
			<title>Greg on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13527</link>
			<pubDate>Sun, 27 Sep 2009 20:00:46 +0000</pubDate>
			<dc:creator>Greg</dc:creator>
			<guid isPermaLink="false">13527@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Is the Joystick a singleton?  Seems like from a design standpoint it could be even though it does have state?  I guess that's a personal design decision.</p>
<p>I'd like to view the code also ... please email to greg.heidorn &#60;at&#62; gmail &#60;dot&#62; com.</p>
<p>Thanks!
</p></description>
		</item>
		<item>
			<title>Cyber on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-13413</link>
			<pubDate>Sat, 26 Sep 2009 11:56:22 +0000</pubDate>
			<dc:creator>Cyber</dc:creator>
			<guid isPermaLink="false">13413@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/47'>slipster216</a><br />
Can you Email me the code too? I would like to see it!<br />
thomas &#60;at&#62; equatior &#60;dot&#62; com<br />
Thank you very much!
</p></description>
		</item>
		<item>
			<title>PhilM on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-10881</link>
			<pubDate>Mon, 07 Sep 2009 14:10:14 +0000</pubDate>
			<dc:creator>PhilM</dc:creator>
			<guid isPermaLink="false">10881@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>It sounds like we really really need a contrib dir or similar for cocos2d, so you don't have to keep emailing this out all the time!
</p></description>
		</item>
		<item>
			<title>SpicyNikNaks on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154/page/2#post-10867</link>
			<pubDate>Mon, 07 Sep 2009 12:03:22 +0000</pubDate>
			<dc:creator>SpicyNikNaks</dc:creator>
			<guid isPermaLink="false">10867@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/47'>slipster216</a> I would like to see the code too if you wouldn't mind sending it to me..</p>
<p>dean at callida dot com</p>
<p>Thanks
</p></description>
		</item>
		<item>
			<title>Dakiu on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-10837</link>
			<pubDate>Sun, 06 Sep 2009 23:02:10 +0000</pubDate>
			<dc:creator>Dakiu</dc:creator>
			<guid isPermaLink="false">10837@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>can you add me ? too<br />
I am very interested in the joystick</p>
<p><a href="mailto:dakiu8@hotmail.com">dakiu8@hotmail.com</a>
</p></description>
		</item>
		<item>
			<title>iphoneDev on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-10814</link>
			<pubDate>Sun, 06 Sep 2009 14:57:38 +0000</pubDate>
			<dc:creator>iphoneDev</dc:creator>
			<guid isPermaLink="false">10814@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I as well would love to see it!</p>
<p><a href="mailto:satman88@gmail.com">satman88@gmail.com</a></p>
<p>Thanks...
</p></description>
		</item>
		<item>
			<title>RedshirtRich on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9380</link>
			<pubDate>Tue, 25 Aug 2009 16:19:50 +0000</pubDate>
			<dc:creator>RedshirtRich</dc:creator>
			<guid isPermaLink="false">9380@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Sign me up as well ,I suppose.</p>
<p>redshirtrich AT gmail DOT com</p>
<p>Thanks for all the help!
</p></description>
		</item>
		<item>
			<title>Sama7 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9140</link>
			<pubDate>Mon, 24 Aug 2009 00:45:18 +0000</pubDate>
			<dc:creator>Sama7</dc:creator>
			<guid isPermaLink="false">9140@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I would greatly appreciate a copy to work with as well</p>
<p>vestrit at gmail dOt com
</p></description>
		</item>
		<item>
			<title>vieko on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9068</link>
			<pubDate>Sun, 23 Aug 2009 06:18:19 +0000</pubDate>
			<dc:creator>vieko</dc:creator>
			<guid isPermaLink="false">9068@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Thank you slipster - much appreciated. I will play with it tomorrow and let you know how it works!
</p></description>
		</item>
		<item>
			<title>slipster216 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9056</link>
			<pubDate>Sun, 23 Aug 2009 03:14:31 +0000</pubDate>
			<dc:creator>slipster216</dc:creator>
			<guid isPermaLink="false">9056@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>sent..
</p></description>
		</item>
		<item>
			<title>bradparks on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9038</link>
			<pubDate>Sat, 22 Aug 2009 22:35:43 +0000</pubDate>
			<dc:creator>bradparks</dc:creator>
			<guid isPermaLink="false">9038@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I as well would love to see it!</p>
<p>bradparks@<a href='http://www.cocos2d-iphone.org/forum/profile/145'>bradparks</a>.com</p>
<p>Thanks...</p>
<p>Brad
</p></description>
		</item>
		<item>
			<title>vieko on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9024</link>
			<pubDate>Sat, 22 Aug 2009 21:05:07 +0000</pubDate>
			<dc:creator>vieko</dc:creator>
			<guid isPermaLink="false">9024@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>That would be great if you don't mind - please send it to: vieko@<a href='http://www.cocos2d-iphone.org/forum/profile/5063'>me</a>.com - thank you!
</p></description>
		</item>
		<item>
			<title>natanavra on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9020</link>
			<pubDate>Sat, 22 Aug 2009 19:38:23 +0000</pubDate>
			<dc:creator>natanavra</dc:creator>
			<guid isPermaLink="false">9020@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I'd like to take a peek at the code, please e-mail me to: <a href="mailto:natanavra@gmail.com">natanavra@gmail.com</a><br />
Thanks in advance.
</p></description>
		</item>
		<item>
			<title>slipster216 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9019</link>
			<pubDate>Sat, 22 Aug 2009 19:29:33 +0000</pubDate>
			<dc:creator>slipster216</dc:creator>
			<guid isPermaLink="false">9019@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>no, I was waiting for Riq to chime in on where we should put that kind of stuff.. I can email you the latest version if you need it sooner..
</p></description>
		</item>
		<item>
			<title>vieko on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-9000</link>
			<pubDate>Sat, 22 Aug 2009 16:23:01 +0000</pubDate>
			<dc:creator>vieko</dc:creator>
			<guid isPermaLink="false">9000@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hey slipster216 - just wondering if you manage to post the updated class to the SVN... I'm trying something similar to what was suggested by adunsmoor but would love to give your class a try as well
</p></description>
		</item>
		<item>
			<title>slipster216 on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-8107</link>
			<pubDate>Sat, 15 Aug 2009 02:37:19 +0000</pubDate>
			<dc:creator>slipster216</dc:creator>
			<guid isPermaLink="false">8107@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hmm, my version of this class allows you to get the joystick data expressed as either an X,Y value, or as an angle and velocity value. The version posted might be older though.. I'm going to add the code to an extra's folder in svn tonight, hopefully riq won't mind..
</p></description>
		</item>
		<item>
			<title>adunsmoor on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-8106</link>
			<pubDate>Sat, 15 Aug 2009 02:06:38 +0000</pubDate>
			<dc:creator>adunsmoor</dc:creator>
			<guid isPermaLink="false">8106@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>That "simpler" method I posted earlier was a little too simple.  It doesn't keep the position within a fixed radius from the center.  I can't seem to edit that post to fix it so, for now, just ignore that 2nd method and look at the first (the one with the angle and sqrt, etc. ) That's also very similar to what natanavra suggested too.</p>
<p>Hope this helps.
</p></description>
		</item>
		<item>
			<title>natanavra on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-8059</link>
			<pubDate>Fri, 14 Aug 2009 18:11:44 +0000</pubDate>
			<dc:creator>natanavra</dc:creator>
			<guid isPermaLink="false">8059@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>You could check for collisions between the two circles.<br />
Basically if you have a circle inside a circle then you could do this simple calc:</p>
<pre><code>//Assuming you have 2 objects, one for joystick and one for joystick base.

float x1 = joystick.position.x;
float x2 = joystickBase.position.x;
float y1 = joystick.position.y;
float y2 = joystickBase.position.y;

float xdif = x1 - x2;
float ydif = y1 - y2;

float distance = sqrt( xdif * xdif + ydif * ydif);
float radDist = joystick.radius - joystickBase.radius;

if(distance &#62;= radDist) {
     NSLog(@&#34;Joystick is out of bounds&#34;);
     //Do stuff...
}</code></pre>
<p>I guess you could use that to limit your joystick to just a circle instead of a rect.</p>
<p>BTW: I think slipster meant you should create a constant that is the maximum speed for the shortest length (up, down) and whenever the joystick distance from the center is bigger then the shortest length, you just equalize it to the constant... something like:</p>
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> maxSpeed (yourNum)
#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> maxDist (calculate the distance (up, down))
........
float distance = sqrt(pow(joystick.position.x - joystickBase.poisiton.x, 2) + pow(joystick.position.y - joystickBase.position.y, 2));
if(distance &#62; maxDist)
    [player setSpeed:maxSpeed];</code></pre>
<p>I suppose, sorry if something here isn't correct - it's dirty unchecked code, coming straight from my mind.</p>
<p>EDIT: Didn't see the posts above me, I posted as soon as the my connection came back online - as I was planning to post a few hours ago... sorry...
</p></description>
		</item>
		<item>
			<title>adunsmoor on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-8048</link>
			<pubDate>Fri, 14 Aug 2009 16:12:20 +0000</pubDate>
			<dc:creator>adunsmoor</dc:creator>
			<guid isPermaLink="false">8048@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Turns out there is a slightly simpler way to do this (there always is). </p>
<p>Google "similar triangles" for the a more detailed explanation.  Basically the idea is that the triangle created from the center to your touch is proportional to a triangle that expresses your velocity.</p>
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> CENTER (64.0+5.0)
#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> JOYSTICK_RADIUS 64.0

- (void)updateVelocity:(CGPoint)point {

    // Calculate the distance from the center.
    // Change the scale of dx and dy so that 1.0 is the edge of the circle.
    float dx = (point.x - CENTER) / JOYSTICK_RADIUS;
    float dy = (point.y - CENTER) / JOYSTICK_RADIUS;

    // If the point was outside of the circle then pick the largest distance
    // and change it to 1.0.  The smaller distance should be scaled down
    // proportionally.
    if (dx &#62; 1.0 &#124;&#124; dy &#62; 1.0) {
        if (dx &#62; dy) {
            dy = dy / dx;
            dx = 1.0;
        } else {
            dx = dx / dy;
            dy = 1.0;
        }
    }

    velocity = CGPointMake(dx, dy);
}</code></pre></description>
		</item>
		<item>
			<title>adunsmoor on "Need help with Joystick implementation"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/1154#post-8016</link>
			<pubDate>Fri, 14 Aug 2009 11:26:07 +0000</pubDate>
			<dc:creator>adunsmoor</dc:creator>
			<guid isPermaLink="false">8016@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Here's some code I'm using for my *very* simple joystick implementation.  The joystick has a single public property - the velocity - which has x and y values that are intended to go from -1.0 to 1.0.</p>
<p>The joystick is implemented as a Layer.  I have a circular area in the bottom left that accepts touches.  It is centered at CENTER,CENTER on the screen.  The radius of touchable area constrains the velocity.</p>
<pre><code>#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> CENTER (64.0+5.0)
#<a href='http://www.cocos2d-iphone.org/forum/tags/define'>define</a> JOYSTICK_RADIUS 64.0

- (void)updateVelocity:(CGPoint)point {
	// calculate distance and angle from the center
	float dx = point.x - CENTER;
	float dy = point.y - CENTER;

	float distance = sqrt(dx * dx + dy * dy);
	float angle = atan2(dy, dx); // in radians

	// Velocity goes from -1.0 to 1.0.
	// BE CAREFUL: don&#39;t just cap each direction at 1.0 since that
	// doesn&#39;t preserve the proportions.
	if (distance &#62; JOYSTICK_RADIUS) {
		dx = cos(angle)*JOYSTICK_RADIUS;
		dy = sin(angle)*JOYSTICK_RADIUS;
	}

	velocity = CGPointMake(dx/JOYSTICK_RADIUS, dy/JOYSTICK_RADIUS);
}</code></pre></description>
		</item>

	</channel>
</rss>

