• 2D
  • FOV programming for a limited state AI

Hello again,

I'm current learning how to implement various complexities of AI into my games, and as such I want to understand FOV and FOV interactions, I've created a basic state machine that switches between an Idling, chasing, and firing state (with a fourth "searching" state too act as a bridge in case the player steps just out of range).

Now, the game is top-down and completely bare-bones atm, just a grey background, and the player + AI scenes dropped on it. Here's what I want it to do: While idling, patrol in a continuous circle, looking for hostiles withing a limited cone of view that correlates to the front of the sprite, if a player is detected go to chasing While chasing, rotate smoothly and move toward the player until the player is in range. Once the player is in range, switch to fire. While firing, shoot a projectile from the sprites gun barrel that collides with the player, if the player escapes, go to chasing, if the player escapes that go to searching. while searching, continue traveling in the last known direction with a wider FOV then go back to idling.

And here is what I've done so far: Transition logic A radius detector The bullet and movement system (turned off for testing purposes)

My question then is, how do I go about creating a dynamic FOV that I can rotate with the AI? (i found one alternative on the asset store but it seemed to be quite bulky and confusing to understand).

ill supply the code in the following post if it's needed.

FOV logic in AI is often implemented using the dot product (Vector2.dot()/Vector3.dot()). See Vector math in the documentation for more information.

8 days later

yeah, I'm using the dot product of enemy and player position, but I can't seem to get the angles right, not certain where the issue lies.

Apologies for not responding for so long, I've been very busy for the last week or so.

Here are some excerpts of my code just to showcase what I'm doing:

var viewFOV = 180
var viewR = 300

var ray = {"position": Vector2.ZERO} #these are temporary storage so the code doesn't break
var searchray = {"position": Vector2.ZERO}

onready var player_dir = global_position-get_owner().get_parent().get_node("player").global_position
onready var player_pos = get_owner().get_parent().get_node("player").position
onready var space_state = get_world_2d().direct_space_state

func _Searchlight():
	if position.distance_to(player_pos) < viewR:
			var player_ang_search = rad2deg(direction.normalized().angle_to(player_dir))
			print(player_ang_search)<br />
			if abs(player_ang_search) > rotation_degrees and 		rotation_degrees+viewFOV/2 > abs(player_ang_search):
				searchray = space_state.intersect_ray(player_pos, global_position)
				return true
		return false

also apologies for the crappy markup, i can't seem to get the code highlighting to work, so I hope this isn't too unreadable.

@Same said: also apologies for the crappy markup, i can't seem to get the code highlighting to work, so I hope this isn't too unreadable.

I fixed it for you :smile: You can have a chunk of text be formatted as code by either indenting the entire block by a single tab (what I did in your post) or by using three ~ at either side of the block.

Also, in the future, please refrain from cursing, as it is against forum rules. Thanks :smile:

Also, in the future, please refrain from cursing, as it is against forum rules. Thanks :smile: ah, thank you, I'll remember that. Won't do it again.

Okay, So essentially what's going wrong is canvas strangeness, my "direction" variable that defines the way the tank is facing rotates with the canvas, but the angle my check finds doesn't, I check for the direction of the player by subtracting the position of the one from the other, but since I'm rotating the canvas this seems to be messing with it? I'm not certain...

What I do know is that the Fov is stuck facing one direction so being able to rotate it would solve the issue but I've sat here for an hour now scratching my head at how to go about that, I'm certain it's been solved before, but all I've found online is how to do it with stationary enemies, when begin moving and rotating them stuff gets funky.

5 days later

resolved it, the issue was that I was using rotation instead of global_rotation to find the facing direction