As some of the cubes were above and some below the terrain (the picture does not show this well they all look resnobly uniformally below) i figured there was an error in the code and i found (withthe add of the back of a envolope another calculation which seems to give the "proper" results same test see below)
the code change:
Code: Select all
f32 CTerrainSceneNode::getHeight( f32 x, f32 z )
{
f32 height = -999999.9f;
core::matrix4 rotMatrix;
rotMatrix.setRotationDegrees( TerrainData.Rotation );
core::vector3df pos( x, 0.0f, z );
rotMatrix.rotateVect( pos );
pos -= TerrainData.Position;
pos /= TerrainData.Scale;
s32 X(core::floor32( pos.X ));
s32 Z(core::floor32( pos.Z ));
if( X >= 0 && X < TerrainData.Size && Z >= 0 && Z <= TerrainData.Size )
{
video::S3DVertex2TCoords* Vertices = (video::S3DVertex2TCoords*)Mesh.getMeshBuffer( 0 )->getVertices();
core::vector3df a = Vertices[ X * TerrainData.Size + Z ].Pos;
core::vector3df b = Vertices[ (X + 1) * TerrainData.Size + Z ].Pos;
core::vector3df c = Vertices[ X * TerrainData.Size + ( Z + 1 ) ].Pos;
core::vector3df d = Vertices[ (X + 1) * TerrainData.Size + ( Z + 1 ) ].Pos;
f32 dx = pos.X-X;
f32 dz = pos.Z-Z;
if(dz<0.0001 || (dx/dz)>1) //remove geometry /0 error
{
//in alpha poly
height = a.Y+((d.Y-b.Y)*dz)+((b.Y-a.Y)*dx);
}else
{
//in beta poly
height = a.Y+((d.Y-c.Y)*dx)+((c.Y-a.Y)*dz);
}
height *= TerrainData.Scale.Y;
height += TerrainData.Position.Y;
}
return height;
}