Skip to main content
gamedev.net
🔒 Locked

Interpolation

Started by playworker 15 replies 800+ views
Original Post
playworker
playworker
Hi guys, I know which triangle of the terrain my "tank" is sat on, I just need to interpolate between the heights of the vertices of that triangle to find the exact height of the tank... Does anybody know of an efficient way of doing this? Matt
blizzard999
blizzard999
Suppose your triangle is described by 3 vectors (or point 3D)

P0, P1 and P2

You know that 3 points describe a plane.

consider the classic plane equation = N * P + d = 0

Where N is the normal of the plane and d a scalar...every point P that resolves this equation is 'a point of the plane'. That is in other words

Nx*Px + Ny*Py + Nz*Pz + d = 0

In general the distance from a point to a plane is given by

distance (P, plane) = (N*P + d)/len(N)

where len(N) = sqrt(Nx^2 + Ny^2 + Nz^2)

The sign depends if the point is in front of the plane or not...
Note that if the point is on the plane -> distance = 0

To compute our plane given P0, P1, P2

let vector v0 = P1 - P0
let vector v1 = P2 - P0
-> N = v0 x v1 (cross product)

we know that (foe example) P0 is by definition on the plane

so N*P0 + d = 0 -> d = - N*P0 (or d=-N*P1 or d=-N*P2)

If you dont remember the 'cross' product it is given by

w = u x v = (wx, wy, wz)

wx = uy*vz - uz*vy
wy = uz*vx - ux*vz
wz = ux*vy - uy*vx

Of course you should orient your tank accordingly with the plane!
So it should have an up vector equal to N and a look direction equal to
look = look2D - look2D * N
look = llok/(Len(look) // to re-normalize!


Where look2D is the 2D direction in which your tank is looking (ie look2D = (lx, ly, 0 ) if your hmap is in the plane xy )
But this may be too much !!!

See also the topic

http://www.gamedev.net/community/forums/topic.asp?topic_id=153283

in which I've posted a method to orient an object given look,up and pos

Good work!





[edited by - blizzard999 on April 27, 2003 10:19:18 AM]
playworker
playworker
Thanks Blizzard, but I''m still not sure how to determine the height of my tank...

I''ve read your post a couple of times and I understand what you''re saying, but it seems to me that you''re just telling me how to calculate the face normal of the triangle (which I''ve already done for lighting purposes).

Sorry if I missed something!

Matt
playworker
playworker
In retrospect, I think I may not have worded the question very clearly.

I have a "tank" object and a landscape, I know exactly which triangle of the landscape the "tank" is above, now I just need to work out the exact z coordinate of the triangle at the point where my "tank" is. This is so that I can make the z coordinate of the "tank" equal to the height of the landscape, et voila, the tank looks like it''s sitting on the ground!

This might not be the best way to do this - I''m just guessing!

Any guidance would be much appreciated,

Matt
CraZeE
CraZeE
technically yes, you need to calculate the face normal. that is needed if u want to orient ur tank according to terrain and not just to position it.

about getting ur tanks position, u need to know the plane formula for the triangle the tank is on. Yes, the tanks on a triangle, but the orientation and area of the triangle can be obtained thru its plane formula. blizzard was mainly trying to inform u on the plane mathematics. after u have the plane formula, u shud know ur tanks (x,y) coordinates (assuming the Z-axis points to the sky). if u followed the steps given and substitute ur (x,y) values, u'll get ur tanks height. whoila, now u hav a tank at the right place with the right tilt

[edited by - crazee on April 27, 2003 12:18:31 PM]
To learn, we share... Give some to take some -
playworker
playworker
Substitute my tanks x and y values in to where?

I''m not stupid, I''ve read Blizzard''s post several times and I cannot see where I''m supposed to put my x and y values to get a z value...
blizzard999
blizzard999
Ok! I''m here!

I suppose you know (x,y) coordinates of your tank and you have an height map in the form (x, y, h(x,y) ).
Your question: I know (x,y) and I want h too!

Your plane : N * P + d = 0

That is Nx*x + Ny*y + Nz*z + d = 0

If you need the exact z (I suppose it''s your height) simply
resolve by z...

z = - ( Nx*x + Ny*y + d ) / Nz

and you have your height!




blizzard999
blizzard999
I have given the formula of the distance in the first reply but you do not need it because the height is not the distance from the floor!



playworker
playworker
Many thanks Blizzard - sorry for doubting the accuracy of your post

I''ve just got one small problem...

If I put the world x and y coordinates into the plane formula then I get crazy values for z, if instead I put in the local x and y relative to the triangle then I get a local z, the only problem is, I don''t know what to add to it to get a world z again... It seems to work if I add the z value of one of the verices to it, but which vertex seems to vary between each triangle of the landscape! Aarrrgh!
blizzard999
blizzard999
I suppose you are using an height map in the form of a BYTE map or something like it...in the formula you should use the scaled
values that is ( sx*i, sy*j , sz * hmap(i,j)) the same values you use in rendering and/or fustum culling...

The only case in which the formula is wrong is when Nz = 0 but this happens when N is in the form (Nx,Ny,0) that is a vertical triangle (of course impossible in an height map!).

I dont see why you get starnge values for z...you should obtain an interpolation of the three z values.



Mihail121
Mihail121
Hey Blizzard!How lang it took at to learn all that math?I''m not very good with it,but i''m trying

The PAIN is coming...this summer!!!In cinemas everywhere.
blizzard999
blizzard999
quote:
Original post by Mihail121
Hey Blizzard!How lang it took at to learn all that math?I''m not very good with it,but i''m trying



I dont know...they are fundament of analitic geometry (if it is the right english term...).
The idea to compute the matrix camera has come from an old course in linear algebra (that is total different from geometry or 3D CG!).
In reality I think that what you need from school is not so much...in internet you found everything.
I think that to use advanced API like OpenGL or Direct3D one should understand how to implement a software renderer.
Of course you dont need to do it (although I''ve written a 3D renderer with tex map, light,...both in C++ and java) but it is very useful to understand the basics (matrix, vector, clipping, ...).

I learned a lot here

http://pages.infinit.net/jstlouis/3dbhole/
Mihail121
Mihail121
Haven''t thinked of it but you''re right.That''s for the cool link

The PAIN is coming...this summer!!!In cinemas everywhere.
UltimaX
UltimaX
Man that link is sweet, thanks for sharing it!


-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"
Ilici
Ilici
blizzard, its called computational geometry

and a little touch from me: when u plug in the x and z coord of the point in the plane eq to find the y, dont normalize the plane normal or u''ll get smth ugly.
u can normalize it afterwards



playworker
playworker
Okay, got it working! Thanks Blizzard and anybody else who posted.

Here is the code for finding the z value when you know x and y:

float Landscape::getHeight(float x, float y)
{
float height;

x+= LENGTH/2;
y+= LENGTH/2;

// Use the equation of a plane to determine the height for an arbitrary point on a triangle

int c = (int) floorf(x);
int r = (int) floorf(y);

float dx = x - c;
float dy = y - r;

float z;

int index = c + (r * TLENGTH);

if (dx + dy > 1)
{
index+= TSIZE;
z = vertex[triangle[index][0]][2];

dx = 1-dx;
dy = 1-dy;

VECTOR3 normal = tnormal[index];

height = (normal[0]*dx + normal[1]*dy) / normal[2];

height = height;
}
else
{
z = vertex[triangle[index][2]][2];

VECTOR3 normal = tnormal[index];

height = -(normal[0]*dx + normal[1]*dy) / normal[2];
}

triang = index; // For debugging - highlights the triangle currently in use

height+= z;

return height;
}

If I''ve done something stupid in there let me know gently!

Matt
blizzard999
blizzard999
You should use logic and physic coordinate separately...
I'm sure you are using coefficients sx,sy and sz to scale your hmap and render it...

For example you can have a BYTE map[256][256] in which your logic coordinates are the indices to access the buffer and your height is an integer that goes from 0 to 255.
You have to scale it to physic coords for example sx=10, sy=10 and sz=0,25.
Your tank 'lives' in physic coordinate so it can move from (0,0) to (256*sx, 256*sy) = (2560 m, 2560 m) or light years if you prefer...

Suppose your tanks has physic2D coordinate

fTank_x, fTank_y

You need fTank_z that is the height at which your tank 'flies' over the map.

You have to translate the physic coords in logic (the indices of your hmap) by simply divide them by sx and sy (your scaling value)

Let int iTank_i = fTank_x/sx;
and int iTank_j = fTank_y/sy;

Now you must decide in which triangle your tank is...
I suppose you divide your quad as

(i,j)-(i+1,j+1)-(i, j+1) 'upper'
and (i,j)-(i+1,j )-(i+1,j+1) 'lower'

(in other term I use a diagonal from 0,0 to 1,1 )

Let float di = fTank_x/sx - (float)iTank_i;
and float dj = fTank_y/sy - (float)iTank_j;

As you can see dx and dy vary from 0 to 1.

if(dj > di)
{
// upper : retranslate in physic coordinates with
// the upper triangle indices!
P0 = vector(i*sx, j*sx, hmap[j]*sz);
P1 = vector((i+1)*sx, (j+1)*sy, hmap[i+1][j+1]*sz);
P2 = vector(...)
}
else
{
// lower
P0=...
P1=...
P2=...
}

// compute plane with (P0,P1,P2)

// compute fTank_z with plane and fTank_x, fTank_y

fTank_z = -(Nx*fTank_x + Ny*fTank_y + d)/Nz;





[edited by - blizzard999 on April 29, 2003 4:07:36 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.