- Home /
Making a grid.Help me understand.
I am following an A* tutorial but the part that I am mostly interested in is the way the grid is created .... http://unitygems.com/astar-1-journeys-start-single-step/ .... I understand what all the elements separatley do but I dont understand how the grid is actually created.
//Representation of a grid cell
public class GridCell
{
public bool walkable;
public float height;
}
//Width and height of the map in cells
int width, height;
//Active map of the world
public GridCell[,] cells;
//Size in world units of a cell
public float cellSize = 0.5f;
//Scan the bounds of the model and create a grid
bounds = renderer.bounds;
//Work out the top left corner
topLeftCorner = bounds.center - bounds.extents + new Vector3(0, bounds.size.y, 0);
//Calculate the dimensions of the grid map
width = Mathf.RoundToInt(bounds.size.x / cellSize);
height = Mathf.RoundToInt(bounds.size.z / cellSize);
//Create the grid map
cells = new GridCell[width, height];
//Scan for walkable terrain in each cell
for(var x = 0; x < width; x ++)
{
for(var y = 0; y < height; y++)
{
//Get the position for a ray
var currentPosition = topLeftCorner + new Vector3(x * cellSize, 0, y * cellSize);
RaycastHit hit;
//Create a cell for the grid
var cell = new GridCell();
cells[x, y] = cell;
//Cast the ray
if(Physics.Raycast(currentPosition, -Vector3.up, out hit, bounds.size.y))
{
//The height of the highest item in the cell
cell.height = hit.point.y;
//Test if the thing we hit was walkable
if(((1 << hit.collider.gameObject.layer) & walkableLayer) != 0)
{
//Flag the cell as walkable
cell.walkable = true;
}
}
}
}
This is the script that is used to create the grid. I understand what all the elements do separatley but I dont know how the grid is represented......I mean it has no Vector3 position....the cell isnt attached to a separate gameObject.....and as for the RayCast how do I know if and when the ray hits a cell adn the rays are cast on the cells.
I know the grid is made from a gameObject (just a cube) divided into cells but I dont understand where is a cel represented on the cube.....I see a 2d array cells[x,y] = cel and I know it is the grid but what is its position on the cube..........what is the link betwen the RayCast and each cel in the grid?
The for x,y loop is used for the currentPosition , the grid cells[x,y] = cel; and the RayCast........I see the link betwen the rayCast and currentPosition but what is the link betwen cells[] and the rays.
Answer by Owen-Reynolds · Jul 15, 2013 at 02:09 PM
This line says where each cell[x,y]
is centered:
currentPosition = topLeftCorner + new Vector3(x * cellSize, 0, y * cellSize)
The cells don't have a Transform (they aren't gameObjects,) so they are never placed at any particular spot using cell[x,y].position=
. But when the nested loop checks each particular cell[x,y]
it computes (topLeftCorner.x+x*cellSize, tlc.z+y*cellSize)
for that one cell, and ray casts down. Whatever it finds there, goes into cell[x,y].
Some later spot in other code runs the math backwards: "I'm at world x=450. That would be cell x-index = (450-topLeftCorner.x)/cellSize."
Lets see if I understand correctly.....if for example the for loop is at 1...x = 1 and y = 1 ....so .... topLeftCorner.x+1*cellSize, tlc.z + 1*cellSize ...the rayCast with those coordonates will belong to the cell in cells[1,1].
Cant I just add some Vector3 coordonates to the cell class that are based from a gameObject in the scene?...and cast rays from those V3 coordonates?
Yes, you could add coords to the class. Add public Vector3 pos
to the class. Then use the same nested x/y loop and set Cell[x,y].pos = new Vector3(topLeft.x+x*cellSize, 0, TL.z + y*cellSize);
(the exact same line currently computing currentPosition.)
The, in the nested rayCasting loop you have now, replace currentPosition with cell[x,y].pos;
. Would give the exact same numbers. The method above just saves space by computing position "just in time."
RE comment #1: note that loops and arrays start at 0. So "x=1, y=1" is one square in from the corner. So adding cellSize*1 to each is what you want.
I could just go with the current tutorial but I prefer to write down something I understand and use ins$$anonymous$$d of just blindly copy/paste everything..........what is the performance difference betwen the normal class and the one with Vector3 position?
Adding a V3 will add 3*4 bytes to GridCell. Say your grid is 100x100. That's 10,000 squares, which is 120,000 bytes for the V3's, or an extra 0.1 $$anonymous$$egs of memory. Not much, but moving it around and having it elbow out other stuff is a tiny slowdown (probably why that code doesn't store it.)
But if you look up Cell[x,y].pos
again later, that's a tiny speed-up (no need to recompute.) But the math is very fast, so not much speed up -- may not outweigh the size slowdown.
Close enough that the "goes with what's easiest to read" rule applies.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Free kick unity3d tutorial 1 Answer
RTS Grid Idea? 1 Answer
Easy simple way to create a networking between 2 computers using bluetooth 2 Answers
Saying the car is riding wrong way 0 Answers