[bb] See if an Entity is in View by Berbank [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:38

Previous topic - Next topic

BlitzBot

Title : See if an Entity is in View
Author : Berbank
Posted : 1+ years ago

Description : Haven't properly checked this yet, but it seems to work.
There are other code fragments out there that do similar things but couldn't find anything that did this in such a specific way. Simply use like this:

If InView(EntityA,EntityB,ViewAngle,RangeValue)
 debuglog("A can see B)
else
 debuglog("A can't see B)
endif

This is for 2D worlds and make sure the objects have zero pitch and Roll (are not rotated on the X or Z axis).

Does not take into account occluding objects.


Code :
Code (blitzbasic) Select
Function InView(a,b,Angle#=90,range#=10000)

; a is the entity looking
; b is the entity being looked for
; Angle# is the view angle in degrees.
; range is the distance the entity can see

If EntityDistance(a,b) > range# Then Return False

dx# = EntityX(a,1) - EntityX(b,1)
dz# = EntityZ(a,1) - EntityZ(b,1)

TFormNormal dx#,0,dz#,0,0
nx# = TFormedX()
nz# = TFormedZ()

TFormNormal 0,0,1,a,0
hx# = TFormedX()
hz# = TFormedZ()

dot# = (nx# * hx#) + (nz# * hz#)

If ACos(dot#) < (180 - (Angle#/2))
Return False
Else
Return True
EndIf

End Function


Comments : none...