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.
test = false
;make coordinates relative to attacker
TFormPoint EntityX(target), EntityY(target), EntityZ(target), 0, Attacker
x# = TFormedX()
y# = TFormedY()
z# = TFormedZ()
;check if z is in front of attacker, and not too far away
if (z >= 0) and (z <= 2) then
;check if distance between point and center of cone is smaller than 'z'
;which means that at the base of the cone, they should be zero, and at
;then end of the cone, they should be smaller than 2.
if (x*x+y*y)<=(z*z) then test = true
end if
Here is a working example:
Graphics3D 800, 600, 0, 2
SetBuffer BackBuffer()
;create mesh
mesh = CreateSphere()
ScaleMesh mesh, 0.1, 0.1, 0.1
;create cone (just for visualisation, not used)
cone = CreateCone()
RotateMesh cone, 90, 0, 0
EntityAlpha cone, 0.1
EntityParent cone, mesh
PositionEntity cone, 0, 0, -1
;create camera
cpv = CreatePivot()
cam = CreateCamera(cpv)
MoveEntity cam, 0, 0, -5
;create base dot model
org = CreateSphere()
ScaleMesh org, 0.02, 0.02, 0.02
HideEntity org
;create several dots
piv = CreatePivot()
For x# = -1 To 1 Step 0.25
For y# = -1 To 1 Step 0.25
For z# = -1 To 1 Step 0.25
nw = CopyEntity(org, piv)
PositionEntity nw, x, y, z
Next
Next
Next
;main loop
Repeat
;turn camera with cursor keys
TurnEntity cpv, KeyDown(208) - KeyDown(200), KeyDown(205) - KeyDown(203), 0
;loop through all dots (or entities that you want to check)
For i = 1 To CountChildren(piv)
nw = GetChild(piv, i)
;reset flag
sel = 0
;check if 'nw' is inside cone
TFormPoint EntityX(nw), EntityY(nw), EntityZ(nw), 0, mesh
x# = TFormedX()
y# = TFormedY()
z# = TFormedZ()
;check if 'z' is in cone range (0..-2)
If (z <= 0) And (z >= -2) Then
;check if 'x' and 'y' are in cone radius
z = z / 2.0
If (x * x + y * y) < z * z Then sel = 1 ;set flag
End If
;check flag
If sel = 1 Then
;make dot red
EntityColor nw, 255, 0, 0
EntityAlpha nw, 1.0
Else
;make dot semi-invisible
EntityColor nw, 255, 255, 255
EntityAlpha nw, 0.02
End If
Next
;turn mesh (and cone along with it)
TurnEntity mesh, 0, 1, 0
;render
RenderWorld
Flip
;esc = exit
Until KeyHit(1)
End