gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Sprite touch recognition

#118326 - outphase - Mon Feb 12, 2007 5:25 am

Is there any simplified way of recognizing when a certain sprite is touched?

For example, I have a piano displayed and want to find out which key is touched at a certain time. Other than hardcoding specific coordinates, can sprites be able to recognize certain events?

pseudocode:
Code:

if touch sprites[i]
   return i;

#118328 - DekuTree64 - Mon Feb 12, 2007 6:10 am

All depends on how you want to set things up. Sometimes it's easiest to do an if statement for each sprite, sometimes you can use a grid-type system, sometimes it works well to give the sprites their own update function and have them call a callback function if they detect that they've been touched.

The particular problem of a piano is extra tricky because of the non-rectangular white keys. For the piano in my ever-more-slowly-progressing tracker, I use a sort of table based system. The outer interface is a function that takes the touch position, and it tells you what key is pressed, if any. The table lookups are something like this:
Code:
u32 GetKey(TouchPiano *piano, u8xy touchPos)
{
    int touchX = touchPos.x - piano->topLeft.x;
    int touchY = touchPos.y - piano->topLeft.y;

    if (touchX < 0 || touchY < 0 || touchX >= PIANO_WIDTH || touchY >= PIANO_HEIGHT)
    {
        return KEY_NONE;
    }

    if (touchY < BLACK_KEY_HEIGHT)
    {
        u32 key = blackKeyMap[(touchX + HALF_KEY_WIDTH) / KEY_WIDTH]
        if (key != KEY_NONE)
            return key;
    }

    return whiteKeyMap[touchX / KEY_WIDTH];
}

So the black key lookup is offset by half a key width (because they sit half way between two white keys), and the entries for the spaces with no black key (between B-C, and E-F) are set to none, telling it to go on and do the white key lookup.

It's kind of hard to explain, and the real thing is a bit more complicated because it deals with changing what the lowest key is and stuff, but hopefully that can give you an idea of other ways to do touch checking.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#121887 - FRuMMaGe - Thu Mar 15, 2007 1:28 pm

You may just want to hard code it.

In the paint program i'm making (my first program ^^), I just hardcoded the coordinates of the buttons I press to change the colour.

Code:
if(specialKeysPressed & (1 << 6))
      {
         if(touchXY.py < 28)
         {
            touchPos = 0;
            if(touchXY.px >= 19 & touchXY.py >= 5)
            {
               if(touchXY.px <= 39)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(31, 31, 31);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 43 & touchXY.px <= 63)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(0, 31, 0);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 66 & touchXY.px <= 86)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(31, 0, 0);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 89 & touchXY.px <= 109)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(0, 0, 31);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 112 & touchXY.px <= 132)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(0, 31, 31);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 135 & touchXY.px <= 155)
               {
                  shape_color = RGB15(31, 31, 0);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 158 & touchXY.px <= 178)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(31, 0, 31);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 181 & touchXY.px <= 201)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(0, 0, 0);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 204 & touchXY.px <= 224)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(0, 15, 0);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px >= 227 & touchXY.px <= 247)
               {
                  shape_x = 0;
                  shape_y = 0;
                  shape_color = RGB15(15, 0, 15);
                  goto MAINSHAPECODE;
               }
               if(touchXY.px <= 5)
               {
                  shape_x = 0;
                  shape_y = 0;
               }
            }
         }            
         if(!(specialKeysPressed & (1 << 6)))
         {
            shape_height = 0;
            shape_width = 0;
            shape_x = 1;
            shape_y = 1;
         }
         else
         {
            if(touchPos == 1)
            {
               shape_x = touchXY.px;
               shape_y = touchXY.py;
               shape_height = 3;
               shape_width = 3;
               goto MAINSHAPECODE;
            }
            if(touchPos == 0);
            {
               goto MAINSHAPECODE;
            }
         }
      }
}

#121889 - tyraen - Thu Mar 15, 2007 1:36 pm

FRuMMaGe wrote:
You may just want to hard code it.

In the paint program i'm making (my first program ^^), I just hardcoded the coordinates of the buttons I press to change the colour.


Must be fun when you decide to shift them. :)

#121935 - HyperHacker - Thu Mar 15, 2007 9:13 pm

Constants come in handy there. Define some constants for the X and Y position of the group and then hardcode the buttons' positions relative to that. Of course, using an array of button coordinates is an even better solution.
_________________
I'm a PSP hacker now, but I still <3 DS.

#122051 - tepples - Fri Mar 16, 2007 7:28 pm

For best results, make the drawing code and the collision code use the same list of button coordinates. That's what GUIs on PCs do.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.