Where did I click on heightmap? (pathfinding)

Started by
3 comments, last by rotalever 17 years, 1 month ago
Hi all! In order to calculate a path for my character to walk, I need to know which tile or triangle in the heightmap I clicked on! What do I do? Do I have to name each tile in the map? I guess I have to do that anyway, because I have to flag each tile -> walkable/unwalkable. Any sugestions?
Advertisement
gluProject can be used to project the co-ordinates of a point in world space into screen space. If you know where you clicked, you should be able to work out which object you clicked on by matching the co-ordinates.
Its the other way around!
I need the program to return to me what tile in the heightmap that was clicked on, so that the pathfinding algoritms can kick in, and the player can move to its destination!
The only way I can think of, is to name each tile and then use picking to make ogl be able to return to me, the tile that was clicked on! It just seems so overly complicated that I would have to divide a large world into tons of tiles and give them a name that can be returned. Still, as I mentioned in my first post, I might have to do that anyhow, since I have to be able to store which tiles are walkable, and which are not!

I just need some feed-back from someone whos done it, and knows what options I have!

EDIT:
Could I use the camera-view-vector and the point clicked on the screen together, to make a line, and see where it intersects the heightmap? Would it make it any easier if I made the heightmap plane (which is not really a heightmap, but...)?
Well then you're gonna want to use gluUnProject.

like this

gluUnProject(screen_x, screen_y, screen_z, mvmatrix, projmatrix, viewport, &world_x, &world_y, &world_z);

you may be asking where do I get screen_z from, well you can get that like this after you've drawn your heightmap.

glReadPixels(screen_x, screen_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &screen_z);

the rest of the stuff that you need for the call like the matrices can be obtained with glGet calls.

Hope this helps.
DG
The fastest way is to transform your mouse coords into a 3D-Line. With this line you have to do intersection tests to each triangle of the heightmap and choose the nearest one, which intersects. But wait... You wont want to test all triangles. Do something like "spatial subdivision" do speed up things greatly. Even on large terrains, I get the exact coords (not only which tile it is!) in 1-3ms on my old XP2200+ with this algorithm.
You should not use OpenGls picking function which is deprecated and slow (>100ms).

This topic is closed to new replies.

Advertisement