2D Field of View algorithm implemented in GDScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
FOV.gd initial commit Feb 21, 2018
LICENSE Initial commit Feb 21, 2018
README.md Update README.md Feb 21, 2018
fov_example.png added visualization Feb 21, 2018

README.md

Visibility Polygon

Simple 2D Field of View/Line of Sight algorithm implemented in gdscript (the Godot Engine Scripting language).

visualization of simple fov examples

Algorithm

This implementation is based on this 2d Visibility article on the Red Blob Games website. It also draws heavily from Trylock's C++ implementation

Preconditions

  • The line segments must not intersect except at their endpoints
  • The visiblity polygon has to be closed.

Behaviour is undefined if the preconditions aren't met. Line segment intersections can be solved by first splitting segments at intersection points. The visibility polygon can be guaranteed closed if you add an additional bounding box around your scene.

Godot Example

This sample will draw a blue polygon indicating the computed FOV from the node's origin.

All occluding objects are assumed to have a polygon node called occlusion_mask and be a member of the fov_occluders group.

func _draw():
	fov()

func get_occluder_polygons():
	var occluders = get_tree().get_nodes_in_group('fov_occluders')
	var polygons = []
	for o in occluders:
		var mask = o.get_node('occlusion_mask')
		if mask != null:
			var poly = mask.polygon
			for i in range(len(poly)):
				poly[i] += (o.position - self.position)
			polygons.append(poly)
	return polygons

func fov():
	var fov_computer = get_node('FOV')
	var polygons = get_occluder_polygons()
	var fov_center = Vector2(0,0)
	var fov_poly = fov_computer.get_fov_from_polygons(polygons, fov_center)
	draw_colored_polygon(fov_poly, Color(0,0,255))