Advertisement

HELP ME!! Silly maths problem....

Started by November 17, 2000 12:57 PM
4 comments, last by jollyjeffers 24 years, 2 months ago
hi, What I''m trying to work out seems really simple; but it is really pissing me off. Really. I''ve asked a maths teacher @ my school - she couldn''t work it out; I''ve asked two friends doing further advanced Maths and they cant do it... yet I''m sure it''s not that hard.... The problem: In Direct3D, you can specify the colours of the 3 vertices in a triangle and it blends/interpolates between them. Probably similiar in OpenGL.... Eg, you specify a triangle with a Red, Green and Blue corner and all the area in the triangle is a blend of the three depending on the distance from the point..... My problem; say I know the heights at the 3 vertices; what is the height at any given point in the defined triangle??????? Say I know that the corners 012 have heights 0,128,255 respectively. What if the player is 0.3 along, and 0.2 across (on a 0.0 to 1.0 scale) how high up is he/she?? I really just need to write a simple function that can calculate this - but it''s just not working... please help me, before I resort to destroying/killing things :| Many thanks. Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

What you need to do is interpolate across one axis (and one side) of your triangle, then interpolate from this, leaving at right angles from that side towards the hypotenuse, but stopping at the required point.

I think that''s how I did it, it was a while ago, I''ll try and dig out the source.
Advertisement
You have an arbitrary point, X, in the triangle. Interpolate along one side of the triangle to get a point, P1, that lies on the same horizontal line X. Do the same with the other edge. Then, using P1 and P2, find out what X''s height is by linear interpolation. Make sense?

Ut
This is the code I had - it did work, although it looks very confusing now - I really should have put more comments in

basically v.vx and v.vz are your positions into the triangle
h[0],h[1],h[3] are the heights of the 3 corners
TERRANTILESIZE is the length of the side of the triangle
TERRAINTILESIZEHTP is the length of the hypotenuse

This should give you a rough idea, I hope

  	if (v.vz<(TERRAINTILESIZE-v.vx))				// if top left triangle	{		h[0] = (mappt[tx+tya].height * scale)<<10;		h[1] = (mappt[tx1+tya].height * scale)<<10;		h[2] = (mappt[tx+tya1].height * scale)<<10;		dh = (h[1]-h[0])/TERRAINTILESIZE;		h1 = h[0] + dh * v.vx;				// height at point h1 (along v0-v1)		dh = (h[1]-h[2])/TERRAINTILESIZEHYP;//printf("TR %d",dh);		if (v.vx!=0)		{			hypa = (v.vx*v.vx*2);				// pythagoras to find distance across length v2-v1			sq = hypa/(v.vx*2);				// 1st guess at square root			if (sq!=0)				sq = (sq + hypa/sq)/2;			// get better approximation			if (sq!=0)				sq = (sq + hypa/sq)/2;			// better still			h2 = h[2] + dh * sq;				// height at point h2 (along v2-v1)		}		else			h2 = h[2];		vxdiv = TERRAINTILESIZE-v.vx;		if (vxdiv!=0)			dh = (h2-h1)/vxdiv;		else			dh = 0;		h2 = h1 + dh * v.vz;				// final height	}	else					// if bottom right triangle	{		h[0] = (mappt[tx1+tya].height * scale)<<10;		h[1] = (mappt[tx1+tya1].height * scale)<<10;		h[2] = (mappt[tx+tya1].height * scale)<<10;		dh = (h[1]-h[2])/TERRAINTILESIZE;		// v2-v1		h1 = h[2] + dh * v.vx;		dh = (h[0]-h[2])/TERRAINTILESIZEHYP;		// v2-v0//printf("BR %d",dh);		if (v.vx!=0)		{			hypa = (v.vx*v.vx*2);			sq = hypa/(v.vx*2);			if (sq!=0)				sq = (sq + hypa/sq)/2;			// get better approximation			if (sq!=0)				sq = (sq + hypa/sq)/2;			// better still			h2 = h[2] + dh * sq;		}		else			h2 = h[2];		vxdiv = v.vx;		if (vxdiv!=0)			dh = (h2-h1)/vxdiv;		else			dh = 0;		h2 = h1 + dh * (TERRAINTILESIZE-v.vz);	}  

Ooops, that dodgy code post was mine, sorry
Cheers;

Looks okay to me; I think I might have solved it using the first method...

I look interpolate across the top and bottom (between 4 corners) then interpolate down between the top and bottom.... where the 4th corner is the same colour as the 3rd:

1 - 2
/
3 - 4

and only ever calculate it if it''s within the triangle....

It looks good to me, when I''ve run a few tests through it...


thanks for the help.
Jack;

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

This topic is closed to new replies.

Advertisement