Advertisement

3D Terrain Height Question...

Started by May 11, 2002 02:15 PM
1 comment, last by RegularKid 22 years, 9 months ago
I''m having a problem calculating the height my character should be at given the terrain poly he is standing at. For example, my terrain is broken up into a bunch of triangles. Then I calculate which triangle my character is currently standing on. So basically I have 3 vertices for that terrain poly and the position of my character. This works fine if my terrain is flat, however, I''m stuck on how to calculate the character''s height if the terrain is slanted. Obviously his height would depend on what vertex he is closest to and the angle of terrain. But I can''t quite figure it out. Any help would be greatly appreciated. Thanks.
The easiest is to have the normal for the triangle. The equation for a plane is p.n=d where . is the dot product, p is a point in the plane, n is the normal and d is the distance from the origin in multiples of the magnitude of n. So if the normal is (a,b,c), p is (x,y,z) and v, a vertex of the triangle, is (vx,vy,vz) then a*x+b*y+c*z=a*vx+b*vy+c*vz. If y is the height then y=1/b*(a*vx+b*vy+c*vz-a*x-c*z)=a/b*(vx-x)+c/b*(vz-z)+vy.
Keys to success: Ability, ambition and opportunity.
Advertisement
I just struggled with this question for a week before I realized my cross product was wrong.. anyways I hope you know vectors!


  float landscape::height_at( float x, float z) const{	if( x <= 0 ) return 0;	if( z <= 0 ) return 0;	if( x >= width ) return 0;	if( z >= height ) return 0;	int ax = int( x );	int az = int( z );	vector a( ax, data( ax, az), az);	vector b( ax + 1, data( ax + 1, az + 1), az + 1);	vector c;	// quad:	//	// +=====b	// |    /|	// |   / |	// |  /  |	// | /   |	// |/    |	// a=====+	//	// depending on which triangle our point is on,	// we put c at one of the +	float localx = ax - x;		// where they are within the square	float localz = az - z;	if ( localx < localz )	// which triangle they are on.							// To me, this inequality should be reversed, but it works this way.	{		c.x = ax + 1;		c.y = data( ax + 1, az );		c.z = az;	}	else	{		c.x = ax;		c.y = data( ax, az + 1 );		c.z = az + 1;	}	// now we have our copmlete triangle	vector n = vector::normal( a, b, c);	//	if ( n.y == 0) return 0; This seems impossible, it would mean a flat wall.	return ( n.dot(a) - ( n.z * z + n.x * x ) ) / n.y; // this follows from 													   // seperating y from the plane equation.}inline float landscape::data( int x, int z) const{	return land_data[ z * width + x ];}  


I just posted this because the above suggestonig seemed a bit longer and kind of obtuse. that equation at the return is just teh plane equation worked around.

peace!

This topic is closed to new replies.

Advertisement