I am trying to make a lighting system that keeps track of where you have been by only lightly shading areas that you've seen and darkly shading anything you haven't seen and not shading where you are. The first issue I ran into was the fact that just using a regular circle allowed you to see through/over walls so I decided to raycast 16 points and have them stop when they hit a wall, take the position, make a polygon out of those points and then use that to detect the "fog of war" tiles. Here's my code for the raycasting and polygon generating script:

``````    func sightCheck(light):
if position != oldPos:
delete_children(\$VisionArea)
var space_state = get_world_2d().direct_space_state
PoolVector2Array(collisionPos)
var collisionLengths = [] # this is the array that will store the distance in all 16 directiosn
for theta in range(0,15):
#first it needs to define the vector two for the given angle.
var vector = Vector2(cos((theta+1)*RayAngle)*light,sin((theta+1)*RayAngle)*light)

#RayAngle is set to 22.5 degrees so that after 16 iterations it will have gone a whole circle.

var collision = space_state.intersect_ray(self.position,self.position+map.map_to_world(vector))
if collision:
collisionPos.append(collision.position)
else:
collisionPos.append(self.position+map.map_to_world(vector))
var v = VisionBody.instance()
v.concaveBody(self.position,collisionPos)
for Vi in \$VisionArea.get_children():
Vi.mode = RigidBody2D.MODE_STATIC
``````

and this is the code for the referenced VisionBody:

``````        extends RigidBody2D
var shape = ConcavePolygonShape2D.new()
func concaveBody(pos,points):
position = pos
shape.set_segments(points)
\$CollisionShape2D.shape = shape
``````

and Finally when I ran my program nothing was wrong with compiling but the segments all connected in a terribly:

and then this was it with a ray angle of 45 degrees and only 8 iterations I expected and even 8 pointed polygon.

If you read all of this you're truly a stalwart woman/man.

Thanks,
Xofox

Edit 1: Minor Spelling Issues

asked Oct 2, 2019 in Engine
edited Oct 2, 2019

Since for some reason the images I put in aren't working (thanks imgur) here are just the links:
Image 1: http://imgur.com/a/Eop6pGf
Image 2: https://imgur.com/a/YAzwVbN