Advertisement
NightFox

N'gine Raycast Point

Nov 7th, 2021
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. /*** Consulta si has tocado un punto de un sprite (Raycast point) ***/
  2. bool NGN_Collisions::RaycastPoint(NGN_Sprite* spr, float position_x, float position_y, float &hitpoint_x, float &hitpoint_y) {
  3.  
  4.     // Sprite invalido
  5.     if (spr == NULL) return false;
  6.     // Sprite invisible
  7.     if (!spr->visible) return false;
  8.  
  9.     // Decide el metodo para la primera comprobacion
  10.     int32_t r = 0.0f;
  11.     int32_t d = 0.0f;
  12.     Vector2I32 distance;
  13.     if (spr->rotation != 0.0f) {
  14.         // Si esta rotado, colision por circulos
  15.         r = (int32_t)(std::sqrt(std::pow(spr->width, 2.0f) + std::pow(spr->height, 2.0f)) / 2.0f);
  16.         d = (int32_t)std::sqrt(std::pow((spr->position.x - position_x), 2.0f) + std::pow((spr->position.y - position_y), 2.0f));
  17.         if (d > r) return false;
  18.     } else {
  19.         // Si no lo estan, colision por cajas
  20.         distance.x = (int32_t)(std::abs(spr->position.x - position_x));
  21.         distance.y = (int32_t)(std::abs(spr->position.y - position_y));
  22.         if ((distance.x > (int32_t)(spr->width / 2.0f)) || (distance.y > (int32_t)(spr->height / 2.0f))) return false;
  23.     }
  24.  
  25.     // Renderiza una textura de 1x1 para la comprobacion
  26.     int32_t x = (int32_t)(spr->position.x - position_x);
  27.     int32_t y = (int32_t)(spr->position.y - position_y);
  28.     SDL_Surface* srf = NULL;
  29.     srf = RenderSpriteInSurface(spr, x, y, 1, 1);
  30.  
  31.     // Analiza si hay datos en el pixel solicitado
  32.     bool collision = false;
  33.     uint32_t* pixels = (uint32_t*)srf->pixels;
  34.     if ((pixels[0] & 0x000000FF)) collision = true;
  35.  
  36.     // Paso de limpieza
  37.     SDL_FreeSurface(srf);
  38.     srf = NULL;
  39.  
  40.     // Resultado final
  41.     return collision;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement