First you need to find the triangle. You then calulate the point weight
into the triangle and use that to calculate the appropriate Y value.
Here is some example code.
/**
* Calculates the weight into the triangle from the specified point
*/
private void calculatePointWeight( Triangle t, Point2d xz) {
//define a plane using the three coordinates of the triangle
double x13 = t.p1.x - t.p3.x;
double z13 = t.p1.z - t.p3.z;
double x23 = t.p2.x - t.p3.x;
double z23 = t.p2.z - t.p3.z;
double dbottom = x23*z13 - z23*x13;
//if degenerate case, swap the point order around and try again
if( dbottom == 0 )
{
Vector3d temp = t.p2;
t.p2 = t.p3;
t.p3 = temp;
x13 = t.p1.x - t.p3.x;
z13 = t.p1.z - t.p3.z;
x23 = t.p2.x - t.p3.x;
z23 = t.p2.z - t.p3.z;
dbottom = x23*z13 - z23*x13;
if( dbottom == 0 )
{
temp = t.p1;
t.p1 = t.p2;
t.p2 = temp;
x13 = t.p1.x - t.p3.x;
z13 = t.p1.z - t.p3.z;
x23 = t.p2.x - t.p3.x;
z23 = t.p2.z - t.p3.z;
dbottom = x23*z13 - z23*x13;
if( dbottom == 0 )
{
throw new RuntimeException("Degenerate triangle: " + t.p1
+
t.p2 + t.p3);
}
}
}
double x43 = xz.x - t.p3.x;
double z43 = xz.y - t.p3.z;
double cbottom = -dbottom;
double c = (x43*z23 - z43*x23)/cbottom;
double d = (x43*z13 - z43*x13)/dbottom;
t.c = c;
t.d = d;
}
public float getY( float x, float z ) {
// handle the boundry cases
if ((x<lowX) || (z<lowZ)) return 0.0f;
if (x>highX) {
if (z>highZ) return getY(highX,highZ);
else return getY(highX,z);
}
if (z>highZ) {
if (x>highX) return getY(highX,highZ);
else return getY(x,highZ);
}
Triangle t = getTriangleForPoint(x,z,false);
// check for identity
if ( (t.p1.x==x) && (t.p1.z==z)) return (float)t.p1.y;
if ( (t.p2.x==x) && (t.p2.z==z)) return (float)t.p2.y;
if ( (t.p3.x==x) && (t.p3.z==z)) return (float)t.p3.y;
calculatePointWeight(t,new Point2d(x,z));
//determine y coord from given x,z coords
float newY = (float)(t.c*(t.p1.y - t.p3.y) + t.d*(t.p2.y - t.p3.y) +
t.p3.y);
if (Float.isInfinite(newY)) return 500f;
if (Float.isNaN(newY)) return 500f;
if (newY<-10) {
Log.log.println(LogType.EXHAUSTIVE," point("+x+","+z+") returning
"+newY);
newY = -10f;
}
return (float)newY;
}
> ----------
> From: Kris Korstjens[SMTP:[EMAIL PROTECTED]]
> Sent: Tuesday, May 29, 2001 7:29 AM
> To: [EMAIL PROTECTED]
> Subject: [JAVA3D] Terrain following
>
> Hi,
>
> I created a random terrain with a TriangleArray, now was i wondering if it
> is possible to get the heigth of the TriangleArray at a specified point...
>
> thanx
>
> Kris
>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".