bool CheckBoundingBoxWithSphere(R3Sphere *sphere, R3Player *player) { R3Box bbox = player->shape->mesh->bbox; R3Point coords[8]; coords[1] = bbox.Corner(0, 0, 0); coords[2] = bbox.Corner(0, 0, 1); coords[3] = bbox.Corner(0, 1, 0); coords[4] = bbox.Corner(0, 1, 1); coords[5] = bbox.Corner(1, 0, 0); coords[6] = bbox.Corner(1, 0, 1); coords[7] = bbox.Corner(1, 1, 0); coords[8] = bbox.Corner(1, 1, 1); // check if it is entirely within the sphere bool in = true; for (int i = 0; i < 8; i++) { if (R3Distance(coords[i], sphere->Center()) < sphere->Radius()){ in = false; break; } } if (in) return false; for (int i = 0; i < 8; i++) { if (R3Distance(coords[i], sphere->Center()) < sphere->Radius()) return true; } if (R3Distance(player->pos, sphere->Center()) < sphere->Radius()) return true; return false; }
double R3SignedDistance(const R3Plane& plane, const R3Box& box) { // Return signed distance from plane to box int ix = (plane.Normal().X() > 0) ? 0 : 1; int iy = (plane.Normal().Y() > 0) ? 0 : 1; int iz = (plane.Normal().Z() > 0) ? 0 : 1; double d1 = R3SignedDistance(plane, box.Corner(ix, iy, iz)); if (d1 >= 0) return d1; double d2 = R3SignedDistance(plane, box.Corner(1-ix, 1-iy, 1-iz)); if (d2 < 0) return d2; else return 0.0; }
RNBoolean R3Contains(const R3Sphere& sphere, const R3Box& box) { // Return whether sphere contains box R3Vector v = box.Centroid() - sphere.Center(); R3Point corner = box.Corner(v.Octant()); return R3Contains(sphere, corner); }
RNBoolean R3Contains(const R3Ray& ray, const R3Box& box) { // Return whether ray contains box if (!R3Contains(ray.Line(), box)) return FALSE; RNOctant octant = ray.Vector().Octant(); if (!R3Contains(ray, box.Corner(~octant & 0x7))) return FALSE; return TRUE; }
RNBoolean R3Contains(const R3Box& box, const R3Halfspace& halfspace) { // Return whether box contains halfspace RNOctant octant = halfspace.Normal().Octant(); R3Point corner = box.Corner(octant); if (!RNIsFinite(corner.X())) return FALSE; if (!RNIsFinite(corner.Y())) return FALSE; if (!RNIsFinite(corner.Z())) return FALSE; RNAbort("Not Implemented"); return FALSE; }
RNBoolean R3Contains(const R3Halfspace& halfspace, const R3Box& box) { // Return whether halfspace contains box RNOctant octant = halfspace.Normal().Octant(); return (R3Contains(halfspace, box.Corner(~octant & 0x7))); }