/*
	Trampoline Sim.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GAME_GRID_MESH_SOFT_BODY_H
#define GAME_GRID_MESH_SOFT_BODY_H

//=====================================================================================================================
//=====================================================================================================================

#include "Physics/Body.h"

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	class ShapeHeightfield;
		
	//=================================================================================================================
	/// @brief		This class represents a soft body composed of a grid mesh style data structure. The body is a plane 
	///				(xz) where by the plane is divided up into equal quad parts and whereby the sides of the plane are 
	///				fixed and cannot move.
	//=================================================================================================================
	
	class GridMeshSoftBody : public Body
	{
		public:
		
			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates the grid mesh soft body
				
				GridMeshSoftBody( ShapeHeightfield * shape , float  mass = 1.0f );

				/// Destroys the grid mesh soft body

				~GridMeshSoftBody();
				
				///	Updates all the dynamics and forces acting on the rigid body
				
				virtual void Integrate( float time_delta );

				/// Collides this rigid body with the given rigid body and does collision response:

				virtual void Collide( float time_delta , Body & other );
				
				/// Called after the end of each update to the object: after Integrate() and Collide()
				
				virtual void EndUpdate();

				/// Soft body check. Returns true for this type of body.

				virtual bool IsSoftBody(){ return true; }
				
				/// Apply functions

				#pragma region(APPLY_X_FUNCTIONS)

					/// Applies a given force through the center of mass of the body. Force is given in world coordinates.
					
					virtual void ApplyWorldForce( const Vec3f & force );
					
					/// Applies a given force from a given point in world coordinates. Force is given in world coordinates also.
					
					virtual void ApplyWorldForce( const Vec3f & force , const Vec3f & position );

					/// Applies torque to the body: this is torque in world coordinates
					
					virtual void ApplyWorldTorque( const Vec3f & torque );
					
					/// Applies a given linear acceleration to the body in world coordinates
					
					virtual void ApplyWorldAcceleration( const Vec3f & acceleration );

					/// Applies a given linear acceleration to the body at the given position in world coordinates. Acceleration is in world coords also.
					
					virtual void ApplyWorldAcceleration( const Vec3f & acceleration , const Vec3f & position );

					/// Applies a given angular acceleration to the body in world coordinates
					
					virtual void ApplyWorldAngularAcceleration( const Vec3f & angular_acceleration );
					
					/// Applies an impulse through the center of the body in world coordinates. 
					
					virtual void ApplyWorldImpulse( const Vec3f & impulse );

					/// Applies an impulse to the body at the given world position. Impulse is also in world coordinates.
					
					virtual void ApplyWorldImpulse( const Vec3f & impulse , const Vec3f & position );
				
				#pragma endregion				
				
		private:		
		
			//=========================================================================================================
			// Constants
			//=========================================================================================================
			
				/// Minimum stiffness at the center of the trampoline
				
				static const float MIN_STIFFNESS;

				/// Maximum stiffness at the center of the trampoline
				
				static const float MAX_STIFFNESS;
				
				/// Dampening factor for the springs
				
				static const float DAMPENING;
		
			//=========================================================================================================
			// Structures
			//=========================================================================================================
			
				/// Structure containing additional non positional information for each trampoline point
				
				struct TrampolinePoint
				{
					/// Pointer to the actual position in the heightfield shape itself
					
					Vec3f * m_position;

					/// Pointer to the velocity of the vertex in the heightfield shape itself

					float * m_velocity;
					
					/// Stiffness of the spring at this point
					
					float m_stiffness;
				};
				
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// The array of trampoline points:
				
				TrampolinePoint * m_points;

				/// Velocities or derivatives in the RK4 method: K1

				float * m_rk4_k1_velocities;

				/// Velocities or derivatives in the RK4 method: K2

				float * m_rk4_k2_velocities;

				/// Velocities or derivatives in the RK4 method: K3

				float * m_rk4_k3_velocities;

				/// Velocities or derivatives in the RK4 method: K4

				float * m_rk4_k4_velocities;

			//=========================================================================================================
			// Functions
			//=========================================================================================================
		
				/// Copying is disallowed
				
				GridMeshSoftBody( const GridMeshSoftBody & other );

				/// Copying is disallowed
				
				GridMeshSoftBody & operator = ( const GridMeshSoftBody & other );
	};
}

#endif
