Raycasting

Today we finished our protype which was to create a raycasting method to make “light” or a field-of-view if you want. The final prototype is a field of view that spawns in a guard object. That will illustrate what the guard can actually see. I mostly followed this tutorial but I got a big bunchof help from a friend, Johannes Häggqvist which also went on this university which back then was named HGO.

I tried to divide the prototype into smaller steps. First I wanted to have all those corners and knowing where they was and what angle it was between them and the guard.
When I knew that I looked up on vertices which was not so hard when you got a grasp of it.
Basicly, the light is a bunch of triangles that one point lies in the center of the guard and the other two, called beginPoint and endPoint laid out in the field.
The second step was to draw all those lines and make the collide with all segments. This was definitely the hardest part of the prototype(and also the whole game I wanna think). When I managed to make them stop at the segments and corners i finally drew the triangles on the scene. This was a piece of cake becuase all you have to do is to define a sf::VertexArray with 3 points and just draw the thing.
The third step was to limit the vision to a specific area(like a cone shaped figure) . This the funniest step of them all. I did think of multiple wasy to prevent the whole area to be filled and I knew that I had to clip a polygon out of every vertexarray. I googled for solutions on how to clip and then I stumbled upon a great small library called clipper which was a tool for clipping polygons, exacly what I searched for!
So I defined a shape(a cone with 4 points, the last one for the most furthes point, check image) and I just laid that shape over the field of view where the guard was and I clipped it out from the triangles. Now I had a field of vision.
And for the forth and final step I had to be able to rotate the field of view based on the rotation of the guard.
This was hard at first but when I got my head around the problem everything became so simple. I used a 3D matrix so rotate it. I just rotated the 3 points around the given center point.

About Anthon Fredriksson

2013 Programming