Cube sphere intersection test?

后端 未结 3 549
鱼传尺愫
鱼传尺愫 2020-12-14 12:47

What\'s the easiest way of doing this? I fail at math, and i found pretty complicate formulaes over the internet... im hoping if theres some simpler one?

I just need

3条回答
  •  孤街浪徒
    2020-12-14 13:18

    Jim Arvo has an algorithm for this in Graphics Gems 2 which works in N-Dimensions. I believe you want "case 3" at the bottom of this page: http://www.ics.uci.edu/~arvo/code/BoxSphereIntersect.c which cleaned up for your case is:

    bool BoxIntersectsSphere(Vec3 Bmin, Vec3 Bmax, Vec3 C, float r) {
      float r2 = r * r;
      dmin = 0;
      for( i = 0; i < 3; i++ ) {
        if( C[i] < Bmin[i] ) dmin += SQR( C[i] - Bmin[i] );
        else if( C[i] > Bmax[i] ) dmin += SQR( C[i] - Bmax[i] );     
      }
      return dmin <= r2;
    }
    

提交回复
热议问题