2D Games DevelopersФорум

Определение столкновения луча с Bounding Box'ом в 2D пространстве

#0
12:48, 4 фев 2007

Имеются координаты луча(x,y), его длина (r) и Bounding Box (x,y,x1,y1).
Подскажите кто-нибуть алгоритм определения столкновения(главное чтобы работало).

#1
14:12, 4 фев 2007

Все нашел.

Если кому интересно :

/* 
Fast Ray-Box Intersection
by Andrew Woo
from "Graphics Gems", Academic Press, 1990
*/

#include "GraphicsGems.h"

#define NUMDIM  3
#define RIGHT  0
#define LEFT  1
#define MIDDLE  2

char HitBoundingBox(minB,maxB, origin, dir,coord)
double minB[NUMDIM], maxB[NUMDIM];    /*box */
double origin[NUMDIM], dir[NUMDIM];    /*ray */
double coord[NUMDIM];        /* hit point */
{
  char inside = TRUE;
  char quadrant[NUMDIM];
  register int i;
  int whichPlane;
  double maxT[NUMDIM];
  double candidatePlane[NUMDIM];

  /* Find candidate planes; this loop can be avoided if
     rays cast all from the eye(assume perpsective view) */
  for (i=0; i<NUMDIM; i++)
    if(origin[i] < minB[i]) {
      quadrant[i] = LEFT;
      candidatePlane[i] = minB[i];
      inside = FALSE;
    }else if (origin[i] > maxB[i]) {
      quadrant[i] = RIGHT;
      candidatePlane[i] = maxB[i];
      inside = FALSE;
    }else  {
      quadrant[i] = MIDDLE;
    }

  /* Ray origin inside bounding box */
  if(inside)  {
    coord = origin;
    return (TRUE);
  }


  /* Calculate T distances to candidate planes */
  for (i = 0; i < NUMDIM; i++)
    if (quadrant[i] != MIDDLE && dir[i] !=0.)
      maxT[i] = (candidatePlane[i]-origin[i]) / dir[i];
    else
      maxT[i] = -1.;

  /* Get largest of the maxT's for final choice of intersection */
  whichPlane = 0;
  for (i = 1; i < NUMDIM; i++)
    if (maxT[whichPlane] < maxT[i])
      whichPlane = i;

  /* Check final candidate actually inside box */
  if (maxT[whichPlane] < 0.) return (FALSE);
  for (i = 0; i < NUMDIM; i++)
    if (whichPlane != i) {
      coord[i] = origin[i] + maxT[whichPlane] *dir[i];
      if (coord[i] < minB[i] || coord[i] > maxB[i])
        return (FALSE);
    } else {
      coord[i] = candidatePlane[i];
    }
  return (TRUE);        /* ray hits box */
}  
2D Games DevelopersФорум

Тема в архиве.