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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…