//  Wheel.h - a wheel.
//
//  Copyright (C) 2001--2002 Sam Varner
//
//  This file is part of Vamos Automotive Simulator.
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#ifndef _WHEEL_H_
#define _WHEEL_H_

#include <vamos/body/Suspension.h>
#include <vamos/body/Tire.h>
#include <vamos/body/Brake.h>
#include <vamos/body/Contact_Point.h>
#include <vamos/geometry/Constants.h>
#include <vamos/geometry/Material.h>
#include <vamos/geometry/Three_Vector.h>
#include <vamos/geometry/Inertia_Tensor.h>

namespace Vamos_Body
{
  //* A wheel.  
  // Handles wheel geometry.  See Tire, Brake and Suspension for
  // related functionality.
  class Wheel : public Contact_Point
  {
  private:

	// The initial position of the wheel.
	Vamos_Geometry::Three_Vector m_original_position;

	// The suspension that the wheel is attached to.
	Suspension* mp_suspension;

	// The tire that is attached to the wheel.
	Tire m_tire;

	// The brake for the wheel.
	Brake m_brake;

	// The velocity of the ground relative to the wheel.
	Vamos_Geometry::Three_Vector m_ground_velocity;

	// The current normal vector for the road.
	Vamos_Geometry::Three_Vector m_normal;

	// The angular velocity of the wheel.
 	Vamos_Geometry::Three_Vector m_ang_velocity;
	
	// The surface we're currently on
	Vamos_Geometry::Material_Handle m_material;

	// The torque applied to the tire.
	double m_tire_torque;

	double m_drive_torque;

	double m_braking_torque;

	Vamos_Geometry::Three_Vector m_wheel_force;

	// True if the wheel responds to steering. 
	bool m_steered;

	// True if the wheel is driven.
	bool m_driven;

	// The side of the car that the wheel is on.
	Vamos_Geometry::Side m_side;

  public:
	//** Constructor
	Wheel (double mass, 
		   Vamos_Geometry::Three_Vector position, 
		   double restitution,
		   Suspension* suspension, 
		   const Tire& tire, 
		   const Brake& brake,
		   bool steered,
		   bool driven,
		   Vamos_Geometry::Side side);

	// Find and store the forces and torques for the current
	// configuration.
	void find_forces ();

	// Advance the wheel forward in time by TIME.
	void propagate (double time);

	// Undo the last propagation.
	void rewind ();

 	// Handle collisions.  The return value is how much the wheel was
 	// displaced by the collision.
	double contact (const Vamos_Geometry::Three_Vector& position,
					const Vamos_Geometry::Inertia_Tensor& inertia,
					const Vamos_Geometry::Three_Vector& vecocity, 
					double distance,
					const Vamos_Geometry::Three_Vector& normal,
					const Vamos_Geometry::Three_Vector& ang_velocity,
					Vamos_Geometry::Material_Handle material);

	// Apply the torque TORQUE_IN to the wheel.  This torque results
	// from acceleration or braking.
	void drive_torque (double torque_in);

	void brake (double factor);

	// Set the steering angle.
	void steer (double degree_angle) { mp_suspension->steer (degree_angle); }


	// Return the speed of the contact patch of the tire with respect
	// to the ground.
	double slide () const { return m_tire.slide (); }

	// Return the wheel's rotational speed in radians per second.
	double rotational_speed () const { return m_tire.rotational_speed (); }

	// Return the linear speed of the tire tread.  This is the speed
	// that a speedometer on this wheel would read.
	double speed () const { return m_tire.speed (); }

	// Return the position relative to the body where the wheel exerts
	// its force.
	Vamos_Geometry::Three_Vector force_position () const;

	// Return the position relative to the body where the wheel exerts
	// its torque.
	Vamos_Geometry::Three_Vector torque_position () const;

	// Return the position of the wheel relative to the body for the
	// purpose of detecting collisions.
	Vamos_Geometry::Three_Vector contact_position () const;

	// Return the slip ratio for the wheel;
	double slip () const;

	// Return the wheel to its initial conditions.
	void reset ();

	bool single_contact () const { return mp_suspension->bottomed_out (); }

	bool steered () const { return m_steered; }
	bool driven () const { return m_driven; }

	Vamos_Geometry::Side side () const { return m_side; }
  };
}

#endif // !_WHEEL_H_
