Code archives/3D Graphics - Maths/See if an Entity is in View

This code has been declared by its author to be Public Domain code.

Download source code

See if an Entity is in View by Berbank2005
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.
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.

Code Archives Forum