A simpler analog stick idea

shaihaludshaihalud Member Posts: 34
edited November -1 in Working with GS (Mac)
Looking at CodeMonkeys' tutorial, which is certainly viable, I couldn't help but wondering if there was a more simple way to program the analog stick. The Codemonkey method requires a base, stick, and 5 touch actors, and I was wondering if we could do away with the 5 touch actors and also compress the stick actor data.

Basically, the idea is that when the base receives a touch, it first checks for what touch is being pressed by checking each touch's position, either by using tough maths that i can't do (sin? cos? ha! I haven't done maths in....age minus age during last math class....3.2 million years! no wait,), or by easily checking Touch1.x >/< BaseX+BaseRadius/BaseX-BaseRadius (ditto for Y). If true, then it changes a global attribute (ie GlobX/Y) to the position of the touch. Then, instead of having the Stick look for collision with touch actors, it just updates itself based on the GlobX/Y attribute.

Obviously there are going to be a few problems with this idea until it is played with more, such as multiple touches in the analog area, and everything else I haven't thought of yet. Ideas? I just started thinking about this about 5 seconds ago, pretty sure lots of it won't add up.

Edit: For example, what to do when the touch position isn't over the base anymore but is still held down...

Comments

  • JCFordJCFord Member Posts: 785
    Well I just needed the joystick only so stripped out all refereces to touch 2-5 in codemonkeyd example if that helps.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Also, the reason that there are five touches needed is because you don't know which touch it is going to be...

    If you only have the joystick, then yes, one touch SHOULD be fine. As long as nobody accidentally touches the screen anywhere else...

    But if you have other buttons on the screen, or another joystick, then you can't tell which touch number you'll get...

    An easier way to see it is like this:

    Let's say we're playing air hockey, I touch puck number 1. My finger now represents Touch1. My opponent touches their puck, they are now Touch2.

    However, let's say I lift up my finger. My opponent now becomes Touch1! No longer Touch2... so if their controls were only looking for touch2, they won't work anymore. Likewise, if I now put my finger back on my puck - then now I'm Touch2.

    And it doesn't have to be with two players, I could play a game by myself and have two other buttons on the screen, let's say jump and shoot. Well, we don't know in which order people will be touching the buttons/joystick. And if you are constantly lifting your fingers for the jump/shoot buttons, the touch numbers keep changing.

    Or is someone's finger is accidentally touching the screen...

    Having all 5 Touches insures that the joystick will work at all times.

    And although the trigonometry is difficult, thankfully CodeMonkey generously gave it to us, so we don't have to figure it out!
  • TobyToby Member Posts: 478
    A prebuilt joystick actor component would be ideal, which you could just drag onto the stage and customise the graphics would be really awesome. Hint.... Hint...... ;)
  • TwistedMechTwistedMech Member Posts: 408
    Gs should have some built in actors like joysticks as it's really needed. I added the rules from cm project to my game. It took a while to duplicate and then I found very weired things happened to the actor in scene when gravity was added to the scene. Had to drop the joysticks in the end.
  • shaihaludshaihalud Member Posts: 34
    @firemaple
    oh, didn't make it that clear in my first post, but what i meant that when it checked touch it checked all touches to find out which one was touching the base area-
    so

    >base is touched inside
    ->if touch1.x&y is (within area of base) then globX&y = touch1.x&y
    ->if touch 2.x&y is (within area of base) then globX&y = touch2.x&y
    etc

    this way should work even with multiple touches on the field, because it would just check to see which touch is over the base. the way i understand the FAQ about touching is that when a touch is down is remains that number until it is released, so in your example of air hockey, when you release your finger, your opponent is still touch 2 unless they release their finger and touch it again.

    a better rule set:
    >when touch is inside
    AND
    >attribute "touch" = 0
    ->if touch1x&y is (within area of base), change "touch" to 1
    ->if touch 2.x&y is (within area of base), change "touch" to 2
    etc

    *now that touch is set to not 0, it should? or maybe go to the otherwise section of the rule automatically, i am unsure how GS deals with event handling inside rules. you might just have to make this next section a new rule instead of in the otherwise section

    otherwise
    >when touch is pressed
    ->if "touch"=1, globx&y = touch1x&y
    ->if "touch"=2, globx&y = touch2x&y
    etc

    otherwise
    >change "touch" to 0, change globx&y to (default)

    and then each touch area (that needs to monitor x&y) would just modify its own "touch" attribute, touchA, touchB, etc. because the initial touch of the base area will instantly update the device as to which touch is running, the second rule which actually controls the attribute should work fine. and because "touch pressed" reads as the initial position of the touch within the actor, even if another touch is down, when the first touch is released it should clear the touch data and reset. at least, to my way of understanding.
Sign In or Register to comment.