Advertisement

A line, a plane, and the intersection thereof.

Started by September 20, 2002 03:05 AM
0 comments, last by Juz 22 years, 5 months ago
Well, it''s a tad more complicated then that. This image details my problem: http://www.swgalaxies.net/jmud/problem.jpg I have my tile based heightmap engine, which renders the tiles as in the image above. I also have my player avatar - at this time, a box - who can run around the landscape. I want the dead center of my avatar (x, y, z in the image above) to always be right on the surface of the land. I''ve been wacking at this problem for days, and have determined that an extreme lack of sleep mixed with an extreme lack of schooling over the past few years have resulted in my posting this problem here. Help me Obi-wan Kenobi, you''re my only hope. So to summarize: The problem: I need to find the y value of my player character on the tile in the image. I have all of the information for the tile and the player *except* for that y value. I need an equation (or set of equations) to find the y value, using the aforementioned data. Thanks, - Juz www.swgalaxies.net/jmud/
Hi

to give you a hint how i did it in my engine:

Since you already know on what tile your character is and have the 3 vertices (v3/4/5 in your image) you can take those 3 verts to define a plane.
Next you construct a line that includes the position of your character ( since your x and z are known just take one point like (x,1000,z) and (x,1000,z) where your y is guaranteed to be somewhere between -1000 and 1000 )

Now you have a line and a plane, and you can get your y with a simple line/plane intersection function.


heres the code for a simple plane class and the intersection:


    class Plane{public:	Vector normal;      // A, B and C are normal.x, y and z	float D;	Plane () { D = 0.0f; };	Plane ( float _A, float _B, float _C, float _D )	{		normal.x = _A; normal.y = _B;		normal.z = _C; D = _D;	}	Plane ( Vector _v1, Vector _v2, Vector _v3 )	{		FromPoints ( _v1, _v2, _v3 );	}	void FromPoints ( Vector v1, Vector v2, Vector v3 );};void Plane::FromPoints ( Vector v1, Vector v2, Vector v3 ){	Vector d1, d2;	d1 = v2 - v1;	d2 = v3 - v1;	normal = CrossProduct ( d2, d1 );	normal.Normalize ();	D = -(normal.x*v1.x + normal.y*v1.y + normal.z*v1.z);}/*formula for intersection of line and plane:        Ax + By + Cz + Dt = -  ------------------          Ai + Bj + CkIntersectionPoint = LineOrigin + t * LineDirection*/Vector LinePlaneIntersection ( Vector vOrigin, Vector vDirection, Plane& P ){	float a = P.normal.x; float b = P.normal.y; 	float c = P.normal.z; float d = P.D;	float x1 = vOrigin.x; float y1 = vOrigin.y;	float z1 = vOrigin.z;	float i = vDirection.x; float j = vDirection.y;	float k = vDirection.z;	float t =  - (a*x1 + b*y1 + c*z1 + d)/(a*i + b*j + c*k);	float ix = (vOrigin.x + ( t * vDirection.x ));	float iy = (vOrigin.y + ( t * vDirection.y ));	float iz = (vOrigin.z + ( t * vDirection.z ));	return Vector ( ix, iy, iz );  }  



EDIT: i just see that this might be a bit misleading with the (x,-1000,y) etc. above, since in the formula i use a direction vector, so just take one point that is definitely above your terrain like (x, 1000, z ) and a unit length vector that points directly down (x,-1.0,z) and pass those two together with the plane to the lineplaneintersection function.




Runicsoft -- home of my open source Function Parser and more



[edited by - Burning_Ice on September 20, 2002 6:44:46 AM]

This topic is closed to new replies.

Advertisement