Check to see if an entity can see another

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

(tu) sinu(Posted 2006) [#1]
EDIT:A function to see if an entity is in view of another in 3d

Function IsInRadarView(ent1,ent2,radiusx#,radiusy#,range#,fov#)

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

If (TFormedZ()-radiusx)<=range
	If (Abs(TFormedY())-radiusy) <=((TFormedZ()+1)*fov)
		If (Abs(TFormedX())-radiusx)<=((TFormedZ()+1)*fov)
			;if EntityVisible(ent1,ent2)
				Return True
			;else
				;Return False
			;EndIf
		Else
			Return False
		EndIf
	Else
		Return False
	EndIf
Else
	Return False
EndIf


End Function



John Blackledge(Posted 2006) [#2]
This looks really interesting.
Can you give us a quick example call, with comments on the parameters?


(tu) sinu(Posted 2006) [#3]
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




n8r2k(Posted 2006) [#4]
[mental note] check this out when i get home [/mental note]
Now that you have read my mind, are you going to add this to the code archive? It would be found easier there you know, instead of being buried alive in this forum


(tu) sinu(Posted 2006) [#5]
Slight edit to the demo, added a plane etc, works in full 3d, ie up/down.

A higher fov allows the entity to see more out of the corner of his eye and below above him.


John Blackledge(Posted 2006) [#6]
That's amazing.

Can I take it that it would be used in a typical (Doom-like) situation of 'enemy sees character, therefore'.

Can I also take it that EntityVisible would not achieve this effect. Why not?


(tu) sinu(Posted 2006) [#7]
Entityvisible checks if one entity can see another not matter where it is based around the entity, be it in front, far to the side or 100 hundred units directly below or way behind it.

This method only allows you to see the entity if it's in your fov like a real person.
The fov and range can be changed to give better or worse sight.


GfK(Posted 2006) [#8]
You can also use DeltaPitch/Yaw to determine if an entity is in the 'line of sight' of another entity. If it is, you can then do an EntityVisible to work out if there are objects between the two.

Function CanSeeEntity(EntityA,EntityB)
  Local FOV = 70
  Local Result = False
  If Abs(DeltaYaw(EntityA,EntityB)) < FOV
    If Abs(DeltaPitch(EntityA,EntityB)) < FOV
      If EntityVisible(EntityA,EntityB))
        DebugLog "I can see you, Kirk.  Can you see me?"
        Result = True
      EndIf
    EndIf
  EndIf
  Return Result
End Function



John Blackledge(Posted 2006) [#9]
Nice one, sinu!

This has gone into my folder of 'code to add next time I do a re-write'.

You should add it to the Code Archives.


(tu) sinu(Posted 2006) [#10]
@GFK, does yours work the same as mine?
Is yours faster/better?
Never knew of the delta commands.


(tu) sinu(Posted 2006) [#11]
The delta commands are pretty neat.


Beaker(Posted 2006) [#12]
http://www.blitzbasic.com/codearcs/codearcs.php?code=532