✅ A* Algorithm Get Node where x and y is 1 less (Neigbour)
Any idea how to get the neighbours? ive tried to grab the neighbour pathnode item by subtracting 1 from x. Either im not doing it correctly or the list needs to be formatted better but all the ways i have tried are not seeming to work so help?.
72 Replies
hi, what does panelNodes argument hold and, whatever it is, is it ordered?
not knowing pretty much anything, would "Dictionary" or anything "Hash.."ed be a better (easier) choice?
panelNodes holds all the Nodes in the A* Grid. It’s a 1201x601 size Grid with each coordinate being 1 singular instance of the “PathNode” class
Given the amount of classes due to the grid size…it’s definitely gonna be a class I don’t call until needed. #performancematters 😂
so
without changing the data and without knowing the order of the nodes
a very crude way would be
var mx = this.x - 1;
var my = this.y - 1;
var x_neighbour = panelNodes.FirstOrDefault(n => n.x == mx);
var y_neighbour = panelNodes.FirstOrDefault(n => n.y == my);
but it will hurst perf
hurt*
Do you want me to show you the code creating the nodes?
simplest ways, either change the List<> or change the PathNode class adding pointers to neighours, basically implementing your own "true" list
yes, paste it
Okay 2 secs
but the code above will find what you need, or null
it doesnt care about duplicates
nor anything else basically
😛
only use for "Canvas panelImport" is just to get the width and height for the grid
also i++ was used for testing i just forgot to remove it 🤣
yeah that private field is not needed, but it's a very minor point 😛
yea i was using it to edit stuff earlier.
all things considered. this should get the .barrierNode return value from that list item right?
i combined it because i need to get a singular coord
wait a sec 🙂
yes?
just gimme a sec xD
haha your good dw xD
& is a bit operator, && is a boolean operato, but what you want there is a ||
iun that lambda
facepalm rookie mistake. i didnt even click that i did that lol
something like that if you want to run once like above - panelNodes.Where(n=> n.x | x.y).ToList();
yyes those are my nemesis as well, misslicks, ctrl+c, ctrl+v.. even ctrl+s can ef you over 😛
but then Where can return null, one or two items.. where two is the expected one 🙂
but im thinking about the second solution i mentioned
?
where PathNode holsd references to its neighbours*
Oh yea so I have that in there already…it’s just commented out.
“neighbours”
But before you get all “why didn’t you tell me” 😂 I hadn’t created a search into that yet.
show me then 😛
It’s literally just “private list neighbours”
Obviously coded correctly
thats inside the "PathNode" class
how do you create such a list?
reason why i havent implemented it is because i dont want to get all the grid members. just the ones surrounding it for better performance
or you dont? yet 😛
yep havent yet
well it's just a reference, not a value object 😛
no point in me piling the whole grid into 7 billion classes when i just want the 8 nodes that are surrounding the node
you wouldn't, but yes memory would rise, but..
oh i know. dont worry i know i havent made it into a new list. i commented it out for that reason because i wasnt sure what i was gonna do with it
x and y is a tuple.. basically and it is unique, from what i rember about A*
right>
?
my x and y is in double[,] format but i can switch back to tuple cuz i did have it there earlier. yes the x and y are unique to each cord in the grid
sorry x and y are both double type variables. they are not combined in an array by default
well you can always create your own "tuple" class, but now you dont have to
well what dotnet version do yo use? 😉
6.0
i believe it is
because
(double x, double y) node = (0.1, 0.2);
7.0 actually
which is a new tuple
supoprts == and !=
its a value type though
not quite following you
so i dont know about a performance with dictionaries, but dictionaries 🙂
insdtead of a list
what...so switch out my x and y type from double to dictionary?
oh instrad of list
use Dictionary<"tuple", PathNode> ddict
than
you can find any node in a O(1) time 🙂
in an easy way
you talking about switching this out?
yes
exactly
to a dictionarty with a key including both x and y 🙂
you can use tyuple
or new tuples
or wrote your won tupe class
and overridfe hascode method
either im stupid and not reading this properly or its just my never used dictionary class before....."tuple" is red underlined
where: "tuple" is class for axample
publis class MyTuple { public int x; public int y; public MyTuple(int argX, int argY) { x = argX; y = argY;}}
2 secs
and use it both as a composite field in the PathNode class
i got you now.
do not forget to overridre hashcode method in that tuple class
use this one 🙂
HashCode.Combine
you can store it as a private field (the hashCode) but do not persist it, cause of problems 😛
Add method
you ned to provide a key and a value
check its header 🙂
i need to do some reading on dictionaries and tuples...god damn. sorry can you show me the method lol
i know its public override
wait a sec
public class MyTuple
{
private int _hashCode;
public MyTuple(...)
{
// your code
_hashCode = HashCode.Combine<int, int>(x, y);
}
//somewhere down there
public override GetHashCode()
{
return _hashCode;
}
}
for (int y = 0; y < gridArray.GetLength(1); y++)
{
panelNodes.Add(new MyTuple(x, y), new PathNode(width, height, x, y));
}
this part is in a Grid class
😛
then in :
public void updateNeighbours(List<PathNode> panelNodes)
{
var mx = x - 1;
var my = y - 1;
MyTuple predicate = new(x-1, y-1);
if (panelNodes.CintainsKey(predicate))
{
// access it by panelNodes[predicate]
}
}
do not store _hashCode anywhere outside of the memory 😛
public override INT GetHashCode()
*
😛
doesnt work nor when i change to
doesnt comple
the not working part.. some details? 😛
double needed to be casted to int
lol
aaa my bnad
change it to double of course
wherever i use int
got it tho
mybad 😛
haha alg casting is annoying and easy to forget
no dont cast it
it a waste of tie and memory 😛
in this case
just use double
as types
bouth in mytupel
and in dictionery's key
where dicts key is ourt calss so you dont have to 😛
i should have done it
oh right i can just change the tuple x type. im an egg
yes you have the power here 😛
you are the captain now 😄
HashCode.Combine<double, double> as well
yep changed that
casting int to double looses data, so it would not work anyway
Much better 🙂
couple bits of garbage now to clean up but now i have the neighbour
and more importantly can access their return method functions 🙂
its actually quite fast as well
no boxing issues i think 😛
legendary man. i added you......in case i have more errors....... but thank you for that.
should do the job
no prob
ill let you know how it goes. Thanks bro 🙂
"return walkable = false;"
will assign false to walkable and then return false
always 😛