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.

Coding > help with collision detection

#18451 - Darmstadium - Fri Mar 26, 2004 6:11 pm

I am having a bit of trouble getting my system of collision detection to work. It works like this: there is a struct called object that stores the x, y, height, and width of all of the stuff you can collide with. All of the objects are pointed to in an array of pointers to objects called objs. Here's my code:
Code:

struct object
{
   u8 n;
   u16 x;
   u16 y;
   u8 h, w;
};

#define OBJSLEN   1
object * objs[OBJSLEN];

bool MoveOK(object * client, s16 xmove, s16 ymove)
{
   u8 oks = 0;
   u16 loop;
   for (loop = 0; loop <= OBJSLEN; loop++)
   {
      if (objs[loop]->n != client->n)
      {
         if (objs[loop]->x > client->x + xmove + client->w)
            oks++;
         else if (objs[loop]->x + objs[loop]->w < client->x + xmove)
            oks++;
         else if (objs[loop]->y > client->y + ymove + client->h)
            oks++;
         else if (objs[loop]->y + objs[loop]->h < client->y + ymove)
            oks++;
      }
   }
   if (oks == OBJSLEN - 1)
      return true;
   
   return false;
}


When I test my demo the sprite I'm checking moves right on over all the other objects. I'm not at all sure why this is


Last edited by Darmstadium on Sat Mar 27, 2004 3:27 am; edited 1 time in total

#18452 - poslundc - Fri Mar 26, 2004 6:22 pm

Well, for one, if OBJSLEN is defined as 1 and you're looping over OBJSLEN objects... that means you're only testing against 1 object.

Dan.

#18475 - Darmstadium - Sat Mar 27, 2004 3:25 am

Heh. That was kind of stuipid... OK, I fixed it, and the very same thing happens. Am I going about this the right way? How might you do it?

#18477 - poslundc - Sat Mar 27, 2004 4:13 am

Search the forums; other people have much better ideas than I do.

http://forum.gbadev.org/viewtopic.php?t=2539
http://forum.gbadev.org/viewtopic.php?t=2390

... are both good starting places I pulled at first glance from a quick search of collision and detection.

Dan.

#18479 - Darmstadium - Sat Mar 27, 2004 5:25 am

thank you very much for your help