A* Pathfinding Needs help when walls come to play

So I made an A* Pathfinding algorithm and it works pretty well without walls, when walls come it gets stuck there, i will show some pictures

Script:

local start = workspace.Start
local goal = workspace.Goal

local openlist, checked, waypoints = {}, {}, {}
table.insert(openlist, start)

local validDirections = {
	Vector3.new(5,0,0), Vector3.new(-5,0,0),
	Vector3.new(0,0,5), Vector3.new(0,0, -5),
	Vector3.new(5,0,5), Vector3.new(-5,0,-5),
	Vector3.new(-5,0,5), Vector3.new(5,0,-5)
}

local currentNode, Position = start, 1
local goal_Reached = false

repeat
	
	table.remove(openlist, Position)
	
	local bestfcost,bestIndex = 9999,1
	
	for i,v in pairs(validDirections) do
		local checkWhetherwallisinfront = workspace:Raycast(currentNode.Position, v)
		if checkWhetherwallisinfront and checkWhetherwallisinfront.Instance and checkWhetherwallisinfront.Instance.Parent ~= workspace.Nodes then
			print(checkWhetherwallisinfront.Instance)
			continue
		end
		local magnitude = (goal.Position - (currentNode.Position + v)).Magnitude
		local part = Instance.new("Part")
		part.Parent = workspace.Nodes
		part.Position = currentNode.Position + v
		part.Anchored = true

		local intValue = Instance.new("IntValue")
		intValue.Parent = part
		intValue.Name = "F"
		if v == Vector3.new(5,0,5) or v == Vector3.new(-5,0,-5) or v == Vector3.new(-5,0,5) or v == Vector3.new(5,0,-5) then
			part.BrickColor = BrickColor.new("Red flip/flop")
			intValue.Value = magnitude + 14
		else
			intValue.Value = magnitude + 10
		end

		table.insert(openlist, part)
	end

	for i,v in pairs(openlist) do
		if(v.F.Value < bestfcost) then
			bestfcost = v.F.Value
			bestIndex = i
		elseif v.F.Value == bestfcost then
			local magnitude = (v.Position - goal.Position).Magnitude
			local magnitude2 = (openlist[bestIndex].Position - goal.Position).Magnitude
			
			if magnitude < magnitude2 then
				bestIndex = i
				bestfcost = v.F.Value
			end
		end
	end
	currentNode = openlist[bestIndex]
	currentNode.BrickColor = BrickColor.new("Sea green")
	currentNode.Size = Vector3.new(1, 10, 1)
	print((currentNode.Position - goal.Position).Magnitude)
	table.insert(waypoints, currentNode)
	task.wait()
until (currentNode.Position - goal.Position).Magnitude < 3

print("yes")

I think something with the raycasting is wrong, any help?

4 Likes

Anyone please help? I really need this

1 Like

It seems it is stuck in an infinite loop (adding previously visited nodes to the open list).

A very simple (cheaty) fix would be removing this check:

Also, you forgot to utilize the checked list.

it works, but the waypoints are clipping at the wall. If i were to implement this to an NPC, it might get stuck.So you got any solutions?


I dont know if its the algorithm but it gets stuck on easy problems like this


also uhhh

1 Like

I see that you have check for obstacles but you don’t even you use it

local checkWhetherwallisinfront = workspace:Raycast(currentNode.Position, v)
		if checkWhetherwallisinfront and checkWhetherwallisinfront.Instance and checkWhetherwallisinfront.Instance.Parent ~= workspace.Nodes then
			print(checkWhetherwallisinfront.Instance)
			continue
		end

To be honest I am a bit lazy to check it all so you can just use mine version of it

Pathfinding3.rbxl (56.4 KB)

1 Like


here is the video me using it

Never seen overlap getpartinbounds used before

Also there is one thing that may seem strange to you

g = (origin - point).magnitude + closedarr[origin].h

I did this because g = (origin - point).magnitude + closedarr[origin].g
was the worst path possible
Actually I am not even sure why it even works but it does work

since its h is the end point prob why

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.