I have a quad with four vertices of variable height which is 1 unit square. 2 is 0,0 3 is 1,1
1--3
| |
| |
2--4
The first step I''m assuming is to determine which way the quad folds.
v1[0]=(int)x*1.0;
v1[1]=(int)y*1.0+1.0;
v2[0]=(int)x*1.0;
v2[1]=(int)y*1.0;
v3[0]=(int)x*1.0+1.0;
v3[1]=(int)y*1.0+1.0;
v4[0]=(int)x*1.0+1.0;
v4[1]=(int)y*1.0;
v1[2]=height_field[(int)v1[1]][(int)v1[0]];
v2[2]=height_field[(int)v2[1]][(int)v2[0]];
v3[2]=height_field[(int)v3[1]][(int)v3[0]];
v4[2]=height_field[(int)v4[1]][(int)v4[0]];
if((v1[2]+v4[2])/2.0f>(v2[2]+v3[2])/2.0f)
If the height in the center of vertices 1 and 4 are greater then the height in the center of vertices 2 and
three the the quad is folded along 1 and 4
-----
|\ |
| \ |
| \|
-----
Other wise the fold is along 2 and 3
-----
| /|
| / |
|/ |
-----
And that determines my two planes that I could be standing on. If the height in the center is equal, it doesn''t really matter since the quad is flat so going either way will work since it''s actually a single plane.
The next step is to figure out which plane I''m standing on.
In case of the first one
if(y-(int)y<1-x+(int)x)
Then I''m on the left plane otherwise I''m on the right plane.
In case of the second one
if(y-(int)y>x-(int)x)
I''m on the left plane, otherwise the right.
Once it''s determined which plane I''m standing on, the vertices that make up the plane are passed to a function which does the vector math where v2 is the center vertice. Vector v1v2 and v2v3 are used to determine the plane.
float getHeight(float v1[3], float v2[3], float v3[3], float x, float y)
{
float val;
val=-((v1[1]-v2[1])*(v3[2]-v2[2])-(v1[2]-v2[2])*(v3[1]-v2[1]))*(x-v2[0]);
val-=((v1[2]-v2[2])*(v3[1]-v2[1])-(v1[0]-v2[0])*(v3[2]-v2[2]))*(y-v2[1]);
val/=((v1[0]-v2[0])*(v3[1]-v2[1])-(v1[1]-v2[1])*(v3[0]-v2[0]));
val+=v2[2];
return val;
}
This works approximatly well however, although it gets the general solution (in that I can take partial steps around a quad and end up at the right height), moving around results in a jerky motion. I''m thinking it might be the transition between quads that''s confusing it.
I was wondering if anyone could spot any obvious errors in my math or suggest a simpler solution.
Ben
[ IcarusIndie.com | recycledrussianbrides.com | Got Linux? ]