November 25, 2020, 08:42:07 AM

### Author Topic: [bb] See if an Entity is in View by Berbank [ 1+ years ago ]  (Read 568 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] See if an Entity is in View by Berbank [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
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
1. Function InView(a,b,Angle#=90,range#=10000)
2.
3.         ; a is the entity looking
4.         ; b is the entity being looked for
5.         ; Angle# is the view angle in degrees.
6.         ; range is the distance the entity can see
7.
8.         If EntityDistance(a,b) > range# Then Return False
9.
10.         dx# = EntityX(a,1) - EntityX(b,1)
11.         dz# = EntityZ(a,1) - EntityZ(b,1)
12.
13.         TFormNormal dx#,0,dz#,0,0
14.         nx# = TFormedX()
15.         nz# = TFormedZ()
16.
17.         TFormNormal 0,0,1,a,0
18.         hx# = TFormedX()
19.         hz# = TFormedZ()
20.
21.         dot# = (nx# * hx#) + (nz# * hz#)
22.
23.         If ACos(dot#) < (180 - (Angle#/2))
24.                 Return False
25.         Else
26.                 Return True
27.         EndIf
28.
29. End Function