Help drawing a Polygon with RayCasting

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
LuisLJLV
Prole
Posts: 5
Joined: Tue Sep 07, 2021 8:08 pm

Help drawing a Polygon with RayCasting

Post by LuisLJLV »

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
(2.12 KiB) Downloaded 75 times
rays.lua
(5.27 KiB) Downloaded 70 times
main.lua
(2.71 KiB) Downloaded 71 times
User avatar
pgimeno
Party member
Posts: 2819
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help drawing a Polygon with RayCasting

Post by pgimeno »

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

Post by LuisLJLV »

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

Post by LuisLJLV »

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
Screenshot_2.png (259.88 KiB) Viewed 2981 times
Screenshot_12.png
Screenshot_12.png (9.53 KiB) Viewed 2982 times
main.lua
(3.32 KiB) Downloaded 63 times
User avatar
pgimeno
Party member
Posts: 2819
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help drawing a Polygon with RayCasting

Post by pgimeno »

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.
User avatar
darkfrei
Party member
Posts: 417
Joined: Sat Feb 08, 2020 11:09 pm

Re: Help drawing a Polygon with RayCasting

Post by darkfrei »

For the full hd you are need 2x(1920+1080) rays, not more. Just targeted to every pixel on the every screen side.
:awesome: Falling in LÖVE :awesome:
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

Post by LuisLJLV »

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

Who is online

Users browsing this forum: No registered users and 38 guests