Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

java - Verify if point is inside a cone in 3D space

Consider:

  • X(x1,y1,z1) the point I need to verify if it is inside a cone.
  • M(x2,y2,z2) the vertex of the cone. (the top point of the cone)
  • N(x3,y3,z3) the point in the middle of the cone's base.

I found out that if a point X is on the cone, it needs to verify this equation:

cos(alfa) * ||X-M|| * ||N|| = dot(X-M,N)

Where dot is the scalar product of 2 vectors, and alfa is the angle between these 2 vectors.

Based on the formula, I calculated that:

X-M = (x1-x2,y1-y2,z1-z2)

So,

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)

Unfortunatelly the above calculations seem to give me wrong results. What am I doing wrong?

Also I suspect that to check if X is inside the cone, I have to put <= instead of = in the formula. Is this correct?

The usage of this is: I develop a game where a machine gun has to start firing when an object is in its 'view'. This view will be a cone. The cone's vertex would be in the machine gun, the base of the cone will be at some known distance ahead. Any object entering this cone, the machine gun will shoot it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I totally agree with Tim: we need "angle" (aperture) of cone to get the answer.

Let's do some coding then! I'll use some terminology from here.

Result-giving function:

/**
 * @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;
}

Below are my fast implementations of basic functions, required by the upper code to manipulate vectors.

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]));
}

Have fun!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...