Advertisement
nezvers

Jon Blow BSP tree example

May 25th, 2021
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. bool object_hits_world(BSP_Node *world_node, Polyhedron *solid) {
  2.     if (world_node == NULL) return false;
  3.    
  4.     int status = classify(world_node, solid->center, solid->radius);
  5.  
  6.     if (status & SLICED) {
  7.         if (test_solid_against_polygons(world_node->faces, solid)){
  8.             return true;
  9.         }
  10.     }
  11.  
  12.     if (status & TOUCHES_NEG_HALFSPACE) {
  13.         bool found_hit = object_hits_world(world_node->negative, solid);
  14.         if (found_hit){
  15.             return true;
  16.         }
  17.     }
  18.  
  19.     if (status & TOUCHES_POS_HALFSPACE) {
  20.         bool found_hit = object_hits_world(world_node->positive, solid);
  21.         if (found_hit){
  22.             return true;
  23.         }
  24.     }
  25.  
  26.     return false;
  27. }
  28.  
  29. int classify(BSP_Node *node, Point center, float radius) {
  30.     float dot = dot_product(node->normal, center);
  31.  
  32.     float distance = dot + node->d;
  33.  
  34.     int status = 0;
  35.     if (distance - radius <= 0.0) status |= TOUCHES_NEG_HALFSPACE;
  36.     if (distance + radius >= 0.0) status |= TOUCHES_POS_HALFSPACE;
  37.     if (fabs(distance) <= radius) status |= SLICED;
  38.  
  39.     return status;
  40. }
  41.  
  42. bool test_solid_against_polygons(List *world_polygons, Polyhedron *solid) {
  43.     Polygon *face1, *face2;
  44.     Foreach(world_polygons->face, face1, {
  45.         Foreach(solid->faces, face2, {
  46.             if (polygons_intersect(face1, face2)) return true;
  47.         });
  48.     });
  49.  
  50.     return false;
  51. }
  52.  
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement