# Terrain editing

This topic is 1426 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys & girls,

I am doing 3d terrain editor and I got question. I already made mouse ray casting, which works right. So the question itself:

How can I detect when line "touches" or intersects the terrain vertice? I have vertice array with positions, but, unfortunately, can't figure it out.

##### Share on other sites

You can calculate the distance to a vertex and then use a radius to choose if it's close enough or not.

Edit. If it's just an uniform grid, shouldn't it be quite easy to just grab the closest vertex on the grid depending on where you hit? Just divide the hit coordinates with the size of a tile on the grid and you should get the x and y for the vertex position. And with those you should get the actual vertex nicely from the array, vertices[y*w+x].

Edited by Sponji

##### Share on other sites

You can calculate the distance to a vertex and then use a radius to choose if it's close enough or not.

Yup, that's right! Just figured out one thing:

xPos and yPos are Mouse Ray coordinates. 1024 - Terrain width/height. ( It's equal of two, 1024x1024 ).

  xPos += 1024/2;
zPos += 1024/2;

if(xPos < 0)
xPos = 0;
else if(xPos >= 1024)
xPos = 1024-1;

if(zPos < 0)
zPos = 0;
else if(zPos >= 1024)
zPos = 1024-1;

vertexIndex = (int)(xPos) + (int)(zPos) * 1024;
m_buffer.m_pos[vertexIndex] = ncVec3(....


This works great and returns right vertex index for me, but this is strange way.

Edited by NekoCode

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632975
• Total Posts
3009668
• ### Who's Online (See full list)

There are no registered users currently online

×