overhead view 2D line of sight cones

BlitzMax Forums/BlitzMax Programming/overhead view 2D line of sight cones

Robert Cummings(Posted 2009) [#1]
Hiya,

In my game which is like cannon fodder, I want the enemies to have view cones. Since there will be a lot of enemies how do I do this fast?

They have to have a limited angle to their view cones, something like 80 degrees of vision and a limited distance to it, and objects/level things like buildings or trees should prevent the player being seen.

I have not got any idea about what methods I should use to make this fast. Buildings and objects can use any kind of collision like lines.

So I was thinking of maybe doing raycasts (line to line collision checks) beamed out in an arc.. it would be very slow though.

Any suggestions will be welcome!


Robert Cummings(Posted 2009) [#2]
This one works for Blitz3D but I am doing it in blitzmax and it is 2D and there's no TFormVector / TformedX() etc commands... and no linepick so probably barking up the wrong tree.

Function LineOfSight3D(observer,target,viewrange#=10.0,viewangle# = 90.0)

	;distance between observer and target
	Local dist# = EntityDistance(observer,target)

	;check if the target is within viewrange 
	If dist<=viewrange
		
		;observer vector
		TFormVector 0,0,1,observer,0
		Local ox# = TFormedX()
		Local oy# = TFormedY()
		Local oz# = TFormedZ()
	
		;pick vector
		Local dx# = (EntityX(target,True)-EntityX(observer,True))/dist#
		Local dy# = (EntityY(target,True)-EntityY(observer,True))/dist#
		Local dz# = (EntityZ(target,True)-EntityZ(observer,True))/dist#

		;dot product
		Local dot# = ox*dx + oy*dy + oz*dz

		;check if the target is within the viewangle
		If dot => Cos(viewangle/2.0)
			; check if something is blocking the view
			If LinePick(EntityX(observer,True),EntityY(observer,True),EntityZ(observer,True),dx*viewrange,dy*viewrange,dz*viewrange,0.01)=target
				; observer can see target
				Return True
			End If
		End If
		
	End If

	; observer cannot see target	
	Return False

End Function



matibee(Posted 2009) [#3]
Get your enemies in a quadtree that rebuilds every time they move, then you should be able to query the tree for nodes by distance. For real speed it would help if you could consider your enemies as points rather than circles/rects but it's no show stopper if you can't.

From the quadtree query;

Eleminate nodes that are behind the plane of the agent.

Go through each remaining node, and the enemies it, in this order;
check for behind the agent plane
check them for distance
check if in the field of view

Which is about the cheapest first elimination.

I posted a quadtree demo ages ago.. check out the second sample
[a /posts.php?topic=85634]

Cheers
Matt


Mr. Write Errors Man(Posted 2009) [#4]
You don't need to check LOS every frame. Perhaps once a second.

First check the distance and angle between player and enemy. If it's a check, then you need only check a single line between player and enemy.

I can't imagine how it could cause any kind of speed issues.


slenkar(Posted 2009) [#5]
yes some vector commands in the code archives will help you check the angle