I am trying to implement bins filling algorithm by allowing user to create freehand polygon by drawing lines and then making shape geometry from points/vertices of lines. Then trying to add the bin meshes in scene using min and max of the drawn polygon. After that, I am trying to keep the bins meshes that have collision with drawn polygon and are not interesting with lines.
However, the shape polygon to plane collision doesnt seems to work as expected. It always returns true. which causes all bins loading.
It seems your issue is a bit unclear. Demonstrating the collision test with a simple live example might help to understand what’s going wrong.
BrainstormInstead of checking the intersections, would it be an option to render the polygon to a rendertarget with matching resolution, and use the rendertarget texture to guide the coloring of the bins? Or, similarly, throw a bunch of rays at the polygon and use the results? Hm, none of these solutions will be exact. :thinking:
An exact solution would be, for each bin, to first check whether a line segment from the bin center to outside the grid intersects an odd number of edges. If not, return false. Intersect the bin rectangle with all the edges. If it intersects at least one, return false. Else return true. (The two tests can be swapped, but both must be passed.)
Not necessarily. The polygon could have inward spikes that pierce the edges of the rectangle. But if you check the center point and then intersect the edges against the bin rectangle, you can have an exact solution (as outlined above).
Thanks @EliasHasle. I did not get how to achieve the traversing from bin center to edges. Do i need to use raycaster here? Can you provide some example/link that demonstrate center to edge collision detection?
Also, I had one little doubt here. I think the outside object/panels that are outside the polygon will pass the first test that you mentioned?
Read this topic: [SOLVED] How to find intersection between two Rays?
It’s about intersection of line segments (just line segments, methematical ones , not THREE.LineSegments())
You can use it for checking, if any line segment of bin rectangle instersects with any line segment of the polygon.
Hm, no. If you make sure that the reference point is outside the polygon, all line segments to points outside the polygon will intersect an even (mostly 0) number of polygon edges. All line segments to points inside the polygon will intersect an odd number of polygon edges. This is the principle used in the StackOverflow page you linked. There may be some issues with points that lie on the edges etc., which I guess you can try to sort out.
It is also an option to first check the bounding box of the line segment against the rectangle, and then check if all corners of the rectangle lie on the same side of the line segment, by checking the signs of the dot products of a normal (90 degrees rotated, i.e. swap x,y will do) from the line segment with the vectors from a point on the line segment to each of the corners.