/*
	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_CONTACT_H
#define GAME_CONTACT_H

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

namespace Game
{
	//=================================================================================================================
	/// @brief		This class represents a contact in a collision. It stores the position of the contact as well as
	///				the contact normal.
	//=================================================================================================================

	struct Contact
	{
		public:

			//=========================================================================================================
			// Functions
			//=========================================================================================================

				//=====================================================================================================
				/// @brief		Creates a default invalid contact
				//=====================================================================================================

				Contact()
				{
					m_normal		= Vec3f::UnitX();
					m_penetration	= 0;
				}

				//=====================================================================================================
				/// @brief		Creates a contact space orientation matrix from the contact normal. This can be used
				///				to transform to and from contact space. The x axis is in the direction of the contact
				///				normal. The y and z directions are chosen arbitrarily since there can be any number of 
				///				permutations of these two directions.
				/// @returns	The orientation matrix for contact space.
				//=====================================================================================================

				Matrix3f GetContactSpaceOrientationMatrix() const
				{
					// Need to pick a y and z axis for contact space

					Vec3f y;
					Vec3f z;

					//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
					// See which component of the normal is more significant: x or z. Pick the y axis according to 
					// this significance so as to avoid the y axis being in the same (or reverse) direction of the 
					// normal.
					//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

					if ( Float::Abs( m_normal.z ) > Float::Abs( m_normal.x ) )
					{
						y = Vec3f::UnitX();
					}
					else
					{
						y = Vec3f::UnitZ();
					}

					// Get the z direction:

					z = m_normal.Cross(y);

					// Get the proper y direction:

					y = z.Cross(m_normal);

					// Makeup the contact space orientation matrix and return

					return Matrix3f
					(
						m_normal.x	, y.x	, z.x ,
						m_normal.y	, y.y	, z.y ,
						m_normal.z	, y.z	, z.z 
					);
				}

				//=====================================================================================================
				/// @brief		Returns a 4x4 transform matrix that transforms a vector into contact space
				/// @returns	A transform matrix that transforms a vector into contact space
				//=====================================================================================================

				inline Matrix4f GetToContactSpaceMatrix() const
				{
					// Return it:

					return Matrix4f::Translation(-m_position) * Matrix4f( GetContactSpaceOrientationMatrix().Transpose() );
				}

				//=====================================================================================================
				/// @brief		Returns a 4x4 transform matrix that transforms a vector from contact space into world
				///				space.
				/// @returns	A transform matrix that transforms a vector from contact space into world space
				//=====================================================================================================

				inline Matrix4f GetFromContactSpaceMatrix() const
				{
					// Return it:

					return Matrix4f( GetContactSpaceOrientationMatrix() ) * Matrix4f::Translation(m_position);
				}
				
				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.				
				//=====================================================================================================
				
				inline bool operator == ( const Contact & other ) const { return m_penetration == other.m_penetration; }

				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.				
				//=====================================================================================================
				
				inline bool operator != ( const Contact & other ) const { return m_penetration != other.m_penetration; }
				
				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.
				//=====================================================================================================
				
				inline bool operator > ( const Contact & other ) const { return m_penetration < other.m_penetration; }

				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.				
				//=====================================================================================================
				
				inline bool operator >= ( const Contact & other ) const { return m_penetration <= other.m_penetration; }

				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.				
				//=====================================================================================================
				
				inline bool operator < ( const Contact & other ) const { return m_penetration > other.m_penetration; }

				//=====================================================================================================
				/// @brief		Compares two contact points based on penetration depth
				/// @param		other - other contact point to compare with
				/// @returns	Result of the comparison
				/// @remarks	Contacts with less penetration are regarded as being less significant. This is so
				///				contacts can be sorted in a list where the most significant penetrations are processed
				///				first.				
				//=====================================================================================================
				
				inline bool operator <= ( const Contact & other ) const { return m_penetration >= other.m_penetration; }
				
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// Position of the contact point

				Vec3f m_position;

				/// Contact normal. This is the direction to move back along to resolve a collision.

				Vec3f m_normal;

				/// Soft body world velocity of the point during deformation. Only used for soft bodies.

				Vec3f m_deformation_velocity;

				/// Penetration depth

				float m_penetration;
	};
}

#endif
