Check to see if an entity can see another

Blitz3D Forums/Blitz3D Programming/Check to see if an entity can see another

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

heres an example for you.


Graphics3D 1024,768,0,2
SetBuffer BackBuffer()



scalex# = 3
scaley# = 7

player1 = CreateSphere()
ScaleEntity player1,scalex,scaley,scalex
EntityColor player1,255,0,0

player2 = CreateSphere()
ScaleEntity player2,scalex,scaley,scalex
EntityColor player2,0,0,255

PositionEntity player2,0,0,200

wall = CreateCube()
ScaleEntity wall ,10,10,5
MoveEntity wall ,0,0,30
EntityPickMode wall,2

wall = CreateCube()
ScaleEntity wall ,10,10,5
MoveEntity wall ,-130,0,130
EntityPickMode wall,2

plane = CreatePlane()
ScaleEntity plane,100,100,100
EntityColor plane,0,255,0

cam = CreateCamera()
MoveEntity cam,0,150,-150
PointEntity cam,player1


range# = 400;how far into distance entity can see
FOV# = 2;how much is visible to the entity further away to each side


If use = 0

fov_mesh = CreateMesh()
fov_surf = CreateSurface(fov_mesh)

	v0 = AddVertex(FOV_surf,FOV,0,0)
	v1 = AddVertex(FOV_surf,-(range+1)*FOV,0,range)
	v2 = AddVertex(FOV_surf,(range+1)*FOV,0,range)
	v3 = AddVertex(FOV_surf,-FOV,0,0)
	
	AddTriangle(FOV_surf,v0,v1,v2)
	AddTriangle(FOV_surf,v2,v3,v0)
	
	
	;EntityOrder FOV_mesh,-1
	EntityColor FOV_mesh,255,0,0
	
	PositionEntity FOV_mesh,EntityX(player1,1),EntityY(player1,1),EntityZ(player1,1)
	RotateEntity FOV_mesh,0,EntityYaw(player1),0
	EntityParent FOV_mesh,player1
	EntityAlpha FOV_mesh,.2
	EntityOrder FOV_mesh,-1

EndIf


While Not KeyHit(1)
Cls



If KeyDown(30) MoveEntity player2,-2,0,0
If KeyDown(32) MoveEntity player2,2,0,0
If KeyDown(17) MoveEntity player2,0,2,0
If KeyDown(31) MoveEntity player2,2,-2,0

If KeyDown(203) TurnEntity player1,0,2,0
If KeyDown(205) TurnEntity player1,0,-2,0


RenderWorld

If IsInRadarView(player1,player2,scalex#,scaley#,range#,FOV#) 

	Text 0,0,"player1 can see player2"
	CameraProject cam,EntityX(player1),EntityY(player1),EntityZ(player1)
	px1# = ProjectedX()
	px2# = ProjectedY()
	CameraProject cam,EntityX(player2),EntityY(player2),EntityZ(player2)
	Line px1,px2,ProjectedX(),ProjectedY()

EndIf

Flip

Wend
End







; -------------------------------------------------------------------------------------------------------------------
; -------------------------------------------------------------------------------------------------------------------
Function IsInRadarView(ent1,ent2,radiusx#,radiusy#,range#,FOV#)

TFormPoint EntityX(ent2,1),EntityY(ent2,1),EntityZ(ent2,1),0,ent1

If (TFormedZ()-radiusx)<=range
	If (Abs(TFormedY())-radiusy) <=((TFormedZ()+1)*FOV)
		If (Abs(TFormedX())-radiusx)<=((TFormedZ()+1)*FOV)
			;do visibility check ie entity visible
			If EntityVisible(ent1,ent2) ;True
				Return True
			Else
				Return False
			EndIf
		Else
			Return False
		EndIf
	Else
		Return False
	EndIf
Else
	Return False
EndIf


End Function