# Above which triangle is the object located?

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

## Recommended Posts

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.

##### Share on other sites
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".)

##### Share on other sites

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.

##### Share on other sites
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]

##### Share on other sites
if it's not a heightmap, you'll need some sort of space partitioning.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002176
×