Grid-only pathfinding only working on top left ?

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
LuaCre
Prole
Posts: 27
Joined: Mon Dec 03, 2018 11:21 am

Grid-only pathfinding only working on top left ?

Post by LuaCre »

I've been working on a side project in my spare time and got around to needing pathfinding for that project, but couldn't find one as simple for my often sleep deprived brain to drag and drop as I would like, and thought I'd try and make one.

Well I've tried and failed. The little robo-brain seems to follow its logic well, but only for the upper-left quadrant of the grid.
Other than that it gets stuck in an infinite loop at various distances, seemingly refusing to choose an optimal move.

The downsides to my current setup is I cannot generate the path step by step, its all at once or if it fails it will crash.
The initial hopes were that with simple print statement debugging and a couple alternate method attempts would get a working enough version to iron out and clean up.

The way it attempts to generate a path is pretty easy to follow,
it tabulates all possible moves (up left right and down) that are in bounds,
it then examines that table, removing any moves that are farther than we already are from the target
it then picks an axis to move on, based on which one is furthest or random if equal
Then it finds a possible move matching the axis and adds it to the path
Checks to see if the path is at an appropriate distance from the target, and if so breaks the loop.

If you think you can provide any help, let me know, heres the love file
Grid Pathing.love
(23.53 KiB) Downloaded 32 times
Cheers!
User avatar
BrotSagtMist
Party member
Posts: 641
Joined: Fri Aug 06, 2021 10:30 pm

Re: Grid-only pathfinding only working on top left ?

Post by BrotSagtMist »

Interesting approach, and by that i mean, Pythagoras in pathfinding? How is this even supposed to work at all?
There is a lot going on in this file that is distracting from the actual path generator, you need to unclutter it.
But i do think adding the directions has a flaw, swapping < for > at one point made down work for example. Might make sense if you actually draw the steps to the screen to see exactly whats going on.

Anyway, here is a reference pathfinder in case you get stuck with that approach. And i am certain it will once you have to move around objects.
https://love2d.org/forums/viewtopic.php ... 72#p249372

edit: Yesyes some > and < are wrong.
You also want to add an emergency exit: Count how many circles it did then compare it with #path so it wont get stuck.
Image
obey
LuaCre
Prole
Posts: 27
Joined: Mon Dec 03, 2018 11:21 am

Re: Grid-only pathfinding only working on top left ?

Post by LuaCre »

Lol yeah I overcomplicated and made things messy trying to make it work/debug it. I just rewrote the entire thing with simplicity in mind and it works really easily (especially since I was able to go step by step), and this little pathfinder doesnt need to worry about walls, though one in the future will :) thanks for the assistance
User avatar
BrotSagtMist
Party member
Posts: 641
Joined: Fri Aug 06, 2021 10:30 pm

Re: Grid-only pathfinding only working on top left ?

Post by BrotSagtMist »

Also note that your code, while working, is pretty weird in lua standards.

while (pathcomplete ~= true) do >> while not pathcomplete do --easier readable
if mouseover ~= nil then >> if mouseover then --same
print("choice:"..choice) >> print("choice:",choice) -- much faster for skipping a string operation
local pathcomplete = false >> local pathcomplete -- inits as nil anyway
math.sqrt( (x2-x1)^2 + (y2-y1)^2 ) >> ( (x2-x1)^2 + (y2-y1)^2 )^.5

You should take a look at PIL at lua.org
obey
User avatar
dusoft
Party member
Posts: 555
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Grid-only pathfinding only working on top left ?

Post by dusoft »

Why, oh, why?
Just use the grid path finding library such as this one instead of reinventing the wheel:
https://github.com/Yonaba/Jumper
User avatar
BrotSagtMist
Party member
Posts: 641
Joined: Fri Aug 06, 2021 10:30 pm

Re: Grid-only pathfinding only working on top left ?

Post by BrotSagtMist »

One will keep failing to create anything useful if one keeps trying to use random internet stuff instead taking an hour time to do it proper oneself.
obey
LuaCre
Prole
Posts: 27
Joined: Mon Dec 03, 2018 11:21 am

Re: Grid-only pathfinding only working on top left ?

Post by LuaCre »

BrotSagtMist wrote: Sun Feb 26, 2023 10:09 pm One will keep failing to create anything useful if one keeps trying to use random internet stuff instead taking an hour time to do it proper oneself.
I agree, and I've used jumper before but I was curious about the process. I've gotten a very surprised response from my crappy, sleep deprived code that I've shared here haha, and Im confused about it.
dusoft wrote: Sun Feb 26, 2023 9:47 pm Why, oh, why?
Just use the grid path finding library such as this one instead of reinventing the wheel:
https://github.com/Yonaba/Jumper
Well, I've used jumper but I wanted to create something on my own here, and possibly save some overhead from my extremely small side project
LuaCre
Prole
Posts: 27
Joined: Mon Dec 03, 2018 11:21 am

Re: Grid-only pathfinding only working on top left ?

Post by LuaCre »

BrotSagtMist wrote: Sun Feb 26, 2023 8:05 pm Also note that your code, while working, is pretty weird in lua standards.

while (pathcomplete ~= true) do >> while not pathcomplete do --easier readable
if mouseover ~= nil then >> if mouseover then --same
print("choice:"..choice) >> print("choice:",choice) -- much faster for skipping a string operation
local pathcomplete = false >> local pathcomplete -- inits as nil anyway
math.sqrt( (x2-x1)^2 + (y2-y1)^2 ) >> ( (x2-x1)^2 + (y2-y1)^2 )^.5

You should take a look at PIL at lua.org
I tried to reply to your post but hit report and typed my reply and sent it, I apologize,

I know my code is weird in allot of cases, but this code is just awful because I made it on no sleep while very stressed out from said lack of sleep, I find it a little rude you sent me to the Lua book XD (like we havent all clicked a Lua.org link for quick syntax reference right) I've been programming as a hobby for over six years, lua being the primary language, it doesnt mean I write my best code every day of the year haha, I focus on allot of other stuff now and this skill only comes out sometimes.

But as I said in the first reply, I did rewrite this code, its much better now, I appreciate the advice, I know the code I shared is bad, I said so in the original post and the documentation of said code. I really didnt want advice on that. More on pathfinding solutions that aren't just dragging jumper into my project again.
User avatar
BrotSagtMist
Party member
Posts: 641
Joined: Fri Aug 06, 2021 10:30 pm

Re: Grid-only pathfinding only working on top left ?

Post by BrotSagtMist »

Hah, fun.

My point being, a lot of bugs can be introduced by not following a fixed style/syntax guide.
And please, being drunk and 30 hours over bedtime is the normal modus operandi for the average coder.
Making it even more important to not write stuff that is hard to get on the first glance, i for sure have trouble reading ~= nil after the fourth beer.
obey
LuaCre
Prole
Posts: 27
Joined: Mon Dec 03, 2018 11:21 am

Re: Grid-only pathfinding only working on top left ?

Post by LuaCre »

Haha, while Im more on the green than the suds I do appreciate it. The ~= nil for me is indicative of edge case error dodging and lets me know my logic in that area should either be tightened up or rewritten to avoid needing that catch entirely. And I know THIS is a terrible habit, but I always document my code roughly beforehand, write the code with notes on a whiteboard, and then when I have a working product I document properly.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests