Need help with pathfinding C#

Started by
8 comments, last by WozNZ 8 years, 6 months ago

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing. How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

Advertisement

Also preferably a list of the rectangles name in listing them like "myTile1" "myTile2" "myTile3" ect.

It is hard to determine what you mean in the original post

Hey, im making pathfinding for c# and understand the algorithm but the code is confusing. How would i be able to devide the amount of set space and make rectangles using graphics that are 50 , 50 pixels and fill the area if the area is a multiple of 50? Im not using XNA or Unity.

Do you mean you are doing a 2D game which is on a grid or that is 50*50 pixels in size? Or is this a 3D render and you want to overlay a grid on top that you will path find over?

If your game is 2D grid based they you should not worry about the render size of each tile at all, just the coordinates, as the render size of the tile is UI based and all the path finder should care about is the tile you are currently evaluating etc.

My Path finder code has no concept about the map/game world or whatever it is navigating over, instead it take the follow arguments, The bounding size of the map, so X,Y,W,H along with a Func<Vector, bool> which the path finder uses to decide if the vector location is a valid move. The caller supplies the function and this is what decouples the route finder from the map. If you can think about it this way it will help split what is A* code away from map and render based stuff which just don't matter :)

Also preferably a list of the rectangles name in listing them like "myTile1" "myTile2" "myTile3" ect.

If you have a grid why would you want to "name" of the tile instead of its coordinates? The only logical reason for the names would be you have the tiles in a map by name and you want to use that for lookup, this is unwise as the cost of string based lookups with a large grid could get costly for something like path finding.

Generally the navigation mesh (3D) or navigation grid (2D) is created along with the art.

Often it will encode additional information, such as the type of motion to use on the area (walk, swim, climb, crawl, slippery, etc) and sometimes different areas can be traversed at different times (can swim after getting flippers, can walk on quicksand after getting special boots, etc).

WozNZ basically covered the rest.

Guys, All i need is a algorithm to make 50, 50 width and height rectangles that fit into a 1000 , 1000 pixel form. i want to name the tiles so that in different locations it could set a boolean for the tile to be unable to pass or not. It is a 2d game. No xna or Unity. all i want is to make multiple rectangles like new Rectangle blah (blah blah) exept without me having to do it a bunch of times.

Surely all you need is two for() loops inside each other then, from 0 to your world size / 20?

Please be more descriptive. Im trying to make it so there is 44 tiles (i did the math) each 50 by 50 pixels. I want them all named from 1 to 44 using possibly a loop. Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.

Not sure where you're getting 44 tiles from.... a 1000X1000 pixel area would hold 400 50X50 pixel rects

If you're asking to name the variable itself myTile1, myTile2 etc, like:

var myTile1 = new Rectangle(...);

Then no this is not possible in any reasonable way and is really a horrible idea. Instead, you should have something like a 2d array of Rectangles accessed by their grid location:

var width = worldSizeX / tileSizeX;

var height = worldSizeY / tileSizeY;

var grid = new Rectangle[width, height]();

And then use something like this to build the array:

for (var j = 0; j < height; j++)

{

for (var i = 0; i < width; i++)

{

grid[i,j] = new Rectangle(i * tileSizeX, j * tileSizeY, tileSizeX, tileSizeY);

}

}

Im not sure how loops (or any commands beside manually naming it like (new Rectange recty;)) can be used to name things but if you can find that out it will be extremely helpful.


I'm not sure why you're jumping in to pathfinding already without having the fundamentals down already. You MUST understand the basic principles of programming, or else you'll ask extremely weird questions like this.

You can't make a loop to generate multiple *variables* such as "Rectangle myRect1; Rectangle myRect2;" without using code generation, and you should NEVER use code generation for this kind of problem. You can't do ANYTHING to make new variables in C# at runtime unless you're compiling code on the fly, which you shouldn't do. You can only set the values of existing variables and use collections such as arrays, lists, etc.

In this case you need to use an array. Here is an example of a 2D array of Rectangles in a C# WinForms app:


Rectangle[,] grid = new Rectangle[20,20];

for (int y=0; y<20; ++y)
{
    for (int x=0; x<20; ++x)
    {
        grid[x,y] = new Rectangle(x * 50, y * 50, 50, 50);
    }
}
And like jdean300 said, where did you get 44 tiles? That isn't even a square number. (1000 / 50) per axis is 20, squared is 400.

For the OP, you are trying to connect the appearance of the tile to deep into your code.

public class Tile

{

public string Name { get; set; }

// some other properties in you tile here

}

Now you can store your map as follows

var Map = new Tile[20, 20];

And loop through the grid adding the tiles using whatever map builder you want.

NOTE [20, 20] is based in 1000*1000 pixels divided by your 50*50 tiles

Notice how there is no notion of the size of the tiles above, that is all decoupled and is the responsibility of the render engine which could use Tile.Name to determine the graphic representation for the tile. This way the tiles could be 10*10 or 200*200 on screen or even ACSII text display, it does not matter.

You have to decouple in this way, think what happens if your game is played on a 4K monitor, your 1000*1000 pixel map is now tiny. The pixel size of tiles should never impact the game engine logic or you will find yourself in a world of pain later down the line. The reality is that if you have grid based movement in your game you should be able to write the engine to control the game that is self contained and has no concept about how input is received or how the game is displayed, they just do not matter to the game logic

You need to step back and think about what you are trying to do and possibly take some time out and learn some more about code, abstraction and separation of interests before you revisit your game code.

This topic is closed to new replies.

Advertisement