Terrian getHeight problems

You discovered a bug in the engine, and you are sure that it is not a problem of your code? Just post it in here. Please read the bug posting guidelines first.
Post Reply
cipher06
Posts: 2
Joined: Sun Mar 18, 2007 12:49 am

Terrian getHeight problems

Post by cipher06 »

I have been working on a terrain based game which used the terrain get hieght command over triangle selection and got odd results as to the terrain height at pointsi did a test on the terrain tutorial file by making cubes and setting them to the terrain->get height(x,y) height shown below

Image


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)

Image


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; 
	}
If i have been realy stupid or noone else has had this problem or even terrain get height shold produce those results im sorry for bothering you im relativly inexperienced
bane
Posts: 6
Joined: Sat Mar 24, 2007 12:31 pm

Post by bane »

i have problems with getHeight so ill try with this fix and let you know :)
bane
Posts: 6
Joined: Sat Mar 24, 2007 12:31 pm

Post by bane »

ok, i confirm cipher06 tracked a bug and fixed it just right (and made my day by it) :)

i thought it was limitation of terrain renderer not to be able to calculate height accuratelly.

good work, cipher06
cipher06
Posts: 2
Joined: Sun Mar 18, 2007 12:49 am

Post by cipher06 »

Thnks I am also working on changing the code so you can change the vertices hieght and it will only recalculte the LOD on those parts of the terrain.
hybrid
Admin
Posts: 14144
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

I commited the bugfix yesterday. If you make larger changes it would make sense to provide patches against the latest SVN to simplify the change tracking.
Post Reply