dNewtonCollisionHeightField::dNewtonCollisionHeightField(dNewtonWorld* const world, const dFloat* const elevations, int resolution, dVector scale)
	:dNewtonCollision(world, 0)
{
	char* const attibute = new char[resolution * resolution];
	memset(attibute, 0, sizeof(char) * resolution * resolution);

	dFloat scaleFactor = 1.0f / (resolution - 1);
	NewtonCollision* const shape = NewtonCreateHeightFieldCollision(
		m_myWorld->m_world, resolution, resolution, 1, 0, elevations, attibute, 1.0f, scale.m_x * scaleFactor, scale.m_z * scaleFactor, 0);

	delete[] attibute;
	SetShape(shape);
}
	void AddCollisionHeightFieldMesh(DemoEntityManager* const scene)
	{
		int size = 101;
		dFloat* const elevation = new dFloat[size * size];

		//y = a * sin(x)* (sin (z)) ^2
		for (int i = 0; i < size; i ++) {
			dFloat z = 3.1416f * i / (size - 1);
			dFloat z2 = dSin (z);
			z2 = z2 * z2 * 5.0f;
			for (int j = 0; j < size; j ++) {
				dFloat x = 3.1416f * j / (size - 1);
				dFloat y = z2 * dSin(x);
				elevation[j * size + i] = y;
			}
		}
		dFloat cellsize = 0.25f;
		DemoMesh* const mesh = new DemoMesh ("terrain", elevation, size, cellsize, 1.0f/16.0f, size - 1);
		
		int width = size;
		int height = size;
		char* const attibutes = new char [size * size];
		memset (attibutes, 0, width * height * sizeof (char));
		NewtonCollision* const collision = NewtonCreateHeightFieldCollision (scene->GetNewton(), width, height, 1, 0, elevation, attibutes, 1.0f, cellsize, 0);

		void* const proxy  = NewtonSceneCollisionAddSubCollision (m_sceneCollision, collision);
		NewtonDestroyCollision(collision);


		// set the parameter on the added collision share
		dMatrix matrix (dGetIdentityMatrix());
		matrix.m_posit.m_y = -2.0f;
		matrix.m_posit.m_x = -15.0f;
		matrix.m_posit.m_z = -10.0f;
		NewtonCollision* const terrainCollision = NewtonSceneCollisionGetCollisionFromNode (m_sceneCollision, proxy );

		NewtonSceneCollisionSetSubCollisionMatrix (m_sceneCollision, proxy, &matrix[0][0]);	
		NewtonCollisionSetUserData(terrainCollision, mesh);

		delete[] elevation;
		delete[] attibutes;
	}
	static NewtonBody* CreateHeightFieldTerrain (DemoEntityManager* const scene, int sizeInPowerOfTwos, dFloat cellSize, dFloat elevationScale, dFloat roughness, dFloat maxElevation, dFloat minElevation)
	{
		int size = (1 << sizeInPowerOfTwos) + 1 ;
		dFloat* const elevation = new dFloat [size * size];
		//MakeFractalTerrain (elevation, sizeInPowerOfTwos, elevationScale, roughness, maxElevation, minElevation);
		MakeFractalTerrain (elevation, sizeInPowerOfTwos, elevationScale, roughness, maxElevation, minElevation);

		for (int i = 0; i < 4; i ++) {
			ApplySmoothFilter (elevation, size);
		}
//memset (elevation, 0, size * size * sizeof (dFloat));

		//SetBaseHeight (elevation, size);
		// apply simple calderas
		//		MakeCalderas (elevation, size, maxElevation * 0.7f, maxElevation * 0.1f);

		//	// create the visual mesh
		DemoMesh* const mesh = new DemoMesh ("terrain", elevation, size, cellSize, 1.0f/4.0f, TILE_SIZE);

		DemoEntity* const entity = new DemoEntity(dGetIdentityMatrix(), NULL);
		scene->Append (entity);
		entity->SetMesh(mesh, dGetIdentityMatrix());
		mesh->Release();

		// create the height field collision and rigid body

		// create the attribute map
		int width = size;
		int height = size;
		char* const attibutes = new char [size * size];
		memset (attibutes, 0, width * height * sizeof (char));
		NewtonCollision* collision = NewtonCreateHeightFieldCollision (scene->GetNewton(), width, height, 1, 0, elevation, attibutes, 1.0f, cellSize, 0);


#ifdef USE_STATIC_MESHES_DEBUG_COLLISION
		NewtonStaticCollisionSetDebugCallback (collision, ShowMeshCollidingFaces);
#endif

		NewtonCollisionInfoRecord collisionInfo;
		// keep the compiler happy
		memset (&collisionInfo, 0, sizeof (NewtonCollisionInfoRecord));
		NewtonCollisionGetInfo (collision, &collisionInfo);

		width = collisionInfo.m_heightField.m_width;
		height = collisionInfo.m_heightField.m_height;
		//elevations = collisionInfo.m_heightField.m_elevation;

		dVector boxP0; 
		dVector boxP1; 
		// get the position of the aabb of this geometry
		dMatrix matrix (entity->GetCurrentMatrix());
		NewtonCollisionCalculateAABB (collision, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 
		matrix.m_posit = (boxP0 + boxP1).Scale (-0.5f);
		matrix.m_posit.m_w = 1.0f;
		//SetMatrix (matrix);
		entity->ResetMatrix (*scene, matrix);

		// create the terrainBody rigid body
		NewtonBody* const terrainBody = NewtonCreateDynamicBody(scene->GetNewton(), collision, &matrix[0][0]);

		// release the collision tree (this way the application does not have to do book keeping of Newton objects
		NewtonDestroyCollision (collision);

		// in newton 300 collision are instance, you need to ready it after you create a body, if you want to male call on the instance
		collision = NewtonBodyGetCollision(terrainBody);


#if 0
		// uncomment this to test horizontal displacement
		unsigned short* const horizontalDisplacemnet = new unsigned short[size * size];
		for (int i = 0; i < size * size; i++) {
			horizontalDisplacemnet[i] = dRand();
		}
		NewtonHeightFieldSetHorizontalDisplacement(collision, horizontalDisplacemnet, 0.02f);
		delete horizontalDisplacemnet;
#endif

		// save the pointer to the graphic object with the body.
		NewtonBodySetUserData (terrainBody, entity);

		// set the global position of this body
		//	NewtonBodySetMatrix (m_terrainBody, &matrix[0][0]); 

		// set the destructor for this object
		//NewtonBodySetDestructorCallback (terrainBody, Destructor);

		// get the position of the aabb of this geometry
		//NewtonCollisionCalculateAABB (collision, &matrix[0][0], &boxP0.m_x, &boxP1.m_x); 

#ifdef USE_TEST_ALL_FACE_USER_RAYCAST_CALLBACK
		// set a ray cast callback for all face ray cast 
		NewtonTreeCollisionSetUserRayCastCallback (collision, AllRayHitCallback);
		
		dVector p0 (0,  100, 0, 0);
		dVector p1 (0, -100, 0, 0);
		dVector normal;
		dLong id;
		dFloat parameter;
		parameter = NewtonCollisionRayCast (collision, &p0[0], &p1[0], &normal[0], &id);
#endif

		delete[] attibutes;
		delete[] elevation;
		return terrainBody;
	}