# How to do Cells on Terrain?

Hello, here is the project
Map test
I want to make a grid that allow the player to click in any square to travel there, The grid would also have the function to make some location impossible to walk (rivers, high mounts). Currently i have tried make a grid of triggers and put a box in every square to make the grid visible, but it doesn’t work as expected and the function the display the name of the grid box is veeery slow.
Does any of you have some suggestion to make it faster or any idea how i could develop it in an easier way?

If you can pick a terrain (there are analytical very fast functions to do so), then simply take X and Z and use them to convert to index.
It is just math, no entities even needed per cell.

mmmh i see… so i need a math grid to make sectors of the terrain, just have to select the x/y width/length and create the grid, then set a pass true false to make it walkable or not…mmmhh have to remove some dust on my math knowledges lol

Yep. Just math and array of bools

Seems easy the way you tell that lol wish it really could

Picking terrain is more complicated part of this. To simply get X,Y,Z by raycasting to terrain.
I’ve made before terrain raycasting, and there is very fast algorithms, explained around the web.

Then lets say you have 16x16 cells (`cellWidth`, `cellHeight`), that is 256 in total.
And `cellSize` is lets say 4 units (meters).
Then:

``````cellX = Math.floor(pickX / cellSize);
cellY = Math.floor(pickZ / cellSize);
cellInd = cellY * cellWidth + cellX;
console.log(cells[cellInd].type);
``````

I was trying to get the x/y coordinates (they appear as message in the bottom of the page) but is very slow, maybe coz the coord are taken while mousemove event that cause lots of requests, maybe is better to take them when mouse stop? I was thinking to take them when mouse click and to activate the ‘travel to’ function with the double click. What do you think is the best option?

Which picking mechanics you are using?
For picking on each frame, indeed, you need a good optimisations to make sure it is fast. That is why analytical terrain picking usually is the best, as it usually doesn’t need a lot of computation. As pc.Picker - uses extra rendering of whole scene to idBuffer, to do picking. And other options - rayscasting from physics is overkill as well, as it does picking against trimesh which will be slower than good analytical special for terrain algorithm.

But picking only on click - of course, this is most common case and is normal to be used that way.

I do use raycasting, and i was using it with onmousemove to show the name of the location just moving the mouse over them, but since is slow, i can pass to onmouseclick and doubleclick i will try different things and see what it works better.

Hi again, i was wondering if it could be possible to change the terrain.js that i use to generate the map and create there the grid that cut the map into pieces, since the code use a subdivision variable to generate the map and that value is halved to create a collisionmodel, i guessed that is also possible to use the same way to create a ‘sensitive’ grid. But i don’t have a clue how

EDIT: found something in Last Line of Defense project i will study that and try to implement into the heighmap