## Help drawing a Polygon with RayCasting

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
LuisLJLV
Prole
Posts: 5
Joined: Tue Sep 07, 2021 8:08 pm

### Help drawing a Polygon with RayCasting

Hi! i'm new to the forums and relatively new to Love2D. The thing at this is that i wanted to approach a sort of RayCasting by getting al the start and end points of my lines segments (stored in a table named "R") and getting their angles in update, then insert all the intersection points of my rays in a table, sort them and draw a polygon with them as points, but i can't draw it, i don't know why or what i'm doing wrong.

If anyone can help me i wll appreciate it a lot.

(Sorry for the bad english, not my lang)
Attachments
vecs.lua
rays.lua
main.lua
pgimeno
Party member
Posts: 2819
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Help drawing a Polygon with RayCasting

Hi Luis,

There are several issues with this.

One is that you're not clearing the table intersections_ on every iteration. Therefore, when you have fewer intersections than before, the previous ones remain in the table.

Another one is that points seem to be duplicated in the intersections_ table. As a consequence, the line join algorithm gets confused and doesn't draw lines between your points when you use line mode. A quick and dirty solution to see the lines is to use love.graphics.setLineJoin("none"). Anyway, you have line mode disabled, so that's only for when you enable it. The correct thing to do would be to identify why duplicates are being generated and avoid that.

Another one is that love.graphics.polygon is generally unable to draw nonconvex polygons. Still, for this application you might be able to get away with it, because Löve's polygon algorithm uses fan mode, which is appropriate for drawing this particular polygon. But you need to know what vertex does Löve choose as the fan origin, and I don't know that.

A solution would be to use a mesh in fan mode instead of a polygon, so that you have control over which vertex is drawn first. Don't forget to add the centre as the first vertex. See love.graphics.newMesh and MeshDrawMode.
LuisLJLV
Prole
Posts: 5
Joined: Tue Sep 07, 2021 8:08 pm

### Re: Help drawing a Polygon with RayCasting

Thanks for your answer, pgimeno, i will try to solve those issues.
LuisLJLV
Prole
Posts: 5
Joined: Tue Sep 07, 2021 8:08 pm

### Re: Help drawing a Polygon with RayCasting

pgimeno wrote: Wed Sep 08, 2021 10:46 am Another one is that love.graphics.polygon is generally unable to draw nonconvex polygons. Still, for this application you might be able to get away with it, because Löve's polygon algorithm uses fan mode, which is appropriate for drawing this particular polygon. But you need to know what vertex does Löve choose as the fan origin, and I don't know that.

A solution would be to use a mesh in fan mode instead of a polygon, so that you have control over which vertex is drawn first. Don't forget to add the centre as the first vertex. See love.graphics.newMesh and MeshDrawMode.
Same result using Meshes. I suppose that the problem is the way how i insert the intersections in the "intersections" table. I did it that way because table.insert() increase the size of the table and i dont want that.

Casting rays to all the directions seems to work fine, but it is very slow, so i wanted to do it this way but i am for real stuck at this point.
Attachments
Screenshot_2.png (259.88 KiB) Viewed 2979 times
Screenshot_12.png (9.53 KiB) Viewed 2980 times
main.lua
pgimeno
Party member
Posts: 2819
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Help drawing a Polygon with RayCasting

You still have three immediate problems. First, you've forgotten to add the mouse position to the mesh as the first vertex. Second, the mesh is much longer (100,000 rays? seriously?) than the table, and you're not using setDrawRange to limit them, so old rays stay in the mesh and are drawn. Third, for the fan to complete the circle, you need to add the second vertex at the end of the mesh. All three problems can be fixed by adding this code at the end of the loop that sets mesh vertices:

Code: Select all

    mesh:setVertex(1, love.mouse.getPosition())
mesh:setVertex(n, mesh:getVertex(2))
mesh:setDrawRange(1, n)

Once all that is fixed, it works for me, but there seems to be some lag with the mouse position that causes flicker. I don't quite understand how your code is organized, it seems you're passing arguments through one-letter globals, but ordering the actions in a logical sequence, and using the actual position of the mouse in the calculations rather than a lagged position, would probably fix that.
darkfrei
Party member
Posts: 417
Joined: Sat Feb 08, 2020 11:09 pm

### Re: Help drawing a Polygon with RayCasting

For the full hd you are need 2x(1920+1080) rays, not more. Just targeted to every pixel on the every screen side.
Falling in LÖVE
I Löve Lua and Love2D
LuisLJLV
Prole
Posts: 5
Joined: Tue Sep 07, 2021 8:08 pm

### Re: Help drawing a Polygon with RayCasting

Thanks Pgimeno, i'm new using Meshes (and using Love2d) i didn't knew how to set it up. Thanks for your answer.

### Who is online

Users browsing this forum: Google [Bot] and 62 guests