Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nk.Andrei · Jul 15, 2013 at 01:39 PM · classtutorialgrid

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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."

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nk.Andrei · Jul 15, 2013 at 07:58 PM 0
Share

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].

avatar image Nk.Andrei · Jul 15, 2013 at 08:01 PM 0
Share

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?

avatar image Owen-Reynolds · Jul 15, 2013 at 10:22 PM 0
Share

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.

avatar image Nk.Andrei · Jul 16, 2013 at 09:22 AM 0
Share

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?

avatar image Owen-Reynolds · Jul 16, 2013 at 03:20 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges