#### Archived

This topic is now archived and is closed to further replies.

# Clicking on terrain

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

## Recommended Posts

I know this has been asked before but since the search feature still is down I''m going to ask it once again. I have my terrain represented as a grid. I know the height on each point and I can calculate the height between points. What I want to do now is get the closest point to the mouse cursor when I click the mouse. In addition to the terrain data I know my own position, my view vector and the mouse 2d coordinates. If someone could give me some hints of what I should do next or point me in the direction of a tutorial that would be very helpful. Thanks

##### Share on other sites
I recommend building a quadtree to do that.

##### Share on other sites
You assume the mouse click is on the z-near plane. So first you have to convert your screen coordinates to the x and y value of the z-near plane. That is a simple transform and scaling. Then you have to cast a ray from the camera through the point on the z-near screen and find the first plane it intersects. I think that is easiest done using the depth buffer. The depth buffer will not give you the exact z, but rather just an approximate one. That is enough to give you an approximate x and y of the point. You then have to transform that to world coordinates. That gives you a starting place to check actual terrain triangles against the ray cast from the camera.

##### Share on other sites
Pardon my ignorance but what do you mean by z-near plane?

I have thought about what you said but I can't understand how to transform the mouse coordiantes. I could probably manage to trace the cameras view vector untill it intersects the terrain but that won't give me the position I want. Any tips about how I should transform the mouse coordiantes so that I get a more correct position to start my trace from? I take it that was sort of what you meant.

[edited by - ArchMiffo on December 3, 2002 2:11:36 PM]

##### Share on other sites
z-near is the near clipping plane. Anything on the same side of that plane as the camera is not drawn. It is like being off the left, right, top or bottom of the screen. z-far is the opposite limit of your view. Using z-near is mainly a way to avoid things like extreme magnification of textures and extreme distortion of objects due to perspective projection. Think of one finger over the lense when you snap a picture with a camera. The tip of one little tiny finger can block out the entire Golden Gate Bridge you are trying to take a picture of. z-near basically eliminates that since it would keep the finger from being rendered when used properly.

##### Share on other sites
Thanks, that cleared that up. So what you are suggesting is that I project the coordinates on that plane and then trace a line from the cameras position through the point on the plane. Was that anywhere close? If it was just one problem remains and that is how to transform the cordinates

##### Share on other sites
I just based my Terrain Engine off the object selection tutorial at GameTutorials.com

i applied a name (unsigned integer) to each square using glPushName / glPopName combos and let it return the value

1. 1
Rutin
27
2. 2
3. 3
4. 4
5. 5

• 11
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632948
• Total Posts
3009399
• ### Who's Online (See full list)

There are no registered users currently online

×