2
\$\begingroup\$

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.

enter image description here

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:

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – user1118321 Feb 23 '16 at 5:46
  • \$\begingroup\$ 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. \$\endgroup\$ – Yosh Synergi Feb 23 '16 at 10:00
  • \$\begingroup\$ Have you looked at this? redblobgames.com/articles/visibility \$\endgroup\$ – zcabjro Feb 23 '16 at 10:51
2
\$\begingroup\$

First sort all the intersection points either clockwise or anticlockwise around the origin. There are many answers here on gamedev that will help you out in doing that. Just add one more condition that if two points are on the same line (angle is the same), the less distant point will be pushed to the array first.

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.

NOTE: From your second image, it can be seen that your ray intersection test doesn't handle all vertex cases correctly (maybe due to floating point errors), which I think you should fix.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Yeah I'm aware. I've decided to rewrite the ray casting to be more efficient. Thanks for your help. \$\endgroup\$ – Jordan Schnur Feb 24 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.