# Dynamically generating triangles from points

I'm working on 2D lighting.

I have a list of points that have been calculated and when I draw lines from the light origin to the point it works. The problem is connecting the points into triangles. It always overlaps the walls, and I can't figure this it.

The problem not being the drawing, but connecting the points as to not overlap the map is my problem.

This is my code that generates the vertices

for (int i = 0; i < rays.size(); i++) {
int roffset = (i * 2);
glm::vec3 ray = rays[i];

glm::vec3 ray2 = rays[i+1];

if (i == rays.size()-1) {
ray2 = rays[0];
}
pointsV2.push_back(ray.x);
pointsV2.push_back(ray.y);
int offset = (6* i);

lines[offset + 0] = ray.x;
lines[offset + 1] = ray.y;
lines[offset + 2] = mX;
lines[offset + 3] = mY;
lines[offset + 4] = ray2.x;
lines[offset + 5] = ray2.y;

}


The rays are sorted clockwise. I'm not sure what else to include, but let me know if I need to give something else.

These are just the lines:

• I don't understand at all what you're trying to do. You want to connect the light to points in your scene? What do the points represent? What are the triangles you're trying to create? What are the triangles you're creating overlapping? I don't understand the pictures you've posted. – user1118321 Feb 23 '16 at 5:46
• For this you will need a pathfinding algorithm and your objects need to have a polygon definition for the pathfinding algorithm to identify. You then put all your points clockwise into the pathfinding algorithm and you get as an result a polygon which connects all points but does not intercept with your objects. There are plenty in the Unity Asset Store. – Yosh Synergi Feb 23 '16 at 10:00
• Have you looked at this? redblobgames.com/articles/visibility – zcabjro Feb 23 '16 at 10:51

Once you have done that, finding each triangle is easy. A triangle is made of two adjacent points in the sorted array and the origin point. If the points are collinear (abs(triangle_area) <= epsilon), then don't render the triangle.