Clickable 3d terrain

Started by
5 comments, last by Russinkungen 16 years ago
Hey everyone! I've been googling extensivly after this and can't seem to find anything about it which is kind of weird as it's pretty much basic stuff for just about any top-down rts with a heightmap-generated terrain. Anyway, the thing I want to do is get the world-space coordinated from where I click on the terrain. One thing I think I've figured out is that I translate the screencoordinates to worldcoordinates and shoot a ray straight forward from the cursor and see where it intersects the terrain. The trouble I'm having is to get where on the terrain the ray actually hits. On way to go around this would be to loop through the entire terrain and check which polygon, vertice or whatever that the ray intersects but that seems awfully inefficient to do every time I click. (at least when considering that my average clicks per minute is somewhere between 150 and 200 in a regular Warcraft 3 DOTA-game). So, any suggestion?
Advertisement
Check DirectX SDK sample "Pick"
I'm not an expert on this, but you don't really have to test all faces. You only need to test faces that are inside the view frustum. If you use quadtrees, this can be quite efficient.

One could also imagine a very different solution: Create a stencil buffer that contains the one pixel that your cursor is pointing to, then render the world-space-coordinates of your terrain into a texture with the stencil buffer enabled (this should be extremely fast). Then, simply read the color value at the cursor position and turn it into a vector. I haven't tried this, but I assume that this could work.
Wow, really fast answers!
Thanks alot for the suggestions. Really appreciate it. :)

I've gone quite insane over this for quite some time now. :)
We had similar issues...

I assume you know the height at any x,y location of your terrain.

We tried a successive approximation (sort of a binary search) which only required a few samples but it failed in the case where hills are in front of the camera as you might imagine.

Currently, we step down to the ground through many iterations from the camera point until we are below the terrain. If you find a better way lemme know haha.

-programmer_tom
Quote:Original post by Molle85
Check DirectX SDK sample "Pick"


;)
Quote:Original post by programmer_tom
We had similar issues...

I assume you know the height at any x,y location of your terrain.

We tried a successive approximation (sort of a binary search) which only required a few samples but it failed in the case where hills are in front of the camera as you might imagine.

Currently, we step down to the ground through many iterations from the camera point until we are below the terrain. If you find a better way lemme know haha.

-programmer_tom


That was exactly my problem aswell (Edit: or maybe not..). I could get the X & Z coordinates of the ray using the intersection between the ray and a plane. However if the height of the actual point I want to get is higher than the plane, then the ray continues more or less behind the actual coordinate I want to have.

Molle85: Haven't had the time to check out the sample yet. :)

This topic is closed to new replies.

Advertisement