cocos2d for iPhone

A fast, easy to use, free, and community supported 2D game engine

Register or log in - lost password?
  • Blog
  • Store
  • Games
  • Documentation
  • Download
  • About

cocos2d for iPhone » Programming » cocos2d support (graphics engine)

[joystick] SneakyJoystick 0.1!

(37 posts) (11 voices)
  • Started 2 years ago by sneakyness
  • Latest reply from MCroswell

Tags:

  • .99
  • .99 final
  • autocenter
  • buttons
  • circle hit check
  • controls
  • Dead Zone
  • dpad
  • emulatePhysicalJoystick
  • Example
  • fast rect check
  • Feel
  • Frankenstein
  • Joystick
  • joystickRadius
  • joysticks
  • movement
  • Sama7
  • Sensitivity
  • sneaky
  • sneakyjoystick
  • Sneakyness
  • touchRadius
12Next »
  1. sneakyness
    Member

    SneakyJoystick 0.1 just got pushed to github yesterday, give it a try!

    http://github.com/sneakyness/SneakyJoystick

    Uses 0.82 for now, I'll keep it updated as cocos updates. If it ever gets too far behind, bother me, I'll fix it.

    Features:

    Smart Touch Tracking - Use as many joysticks as you want, there's no way to confuse these bad boys!
    Totally Skinnable - Make it fit in!
    Autocenter Toggle - Use it if you need it! Turn it off if you don't!
    Fully Working Example - Easy to read code!

    Think joysticks are stupid because they take up screen real estate and there's a perfectly good acceler-- shut up and go tell somebody who cares. I'm working on some accelerometer code too. Full calibration and orientation. Let me have headaches so you don't have to! :)

    How to use it? Follow the example.
    Need help? Figure it out or ask somebody else.
    Want a feature? Do it yourself.
    Really want a feature? Pay me!

    I'm also working on a pretty solid button implementation. It's mostly done, I'm just too lazy to clean it up and throw an example together.

    I'm not one to beg, but if you use it in your game or utility, you're more than welcome to donate. I'm broke as hell right now, every cent helps. http://pledgie.com/campaigns/8931 :) <3

    If you donate, you can bet your generous ass I'll be more than willing to offer up some personal support with my code, or even something totally un-related, but if you need consulting, I'd rather just have you pay me for it. :)

    Posted 2 years ago #
  2. QcMat
    Member

    Nice job!

    I really like how light and functional it is. I'm definitely going to use this in a future project.

    I'm surely going to keep an eye out on this project. I'll keep you posted when I use it.

    Thanks!

    Posted 2 years ago #
  3. Sama7
    Member

    Nice. Going to take a look more in depth later tonight. There were some changes in touch for .99 final also, so that'll be something to keep in mind when you update. I just had to update my joystick class so it would function again in .99.

    Posted 2 years ago #
  4. sneakyness
    Member

    Glad to hear that QcMat! What kind of project is it going to be?

    Interesting. Care to detail what you had to change, Sama7?

    This joystick is on it's way to becoming much better, it's just a frankenstein built out of bits and pieces worth using at this point. It works surprisingly well, though.

    I'm bored tonight, so I'm writing SneakyDPad too. It's derived from the joystick, but has 1/2/4/8 direction lock, with one background image, and then images for each direction, so that it can be easily skinned like a DPad. Initially, it's only going to output the degrees, but I'll eventually add output in BOOL's too, one for each direction. Just trying to make it as easy as possible to drop into an existing project.

    As far as useful features go, I'll be adding in deadzone support as well. It's the only other thing I really think you can change on a joystick to modify the way it "feels". I suppose I could add sensitivity in, too.

    New graphics are definitely on the way, too. They're only meant to be placeholders, but I can't help but make something more presentable.

    Posted 2 years ago #
  5. Sama7
    Member

    @sneakyness

    Riq politely explained his changes here so it should cover what you may need to do, http://www.cocos2d-iphone.org/forum/topic/3853#post-23061 .

    I too use my joystick as a d-pad (by forcing the input to an axis based on what the input is closer to), so even though its a joystick, i'll take angled input and just force it onto the closest axis. I have a pretty similar concept as you, I just use a typedef for kJoystickDirectionRight, Left, etc to send out notices of the direction of the press.

    Overall however, yours is very robust and likely something at some point in the future (when I'm not tired of constantly re-updating my code) to integrate in. I just finished looking at it, it is nice.

    Why not make your joystick & dpad one class & just init it with a different type. SneakyInputController or something ;)

    Posted 2 years ago #
  6. sneakyness
    Member

    Thanks!

    I'm keeping them all separate for now. Once I finish all of the little pieces, I'll refactor it all down and bundle things together.

    Posted 2 years ago #
  7. dany
    Member

    lol, i done a VERY similar joystick yesterday! :D. However thanks, continue to develop it so i can replace my own joystick :P

    Posted 2 years ago #
  8. Bongeh
    Member

    very nice dude :)

    Posted 2 years ago #
  9. CJ
    Moderator

    Thanks! I liked it so I went ahead and updated it for the latest Cocos2D 0.99

    I forked the project here so you can grab the code:
    http://github.com/wiseganesha/SneakyJoystick

    Let me know if you end up bringing the changes back into your repo and I'll remove mine from github.

    Here's the change log of what I did:
    * Removed Cocos2D distribution files. Instead linked in from external folder outside of this project.
    * Updated references to Cocos2D to be compatible with latest 0.99 trunk (incl. new name changes like CCxxx)
    * Added new class to represent circles to make it simple to draw them on screen as well as put the necessary hit test code inside this new class. (k
    * Updated Joystick class to use new name "SneakyJoystick"
    * Updated Joystick class to simplify and optimize the code.
    * Updated hellow world example to use two joysticks to demonstrate how easy it is to connect multiple controllers to multiple objects and control th
    * Removed 128_white.png and 64_white.png as we now have a nice vector based circle drawing happening in openGL instead (ColoredCircleSprite class)
    * Removed all sqrt calls and instead compare distance using the squared values. (optimization)
    * Changed all function calls to their float counterparts (optimization). For example sin() is now sinf().

    Posted 2 years ago #
  10. sneakyness
    Member

    Wow! CJ, seriously, you're awesome.

    The new circle class is nice!! I like the example, too.

    The vector based circles are awesome! The only problem I could see running into with that is, what if people wanted to skin it with images? I haven't looked at anything 0.99 yet, I'm guessing just load images as textures instead of colors?

    I honestly haven't even looked at much OpenGL stuff yet, I'm working on a few projects, and anything I make along the way that seems useful is gonna go out like this.

    The optimization is very much appreciated, too. I usually don't even try to speed things up until I'm a few versions in.

    I'll likely end up bringing everything you've done back into the repo. It's all great. Very much appreciated. I'll wait for 0.99 to go stable before I do so, though. I'd always like to keep the current version of SneakyJoystick 100% working with the current stable version of CC2D.

    If you are developing a game using 0.99 and would like to use the joystick, use his awesome fork.

    Looking into the near future, once I get SneakyDPad and SneakyButtons a little more solid, it definitely looks like everything is going to be shrinking down in to one codebase. There are many shared components.

    Posted 2 years ago #
  11. alon7
    Member

    I'm getting a error : 'CC_PI' undeclared (first use in this function)

    Where is it declared ?

    Thanks

    Posted 2 years ago #
  12. sndwav
    Member

    @alon7
    I'm guessing here, but I'd say that CC_PI is supposed to refer to some math function of Cocos2d that returns Pi (~3.14).

    at first I got a lot of errors, but I notice I need to copy Cocos2d to the project..
    Now I'm getting the same error as alon7, but even if I remove the CC_PI, I still get a build error:

    ld: duplicate symbol .objc_class_name_CCSpeed in /Users/username/Downloads/wiseganesha-SneakyJoystick-082261a/build/Debug-iphonesimulator/libcocos2d libraries.a(CCAction.o) and /Users/username/Downloads/wiseganesha-SneakyJoystick-082261a/build/SneakyJoystick.build/Debug-iphonesimulator/SneakyJoystick.build/Objects-normal/i386/CCAction.o

    anyone knows what's wrong here?
    thanks

    Posted 2 years ago #
  13. sndwav
    Member

    Okay, I got it working :)
    Had to open a new Cocos2D project, copy over the classes files (HelloWorld, ColoredCircle, SneakyJoystick) and replaced "CC_PI" with "3.14159265f".
    Nice joysticks! thanks.

    Posted 2 years ago #
  14. CJ
    Moderator

    Sorry, I forgot I hacked my cocos2d to add CC_PI as 3.14159265f just as @sndwav suggested. (I removed it now).

    I pushed an update to GitHub with the following changes:
    * Further cleanup and optimization of Joystick class.
    * Removed all visual display stuff from Joystick class.
    * Added local defines for PI, PIx2 and DEG2RAD / RAD2DEG
    * Added skinned examples of Joystick to the demo scene.
    * Renamed HelloWorldScene to DemoJoystickScene
    * Added DPad (directional lock) functionality to joystick.
    * Removed build folder from repo.

    http://github.com/wiseganesha/SneakyJoystick

    @sneakyness
    I added two examples now of skinning. One uses a png graphic for a DPad and the other uses the same vector circles. BUT now this is all done outside of the Joystick class, so it should be more usable for more situations. Joystick is really just a responder to touch input and a data provider to things that want to know about what that touch data represents in the context of the joystick model.

    Posted 2 years ago #
  15. CJ
    Moderator

    Oh, and please let me know if you have compile issues so I can update the repo so it compiles "out of the box"

    Posted 2 years ago #
  16. alon7
    Member

    Still getting errors: Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1

    Posted 2 years ago #
  17. CJ
    Moderator

    @alon7 did you add the cocos2D files to the project? (if they show in RED in xcode it means they are not found. By default they are referenced from a folder on my HD outside of the project, so you'd have to re-reference them in to make it compile)

    Posted 2 years ago #
  18. alon7
    Member

    Yeah I re-added to my project and still getting this error

    Posted 2 years ago #
  19. sneakyness
    Member

    Woahhh another update already!?

    I'm going to bed right now, didn't download it. Just read over it. You're doing all my work for me! D: I have mixed feelings, I was looking forward to working on this little project, but I was also looking forward to finishing it and moving on to the next thing. I'm really not complaining by any means, this is awesome.

    Tomorrow morning, I'll clean up what I have of SneakyButton and throw together a basic example and put that up on GitHub as well.

    Posted 2 years ago #
  20. CJ
    Moderator

    @sneakyness Sorry didn't mean to take away your fun. I just thought it was cool too so I jumped on it :)

    It's at a place where I can use it for my needs, so I probably won't make any more updates to it from here (other than bugs if any are reported). :)

    Posted 2 years ago #
  21. sneakyness
    Member

    Neat. What're you using it for? I keep finding excuses to use it in all of my little test projects. ;P

    @alon7: Your best bet is to start a new cocos2d .99 project and move all the stuff into it.

    Posted 2 years ago #
  22. LoRdAcId
    Member

    I just incorporated this too to see how it would work in our game. It's pretty cool! The only change I made to your code is adding

    -(Sprite*)getBackground; getter which returns a pointer to the background sprite so you can run animations and opacity functions on the texture. I also shrank the smaller circle.

    One thing I noticed was when you have the joystick in the bottom left corner of the screen, obviously you have more room on the top and right of the joystick. As a result, you have less space on the bottom and left. After we reduced the velocity speed and applied movement to a smaller sprite, you can tell that moving up (well out of the joystick image) or moving right gives a bit more speed than moving left or down (since you have less space to move)

    I haven't had a chance to play with the velocity boundaries to even this difference out, but I'm sure it's easy.

    Hopefully my description makes sense.

    Posted 2 years ago #
  23. sneakyness
    Member

    Actually, you can easily fix that.

    All you have to do is take the circle checking code from ccTouchBegan and also use it in ccTouchMoved.

    @CJ what's up in the demo, the outer ~35 pixels of the big joystick don't work? Haven't really dug around yet.

    Posted 2 years ago #
  24. LoRdAcId
    Member

    do you mean putting

    [controller touchesBegan:touches withEvent:event];

    in both ccTouchesBegan and ccTouchesMoved? That seems to break the movement. I'm using the 0.8.2 code btw. By break I mean it doesn't move correctly and stops.

    Posted 2 years ago #
  25. sneakyness
    Member

    Oh you're using the .82 code.

    No, I don't. I mean take the part in the ccTouchesBegan method that takes the location and checks to see if it's in the circle, and put it in ccTouchesMoved. Like this:

    -(bool)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    	if(!active)
    		return NO;
    	NSArray *allTouches = [touches allObjects];
    	for (UITouch* t in allTouches){
    		if ((int)t == touchAddress){
    			CGPoint location = [[Director sharedDirector] convertToGL:[t locationInView:[t view]]];
    			if (isPointInCircle(location, center, joystickRadius)){
    				curPosition = CGPointMake(location.x, location.y);
    				[self updateVelocity:curPosition];
    				return YES;
    			}
    		}
    	}
    	return NO;
    }

    See if that does the trick?

    @CJ: I was going over your work. Great job man. When I defork it, I'll be splitting the joystick and dpad implementations. Mostly because the deadzone isn't toggleable, and if you set it to 0.0f, the dpad breaks. Are you sure you didn't read my mind when you wrote the dpad? :D

    Posted 2 years ago #
  26. sneakyness
    Member

    Looks like 0.99 just went gold, so I'll be spending today deforking for sure now.

    Posted 2 years ago #
  27. alon7
    Member

    Thanks, it worked!!!

    Posted 2 years ago #
  28. sneakyness
    Member

    @CJ: You were using touchRadius for the hit checking of the joystick, this requires you to actually touch inside of the smaller circle to start using it. Was this intentional? I can see where it would be more like a "joystick" this way, but we're above physical limitations around here :D

    Personally, I prefer being able to touch anywhere inside of the joystickRadius to start using the joystick. My thumbs completely cover that up, and in some games that have implemented this, I've found myself increasingly frustrated that I would die because I wasn't actually using the joystick, and I had no idea.

    I'm changing the fast rect check/circle hit check back to joystickRadius, because that's how it was in 0.1. We could code some option in like bool emulatePhysicalJoystick if you'd like? I could see it being very useful in a situation like using the "joystick" over the whole screen to control a ship, with the ship being the touchRadius, or something to that effect?

    Posted 2 years ago #
  29. sneakyness
    Member

    Welp, I'm just now going to sleep on this lovely Friday morning (wooo Friday!). I think I've got SneakyJoystick/Button to where they need to be at as far as deforkitization goes (I make my own words up). All that's really left to do is clean up a little, make a nice enough example, and push it out.

    I think this is a pretty solid indication that SneakyButton is up to snuff as of right now:

    //joystick
    SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
    leftJoy.position = ccp(64,64);
    leftJoy.backgroundSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 0, 0, 128) radius:64];
    leftJoy.thumbSprite = [ColoredCircleSprite circleWithColor:ccc4(0, 0, 255, 200) radius:32];
    leftJoy.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0,0,128,128)];
    leftJoystick = [leftJoy.joystick retain];
    [self addChild:leftJoy];
    
    //button
    SneakyButtonSkinnedBase *rightBut = [[[SneakyButtonSkinnedBase alloc] init] autorelease];
    rightBut.position = ccp(448,32);
    rightBut.defaultSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 255, 255, 128) radius:32];
    rightBut.activatedSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 255, 255, 255) radius:32];
    rightBut.pressSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 0, 0, 255) radius:32];
    rightBut.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 64, 64)];
    rightButton = [rightBut.button retain];
    rightButton.isToggleable = YES;
    [self addChild:rightBut];
    Posted 2 years ago #
  30. CJ
    Moderator

    @LoRdAcId There was already a getter for the sprites it's just implicit because I used the @synthesize at the top of the class. The setter is overridden so I could give it custom behavior. And regarding the position of the joystick relative to the screen space. You are correct that you can continue to move further up/right in the example although it will keep the velocity at 1.0 regardless of how far you go. But it may be a good idea to introduce a final radius after which no input is considered.

    @sneakyness
    1. Re: outer 35 px of joy not working. I assuming you mean that it only responds to touches inside the joystick radius minus the thumb radius (touchRadius). I made it this way because I thought you wanted to constrain the thumb inside the joystick radius completely. It's simple to change it to use the entire joystick radius for input, just let me know if you want help on that.

    2. I'll fix the deadzone bug. I don't think that dpad should be a different class because it's 99% the same code and in the same function as well which won't lend itself to reuse between classes (duplicate code < ideal code)

    Update: To fix the deadzone bug when setting it to 0 modify this section in SneakyJoystick.m in updateVelocity: (Changed < to <=)
    if(dSq <= deadRadiusSq){

    Update #2: I understand the confusion about the touchRadius != joystickRadius so I removed that whole concept and now you can touch inside the entire joystickRadius and the thumb will go outside the joystickRadius by half of its own radius.

    This is also pushed to my github:
    http://github.com/wiseganesha/SneakyJoystick

    Posted 2 years ago #

RSS feed for this topic

12Next »

Reply »

You must log in to post.

cocos2d for iPhone is proudly powered by bbPress.