n99b 122 Report post Posted May 10, 2007 http://img179.imageshack.us/img179/5224/theballoz9.png Basically, what I want to do is to find out the height of the ground beneath an object. I have a function that returns the intersection point of a line and a triangle (if any), which works. The problem is that I have to try it on different triangles from the ground object until I find an intersection. This doesn't seem very effective, so what I'm looking for is an (effective) algorithm that can determine which triangle the object is located above so only one intersection test has to be done. Thanks in advance. 0 Share this post Link to post Share on other sites
Dave Eberly 1173 Report post Posted May 10, 2007 Approach 1. Preprocess the data and create a rectangular array of grid cells, the entire array overlapping all your triangles. For each cell, generate a list of triangles that are partially or fully contained in the cell. At runtime, you can quickly determine which cell your object is over. Then iterate through the triangle list of that cell and do ray-triangle intersection tests.Approach 2. Preprocess the data so that each triangle stores pointers to its adjacent triangles. The first object-over-triangle test is the expensive one. Assuming your object is moving, the next object-over-triangle test does the ray-triangle intersection with the current triangle. If it is, you are quickly finished. If it is not, select the adjacent triangle that is "in the direction of motion" of the object from its last position to current position. Repeat searching adjacent triangles until you find the one the object is over. (This is sometimes called a "linear walk".) 0 Share this post Link to post Share on other sites
n99b 122 Report post Posted May 10, 2007 Thanks for the reply.I suppose I'll go with the first approach. The second is clever, but the first one will be easier for me to make.Thanks again. 0 Share this post Link to post Share on other sites
Calexus 245 Report post Posted May 10, 2007 From your image it looks like you want the height from a height field (?)float Terrain::GetHeight(float x, float z) const{ //Hitta höjden för hörnen i cellen const float ll= GetHeight((int)x, (int)z ); const float lr= GetHeight((int)x+1 , (int)z ); const float ul= GetHeight((int)x , (int)z+1); const float ur= GetHeight((int)x+1 , (int)z+1); //Beräkna positionen i cellen (kvadraten) //moa plocka fram decimaldelen const float dx= x-/*(float)*/((int)x); const float dz= z-/*(float)*/((int)z); //Dela i två trianglar /* |-----/| | 1 / | | / 2 | |/-----| */ if(dx + dz < 1.0) // Första triangeln { //const float slopeX = lr - ll; //const float slopeZ = ul - ll; const float slopeX = ul - ur; const float slopeZ = lr - ur; return ll + (slopeX*dx) + (slopeZ*dz); } else //Andra { //const float slopeX = lr - ur; //const float slopeZ = ul - ur; const float slopeX = ll - lr; const float slopeZ = ll - ul; return ur + (slopeX*(1.0f-dx)) + (slopeZ*(1.0f-dz)); } //Bilinjär interpolering (?) (direkt i cellen, måste vara plan) //return (ll * (1.0f - dz - dx + (dx * dz)) + // ul * (dx - (dx * dz)) + // ur * (dx * dz) + // lr * (dz - (dz * dx)));} [Edited by - Calexus on May 10, 2007 7:47:49 AM] 0 Share this post Link to post Share on other sites
oliii 2196 Report post Posted May 10, 2007 if it's not a heightmap, you'll need some sort of space partitioning. 0 Share this post Link to post Share on other sites