验证点是否在3D空间中的圆锥内

Mih*_*tat 7 java android opengl-es formula

考虑:

  • X(x1,y1,z1) 我需要验证它是否在锥体内.
  • M(x2,y2,z2)锥体的顶点.(锥体的顶点)
  • N(x3,y3,z3) 锥体底部中间的点.

我发现如果一个点X在锥体上,它需要验证这个等式:

cos(alfa) * ||X-M|| * ||N|| = dot(X-M,N)
Run Code Online (Sandbox Code Playgroud)

其中dot是2个向量的标量积,而alfa是这两个向量之间的角度.

根据公式,我计算出:

X-M = (x1-x2,y1-y2,z1-z2)
Run Code Online (Sandbox Code Playgroud)

所以,

cos(alfa)
  * Math.sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)
  * Math.sqrt(x3^2 + y3^2+z3^2)
= x3(x1-x2) + y3(y1-y2) + z3(z1-z2)
Run Code Online (Sandbox Code Playgroud)

不幸的是,上述计算似乎给我错误的结果.我究竟做错了什么?

另外我怀疑要检查是否X在锥体内部,我必须放入<=而不是=在公式中.它是否正确?

这个的用法是:我开发了一种游戏,当一个物体处于"视野"时,机枪必须开始射击.这个视图将是一个圆锥体.锥体的顶点将位于机枪中,锥体的底部将位于前方的某个已知距离处.进入这个锥体的任何物体,机枪都会射击它.

fyo*_*iev 11

我完全赞同蒂姆:我们需要锥形的"角度"(光圈)才能得到答案.

那我们做一些编码吧!我将从这里使用一些术语.

结果给予功能:

/**
 * @param x coordinates of point to be tested 
 * @param t coordinates of apex point of cone
 * @param b coordinates of center of basement circle
 * @param aperture in radians
 */
static public boolean isLyingInCone(float[] x, float[] t, float[] b, 
                                    float aperture){

    // This is for our convenience
    float halfAperture = aperture/2.f;

    // Vector pointing to X point from apex
    float[] apexToXVect = dif(t,x);

    // Vector pointing from apex to circle-center point.
    float[] axisVect = dif(t,b);

    // X is lying in cone only if it's lying in 
    // infinite version of its cone -- that is, 
    // not limited by "round basement".
    // We'll use dotProd() to 
    // determine angle between apexToXVect and axis.
    boolean isInInfiniteCone = dotProd(apexToXVect,axisVect)
                               /magn(apexToXVect)/magn(axisVect)
                                 >
                               // We can safely compare cos() of angles 
                               // between vectors instead of bare angles.
                               Math.cos(halfAperture);


    if(!isInInfiniteCone) return false;

    // X is contained in cone only if projection of apexToXVect to axis
    // is shorter than axis. 
    // We'll use dotProd() to figure projection length.
    boolean isUnderRoundCap = dotProd(apexToXVect,axisVect)
                              /magn(axisVect)
                                <
                              magn(axisVect);
    return isUnderRoundCap;
}
Run Code Online (Sandbox Code Playgroud)

下面是我的基本函数的快速实现,上层代码需要操作向量.

static public float dotProd(float[] a, float[] b){
    return a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
}

static public float[] dif(float[] a, float[] b){
    return (new float[]{
            a[0]-b[0],
            a[1]-b[1],
            a[2]-b[2]
    });
}

static public float magn(float[] a){
    return (float) (Math.sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]));
}
Run Code Online (Sandbox Code Playgroud)

玩得开心!