Jump to content
  • Advertisement
Sign in to follow this  
Russinkungen

Clickable 3d terrain

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Wow, really fast answers!
Thanks alot for the suggestions. Really appreciate it. :)

I've gone quite insane over this for quite some time now. :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!