Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 161)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.cpp	(revision 164)
@@ -120,5 +120,6 @@
 mCurrentObjectType(0),
 mApplication(app),
-mUseAnimation(false)
+mUseAnimation(false),
+mDeleteObjects(false)
 {
 	//mInputDevice = PlatformManager::getSingleton().createInputReader();
@@ -206,4 +207,5 @@
 		 
 		 Real val = Math::RangeRandom(0, 360); // random rotation
+
 		 // get results, create a node/entity on the position
 		 mCurrentObject = mTerrainContentGenerator->GenerateSceneObject(
@@ -301,4 +303,11 @@
 	}
 	
+	if (mDeleteObjects)
+	{
+		mApplication->deleteEntityStates();
+		mTerrainContentGenerator->RemoveGeneratedObjects();
+		mDeleteObjects = false;
+	}
+
 	if (mUseAnimation) // update animations
 	{
@@ -1034,8 +1043,8 @@
 		break;
 	case KC_F9:
-		toggleUseAnimation();
+		mUseAnimation = !mUseAnimation;
 		break;
 	case KC_F11:
-		takeScreenShot();
+		takeScreenshot();
 		break;
 	case KC_F12:
@@ -1053,6 +1062,5 @@
 		break;
 	case KC_DELETE:
-		mApplication->deleteEntityStates();
-		mTerrainContentGenerator->RemoveGeneratedObjects();
+		mDeleteObjects = true;
 		break;
 	//KEY_PRESSED(KC_F3, 0.3, writeFrames());
@@ -1223,5 +1231,5 @@
 }
 //-----------------------------------------------------------------------
-void TerrainFrameListener::takeScreenShot()
+void TerrainFrameListener::takeScreenshot()
 {
 	char tmp[20];
@@ -1415,7 +1423,2 @@
 	queryPanel->setHeight(top + border_height);
 }
-//-----------------------------------------------------------------------
-void TerrainFrameListener::toggleUseAnimation()
-{
-	mUseAnimation = !mUseAnimation;
-}
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 161)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.cpp	(revision 164)
@@ -24,7 +24,15 @@
 };
 
+const float walk_duration = 10.0f;
+const float wait_duration = 2.0f;
+const float rotate_factor = 0.7f;
+const float move_factor = 0.03;
+
 /***************************************/
 /*      EntityState implementation     */
 /***************************************/
+Vector3 EntityState::msMinPos = Vector3::ZERO;
+Vector3 EntityState::msMaxPos = Vector3::ZERO;
+
 EntityState::EntityState(Entity *ent, State entityState, Real speed):
 mEntity(ent), mState(entityState), mAnimationSpeed(speed)
@@ -53,4 +61,10 @@
 }
 //-----------------------------------------------------------------------
+EntityState::~EntityState()
+{
+	mAnimationState = NULL;
+	mEntity = NULL;
+}
+//-----------------------------------------------------------------------
 Entity *EntityState::GetEntity()
 {
@@ -66,8 +80,14 @@
 {
 	mTimeElapsed -= timeSinceLastFrame * mAnimationSpeed;
+
+	if (!mEntity || !mAnimationState)
+		return;
 	
 	if (mState == MOVING) // toggle between moving (longer) and waiting (short)
 	{
 		SceneNode *parent = mEntity->getParentSceneNode();
+		
+		if (!parent)
+			return;
 
 		if (mTimeElapsed <= 0) // toggle animation state
@@ -76,8 +96,9 @@
 			{
 				SetAnimationState("Walk", true);
-				mTimeElapsed = 10; // walk for mTimeElapsed units
-
-				// choose random direction between zero and two pi
-				Radian rnd = Radian(Math::UnitRandom() * Math::HALF_PI);
+				
+				mTimeElapsed = walk_duration; // walk for mTimeElapsed units
+
+				// choose random rotation
+				Radian rnd = Radian(Math::UnitRandom() * Math::PI * rotate_factor);
 
 				//mEntity->getParentSceneNode()->rotate();
@@ -87,33 +108,34 @@
 			{
 				SetAnimationState("Idle", true);
-				mTimeElapsed = 2; // wait for mTimeElapsed seconds
+				mTimeElapsed = wait_duration; // wait for mTimeElapsed seconds
 			}
 		}
 
-		if (mAnimationState->getAnimationName() == "Walk")
+		if (mAnimationState->getAnimationName() == "Walk") // move forward
 		{
+			// store old position, just in case we get out of bounds
 			Vector3 oldPos = parent->getPosition();
-
-			parent->translate(parent->getLocalAxes(), Vector3(0.03 * mAnimationSpeed, 0, 0));
-
-			// HACK: if out of bounds => go back to idle
+			parent->translate(parent->getLocalAxes(), Vector3(move_factor * mAnimationSpeed, 0, 0));
+
+			// HACK: if out of bounds => reset to old position and set animationstate to idle
 			if (OutOfBounds(parent)) 
 			{
 				parent->setPosition(oldPos);
-				SetAnimationState("Idle);
+				SetAnimationState("Idle", true);
 				
-				mTimeElapsed = 2;
+				mTimeElapsed = wait_duration;
 			}
 		}
 	}
 		
-	if (mAnimationState) // add time to drive animation
-	{
-		mAnimationState->addTime(timeSinceLastFrame * mAnimationSpeed);
-	}
+	// add time to drive animation
+	mAnimationState->addTime(timeSinceLastFrame * mAnimationSpeed);
 }
 //-----------------------------------------------------------------------
 void EntityState::SetAnimationState(String stateStr, bool loop)
 {
+	if (!mEntity)
+		return;
+
 	mAnimationState = mEntity->getAnimationState(stateStr);
 	mAnimationState->setLoop(loop);
@@ -126,8 +148,11 @@
 
 	if ((pos > msMinPos) && (pos < msMaxPos))
-		return true;
-
-	return false;
-}
+		return false;
+
+	return true;
+}
+
+
+
 /*********************************************************/
 /*      TestCullingTerrainApplication implementation     */
@@ -136,5 +161,5 @@
 
 TestCullingTerrainApplication::TestCullingTerrainApplication():
-mTerrainContentGenerator(NULL)
+mTerrainContentGenerator(NULL), mRayQueryExecutor(NULL)
 {
 }
@@ -144,4 +169,5 @@
 	OGRE_DELETE(mTerrainContentGenerator);
 	OGRE_DELETE(mRayQueryExecutor);
+
 	deleteEntityStates();
 }
@@ -151,7 +177,7 @@
 	for (int i = 0; i < (int)mEntityStates.size(); ++i)
 	{
-		delete mEntityStates[i];
-		mEntityStates[i] = NULL;
-	}
+		OGRE_DELETE(mEntityStates[i]);
+	}
+
 	mEntityStates.clear();
 }
@@ -248,6 +274,4 @@
 	mSceneMgr->setWorldGeometry(terrain_cfg);
 	
-	// ray query executor
-	mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
 
 	//-- CEGUI setup
@@ -274,4 +298,10 @@
 	mSceneMgr->_renderScene(mCamera, mWindow->getViewport(0), true);
 
+	// ray query executor: needed to clamp to terrain
+	mRayQueryExecutor = new RayQueryExecutor(mSceneMgr);
+
+	mTerrainMinPos = EntityState::msMinPos = Vector3(0, 0, 0);
+	mTerrainMaxPos = EntityState::msMaxPos = Vector3(3000, 5000, 3000);
+
 	mTerrainContentGenerator = new TerrainContentGenerator(mSceneMgr);
 	
@@ -280,6 +310,7 @@
 	{
 		// to provide much occlusion,
-		// height is restricted to 50 => no objects appear on peaks
-		mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 50.0f, 3000.0f));
+		// height is restricted to 50 => no objects are created on peaks
+		mTerrainContentGenerator->SetMinPos(Vector3(mTerrainMinPos));
+		mTerrainContentGenerator->SetMaxPos(Vector3(mTerrainMaxPos.x, 50.0f, mTerrainMaxPos.z));
 		mTerrainContentGenerator->SetOffset(0);
 
@@ -288,7 +319,7 @@
 	}
 
-	// no limitations needed anymore: the user can set 
-	// objects also on peaks of terrain
-	mTerrainContentGenerator->SetMaxPos(Vector3(3000.0f, 5000.0f, 3000.0f));
+	// no limitations anymore => it is possible for the user to put single 
+	// objects on peaks of the terrain (only few, not relevant for occlusion)
+	mTerrainContentGenerator->SetMaxPos(mTerrainMaxPos);
 }
 //-----------------------------------------------------------------------
@@ -314,5 +345,5 @@
 	for (int i = 0; i < (int)mEntityStates.size(); ++i)
 	{
-		SceneNode *sm = mEntityStates[i]->GetEntity()->getParentSceneNode();
+		SceneNode *sn = mEntityStates[i]->GetEntity()->getParentSceneNode();
 
 		mEntityStates[i]->update(timeSinceLastFrame);
@@ -320,6 +351,5 @@
 		if (mEntityStates[i]->GetState() == EntityState::MOVING)
 		{
-			Clamp2Terrain(sn, 0);
-			//sm->setNodeVisible(false);
+			Clamp2Terrain(sn, 0); //sn->setNodeVisible(false);
 		}
 	}
Index: trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 161)
+++ trunk/VUT/work/TestCullingTerrain/TerrainFrameListener.h	(revision 164)
@@ -112,5 +112,5 @@
 	void toggleShowShadows();
 	void toggleDisplayCameraDetails();
-	void takeScreenShot();
+	void takeScreenshot();
 	void nextSceneDetailLevel();
 	void nextFilter();
@@ -125,5 +125,4 @@
 	void toggleShowViz();
 	void toggleRecord();
-	void toggleUseAnimation();
 
 	/** Applies visibility query. Collects the visible objects 
@@ -241,4 +240,5 @@
 	float mMoveScale;
     Degree mRotScale;
+
     // just to stop toggles flipping too fast
     Real mTimeDelay;
@@ -272,4 +272,6 @@
 	int mCurrentObjectType;
 
+	bool mDeleteObjects;
+
 	TestCullingTerrainApplication *mApplication;
 };
Index: trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h
===================================================================
--- trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 161)
+++ trunk/VUT/work/TestCullingTerrain/TestCullingTerrainApplication.h	(revision 164)
@@ -34,5 +34,5 @@
 
 	EntityState(Entity *ent, State entityState, Real speed);
-
+	~EntityState();
 	/** Update the entity state.
 	*/
@@ -46,4 +46,9 @@
 	*/
 	EntityState::State GetState();
+
+	static bool OutOfBounds(SceneNode *node);
+
+	static Vector3 msMinPos;
+	static Vector3 msMaxPos;
 
 protected:
@@ -136,4 +141,6 @@
 	RayQueryExecutor *mRayQueryExecutor;
 
+	Vector3 mTerrainMinPos;
+	Vector3 mTerrainMaxPos;
 private:
 	void chooseSceneManager(void);
