c_intersect(VECTOR* min1, VECTOR* max1, VECTOR* vel1, VECTOR* min2, VECTOR* max2, VECTOR* vel2)

Detects the collision of two moving bounding boxes. Can be used for multiple purposes, for instance for testing whether a ray collides with a box, or for collision detection of moving panels in a 2D game. 8.10

Parameters:

min1, max1 - first bounding box.
vel1 - speed vector of the first bounding box, or NULL for no speed.
min2, max2 - second bounding box.
vel2 - speed vector of the second bounding box A8.11, or NULL for no speed.

Returns:

0 for no collision, -1 for a intersection, distance until collision otherwise.

Speed:

Fast

Remarks:

Example:

// Collision test of a moving panel
VECTOR* pan_min(PANEL* pan)
{
  return vector(pan.pos_x,pan.pos_y,0);
}

VECTOR* pan_max(PANEL* pan)
{
  return vector(pan.pos_x+pan.size_x,pan.pos_y+pan.size_y,0);
}

void main() 
{
// create a blue panel
  PANEL* pan1 = pan_create(NULL,1);
  set(pan1,LIGHT|SHOW);  
  vec_set(pan1.blue,COLOR_BLUE);
  pan1.pos_x = 310;
  pan1.pos_y = 300;
  pan1.size_x = 40;
  pan1.size_y = 40;

// create a red panel
  PANEL* pan2 = pan_create(NULL,2);
  set(pan2,LIGHT|SHOW);  
  vec_set(pan2.blue,COLOR_RED);
  pan2.pos_x = 10;
  pan2.pos_y = 20;
  pan2.size_x = 100;
  pan2.size_y = 150;
  
// move the blue panel with collision detection
  while(1)
  {
    VECTOR speed;
    vec_set(speed,vector(-5,-5,0));
    var dist = c_intersect(pan_min(pan1),pan_max(pan1),speed,
      pan_min(pan2),pan_max(pan2),NULL);
    if(dist) // collision?  
      vec_normalize(speed,dist); // distance vector up to the collision point
    pan1.pos_x += speed.x;  // move panel by remaining distance 
    pan1.pos_y += speed.y;  
    if (dist) break; // break on collision
    wait(1);
  }
  wait(2); // swap screen buffer to foreground, for displaying the actual position 
  printf("Collision!");
}

See also:

c_trace, c_move, c_rotate ► latest version online