- Home /
array of boxes
ok so here is my problem, i want to get from point A to point B. but the way i want to do it is with boxes. so say i have a 20x20 plane that is made up of boxes and the array goes from left to right and then down so box 1 would be 1x1 and to the right of that would be 1x2 and below 1x1 would be 2x1.
so say i start at 10x1 and want to get to 10x20(10x20 being point B) i want my algorithm to be smart and know what the boxes next to it are saying. so if we started at point A then our box would first look to the right of it(10x2) and if that spot was deemed walkable then it would then point the character that was on top of it to the box to its right. but if the box to the right(10x2) was not open then it would look down below it to 11x1 and see if that was open and then point that way. and it would go around in a circle till it found away that worked.
i am like brand new to unity coding, but have experience in c++ so i do know how to code, that being said unity is very unique and doesnt quite make sense completely yet. i have started some tutorials and am starting to get the hang of it, but i still dont have a clue how to make an array of boxes like i said. i would appreciate any help at all! but please teach it to me in a way you would teach like a 10 or 14 year old ha ha
PS sorry for it being so long i didnt know how to explain it other than the amount i toke
ps i have looked at the A* script so please dont refer me to that. my reason being that the a* requires it to be on each character, but i will have multiple characters on each box at a time so this would be a waste of ram.
Answer by DaveA · Jul 15, 2011 at 08:42 PM
You could make a two-dimensional array, the scripting reference and this forum has much to say about how to do that.
Why would it be a waste of ram? I may be misunderstanding what you are trying to do, but it seems you'd just be putting additional checks in your A* alg where it checks for availability. Each character could have it's own A* (maybe that's where the ram waste is?) but they could share a common 2d array and other structures, if that's a big concern. If it's really just 20x20 that's not too bad. If it were very big, I'd worry more.
it is a lot bigger my 20x20 thing was just an example and i am planning on having about 500 characters on the screen at one time. i dont want all of them having an A* algorithm on them when a lot of those characters will probable be in the same box, like 5 characters will all be trying to get to point B and be in box 1x4 i would want A* to calculate all 5 of them when i could have that single box tell all of them where to go. it saves a lot when you are dealing with higher numbers thank you for the very quick response though
and i wasn't planning on having an A* algorithm at all in my game
Well, you should. Your position is equivalent to "I want to add two integers, but I don't want to use the + operator". You can write your own Dijkstras shortest-path algorithm if you like - it's only about 20 lines of code.
Also calculate how much RA$$anonymous$$ you're talking about. 20$$anonymous$$B is hardly anything these days.
ok here is my problem completely fleshed out. i am trying to optimize this game for the iphone so any bit of optimization i can get helps seeing how i am already trying to render the entire thing in 3d. and i have had a lot of problems with A* when it tries to recalculate its path... so i want a predefined path for each character cause this will be a tower defense game so i want those calculations to be spot on and lightning fast. the A* doesnt give me that from what i have seen. and this solution was kinda thought up by me so i would like to make a game that is completely my thought not somebody else.
Answer by Waz · Jul 16, 2011 at 08:57 PM
Sounds like what you want is Dijkstra's algorithm:
only needs to be recalculated if obstacles are added that change the path
all travellers use the one data set
very little memory required
very fast to calculate
A* is just a variant that is faster for a single traveller and handles multiple destinations. It's hardly any different though, so I suspect any concern you have is about some particular implementation of the algorithm.
Your answer
Follow this Question
Related Questions
Follow up to AI Pathfinding Question 0 Answers
More Efficient way? 1 Answer
array out of range in simple pathinder javascript 1 Answer
JS: Display Array contents in a Box 0 Answers
Building an array help 2 Answers