Advertisement
tm512

Fixed collision

Sep 27th, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.52 KB | None | 0 0
  1. uint8 obj_collide_hitbox (obj_t *obj, hitbox_t *testbox)
  2. {
  3.     int32 a;
  4.     uint8 ret = 0;
  5.     fixed *delta;
  6.     fixed *omina, omaxa, tmina, tmaxa;
  7.     fixed ominb, omaxb, tminb, tmaxb;
  8.  
  9.     if (obj->flags & OF_NOCLIP)
  10.         return 0;
  11.  
  12.     for (a = 0; a < 2; a++)
  13.     {
  14.         // 'a' is current axis, 'b' is opposite
  15.         if (!a) // Y Axis
  16.         {
  17.             if (obj->hitbox.y + obj->deltay > testbox->y + testbox->h
  18.             ||  obj->hitbox.y + obj->hitbox.h + obj->deltay < testbox->y) // not touching
  19.                 continue;
  20.  
  21.             tmina = testbox->y;
  22.             tmaxa = tmina + testbox->h;
  23.             omina = &obj->hitbox.y;
  24.             omaxa = *omina + obj->hitbox.h - 1;
  25.             tminb = testbox->x;
  26.             tmaxb = tminb + testbox->w;
  27.             ominb = obj->hitbox.x;
  28.             omaxb = ominb + obj->hitbox.w - 1;
  29.             delta = &obj->deltay;
  30.         }
  31.         else // X axis
  32.         {
  33.             if (obj->hitbox.x + obj->deltax > testbox->x + testbox->w
  34.             ||  obj->hitbox.x + obj->hitbox.w + obj->deltax < testbox->x) // not touching
  35.                 continue;
  36.  
  37.             tmina = testbox->x;
  38.             tmaxa = tmina + testbox->w;
  39.             omina = &obj->hitbox.x;
  40.             omaxa = *omina + obj->hitbox.w - 1;
  41.             tminb = testbox->y;
  42.             tmaxb = tminb + testbox->h;
  43.             ominb = obj->hitbox.y;
  44.             omaxb = ominb + obj->hitbox.h - 1;
  45.             delta = &obj->deltax;
  46.         }
  47.  
  48.         // test for actual intersection
  49.         if ((ominb > tminb && ominb < tmaxb) || (omaxb > tminb && omaxb < tmaxb) || (tminb > ominb && tmaxb < omaxb))
  50.         {
  51.             fixed cost;
  52.             // Which side are we going into?
  53.             // Only correct if this is the cheapest way out
  54.             if (*delta < 0 && *omina + *delta <= tmaxa) // left/up
  55.             {
  56.                 cost = abs (tmaxa - *omina);
  57.  
  58.                 if (cost <= abs (omaxb - tminb) && cost <= abs (ominb - tmaxb))
  59.                 {
  60.                     *delta = 0;
  61.                     *omina = tmaxa;
  62.                 }
  63.             }
  64.             else if (*delta > 0 && omaxa + *delta >= tmina) // right/down
  65.             {
  66.                 cost = abs (tmina - omaxa);
  67.  
  68.                 if (cost <= abs (omaxb - tminb) && cost <= abs (ominb - tmaxb))
  69.                 {
  70.                     *delta = 0;
  71.                     *omina = tmina - (1 + omaxa - *omina);
  72.                 }
  73.             }
  74.             ret = 1;
  75.         }
  76.     }
  77.  
  78.     return ret;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement