detecting a 3d conical range in front of an entity

Blitz3D Forums/Blitz3D Programming/detecting a 3d conical range in front of an entity

Craig H. Nisbet(Posted 2008) [#1]
Anyone know the math, or a trick, for detecting if an entity is within a 3d conical range in front of or at any angle relative to another entity?

I'm trying to write a 3d space sim and I don't know how to check if the target ship is in front of the attacking ship in 3d space. I figured detecting the target within a cone in front of the attacker would be the best approach. Any ideas?




Naughty Alien(Posted 2008) [#2]
..i would suggest use of trigger(checking entity inside of given primitive parented to for example, head of your character)..use it with physics engines and forge about any problems..thats what i did anyway..


Warner(Posted 2008) [#3]
If you want to solve it with maths, you could use this approach:

1. I would take the target's coordinate, and use TFormPoint to convert them from world space into the Attacker's space.

That means that if Target is on the same place as the attacker, x,y,z will be 0,0,0.
If Target is right in front of Attacker, so that means if the Target is on the center axis of the imaginary cone you drew, x and y will still be zero, and z will be the distance between the two objects.
Basically, after using TFormPoint, x and y will give the distance from Target to the center axis of the cone, and z will give the distance from Target to Attacker (from the closest point on the z-axis).

2. Then, you can first check if z is in the range 0 to 2, and if it is, you can calculate if x*x+y*y < z*z.
If that is the case, the coordinates lie within the cone.

sqr(x*x+y*y) is the distance from the Target to the center of the cone.
z*z is the radius of the cone: the further away from Attacker, the bigger the radius of the cone should be. So in this case radius = z.


Here is a working example:



Craig H. Nisbet(Posted 2008) [#4]
Wow! That's awesome! Thanks!


puki(Posted 2008) [#5]
"fredborg" has a code archive for this too.


_PJ_(Posted 2008) [#6]
So..er...call me stoopid, but could the same be done with a sphere around the origin by simply changing the cone entity to a sphere? or if for whatever weird reasosn,s maybe a cube/cylinder/fish ?


Warner(Posted 2008) [#7]
No, each shape should have it's own function.
For a sphere, you can use EntityDistance(), because if the distance between a point and the center of the sphere is smaller than the radius of the sphere, the point lies within the sphere, else it lies on the outside.

For a cube, the comparisation should be more along the lines of:

isoutside = (x < left) or (x > right) or (z < front) or (z > back) or (y < top) or (y > bottom)

For a cylinder, the code would be similair to the cone, except this line:
if (x*x+y*y)<=(z*z) then test = true

should have a fixed radius, like this:
if (x*x+y*y)<=(2*2) then test = true

Because a cylinder has the same radius along it's entire length.

A fish-shaped detection area could maybe best be build up from basic shapes, such as cube,cylinder,sphere etc. Or maybe sin() or cos() if you're thinking about a yin-yang shaped fish:



puki(Posted 2008) [#8]
Actually, it wasn't "fredborg" - might have been "shambler". I'll see if I can find it.

EDIT: nope, wasn't "shambler".