src/vamos/body/Wheel.cc

Go to the documentation of this file.
00001 //  Wheel.cc - a wheel.
00002 //
00003 //  Copyright (C) 2001--2004 Sam Varner
00004 //
00005 //  This file is part of Vamos Automotive Simulator.
00006 //
00007 //  This program is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU General Public License as published by
00009 //  the Free Software Foundation; either version 2 of the License, or
00010 //  (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021 #include <vamos/body/Wheel.h>
00022 #include <vamos/geometry/Conversions.h>
00023 //#include <vamos/geometry/Ac3d.h>
00024 #include <iostream>
00025 
00026 using namespace Vamos_Geometry;
00027 
00028 //* Class Wheel
00029 
00030 //** Constructor
00031 Vamos_Body::
00032 Wheel::Wheel (double mass, 
00033                           Three_Vector position, 
00034                           double tire_offset,
00035                           double roll_height,
00036                           double restitution,
00037                           Suspension* suspension, 
00038                           const Tire& tire, 
00039                           const Brake& brake,
00040                           bool steered,
00041                           bool driven,
00042                           Side side) :
00043   // Friction is handled by the tire.
00044   Contact_Point (mass, position, Material::RUBBER,
00045                                  0.0, restitution),
00046   m_original_position (position),
00047   m_tire_offset ((side == RIGHT) ? -tire_offset : tire_offset),
00048   m_roll_height (roll_height),
00049   mp_suspension (suspension),
00050   m_tire (tire),
00051   m_brake (brake),
00052   m_ground_velocity (0.0, 0.0, 0.0),
00053   m_normal (Three_Vector (0.0, 0.0, 0.0)),
00054   m_ang_velocity (Three_Vector (0.0, 0.0, 0.0)),
00055   m_tire_torque (0.0),
00056   m_drive_torque (0.0),
00057   m_braking_torque (0.0),
00058   m_steered (steered),
00059   m_driven (driven),
00060   m_side (side),
00061 /*  m_slow_wheel_list (0),
00062   m_fast_wheel_list (0),
00063   m_stator_list (0),*/
00064   m_transition_speed (10.0),
00065   m_rotation (0.0)
00066 {
00067 }
00068 
00069 void Vamos_Body::
00070 Wheel::find_forces ()
00071 {
00072   if (!m_contact)
00073         {
00074           m_force.zero ();
00075           m_torque.zero ();
00076           m_impulse.zero ();
00077           m_position = m_original_position;
00078           mp_suspension->reset ();
00079         }
00080 
00081   m_tire.input (m_ground_velocity, 
00082                                 m_ang_velocity [2],
00083                                 mp_suspension->force ().project (m_normal),
00084                                 mp_suspension->current_camber (m_normal.unit () [1]),
00085                                 m_drive_torque + m_braking_torque,
00086                                 m_brake.is_locked (), 
00087                                 m_material);
00088 
00089   m_tire.find_forces ();
00090   m_force = m_tire.force ();
00091   m_torque = m_tire.torque ();
00092   m_torque [1] *= -1.0;
00093 }
00094 
00095 // Advance the wheel forward in time by TIME.
00096 void Vamos_Body::
00097 Wheel::propagate (double time)
00098 {
00099   m_tire.propagate (time);
00100   orient (mp_suspension->orientation ());
00101   m_rotation += speed () * time / m_tire.radius ();
00102         //cout << m_rotation << endl;
00103 }
00104 
00105 void Vamos_Body::
00106 Wheel::rewind ()
00107 {
00108   m_tire.rewind ();
00109 }
00110 
00111 // Handle collisions.  The return value is the displacement of the
00112 // wheel as a result of the contact.
00113 double Vamos_Body::
00114 Wheel::contact (const Three_Vector& position,
00115                                 const Inertia_Tensor& inertia,
00116                                 const Three_Vector& velocity, 
00117                                 double distance,
00118                                 const Three_Vector& normal,
00119                                 const Three_Vector& ang_velocity,
00120                                 Material_Handle material)
00121 {
00122   m_contact = true;
00123   if (mp_suspension->bottomed_out ())
00124         {
00125           Particle::contact (position, 
00126                                                  inertia, 
00127                                                  rotate_in (velocity), 
00128                                                  distance, 
00129                                                  rotate_in (normal), 
00130                                                  rotate_in (ang_velocity), 
00131                                                  material);
00132         }
00133 
00134   m_impulse.zero ();
00135         
00136   m_normal = rotate_in (normal);
00137   Three_Vector v_perp = rotate_in (velocity).project (m_normal);
00138   m_ground_velocity = rotate_in (velocity) - v_perp;
00139   m_ang_velocity = ang_velocity;
00140   Three_Vector disp = 
00141         (m_normal * distance).back_project (Three_Vector (0.0, 0.0, -1.0));
00142   mp_suspension->displace (disp.abs ());
00143   mp_suspension->input (m_force, m_normal);
00144   mp_suspension->torque (m_braking_torque);
00145   m_material = material;
00146   m_position = m_original_position + disp;
00147   double rdisp = mp_suspension->displacement();
00148         return -rdisp;
00149 }
00150 
00151 void Vamos_Body::
00152 Wheel::drive_torque (double torque_in)
00153 {
00154   m_drive_torque = torque_in;
00155 }
00156 
00157 // Return the torque exerted on the wheel by the brake when a pressure
00158 // of FACTOR * Brake::m_max_pressure is applied to the brake.
00159 void Vamos_Body::
00160 Wheel::brake (double factor)
00161 {
00162   m_braking_torque = -m_brake.torque (factor, m_tire.rotational_speed ());
00163 }
00164 
00165 // Return the position relative to the body where the wheel exerts its
00166 // force.
00167 Three_Vector Vamos_Body::
00168 Wheel::force_position () const
00169 {
00170   return m_position + m_tire.contact_position () 
00171         + Three_Vector (0.0, m_tire_offset, m_roll_height);
00172 }
00173 
00174 // Return the position of the wheel relative to the body for the
00175 // purpose of detecting collisions.
00176 Three_Vector Vamos_Body::
00177 Wheel::contact_position () const
00178 {
00179   return m_original_position + m_tire.contact_position ()
00180         + Three_Vector (0.0, m_tire_offset, 0.0);
00181 }
00182 
00183 // Return the position of the wheel relative to the body
00184 Three_Vector Vamos_Body::
00185 Wheel::position () const
00186 {
00187   return m_position;
00188 }
00189 
00190 // Retrun the slip ratio for the wheel.
00191 double Vamos_Body::
00192 Wheel::slip () const
00193 {
00194   if (m_ground_velocity [0] == 0.0) return 0.0;
00195 
00196   return (speed () - m_ground_velocity [0]) / std::abs (m_ground_velocity [0]);
00197 }
00198 
00199 // Return the wheel to its initial conditions.
00200 void Vamos_Body::
00201 Wheel::reset ()
00202 {
00203   Particle::reset ();
00204   m_tire.reset ();
00205 }
00206 
00207 /*GLuint Vamos_Body::
00208 Wheel::make_model (std::string file, double scale, 
00209                                    const Three_Vector& translation, 
00210                                    const Three_Vector& rotation)
00211 {
00212 //  Ac3d* model = new Ac3d (file, scale, translation, rotation);
00213   //GLuint display_list = model->build ();
00214   //delete model;
00215   //return display_list;
00216 }*/
00217 
00218 void Vamos_Body::
00219 Wheel::set_models (std::string slow_file, 
00220                                    std::string fast_file, 
00221                                    double transition_speed,
00222                                    std::string stator_file,
00223                                    double stator_offset,
00224                                    double scale,
00225                                    const Three_Vector& translation,
00226                                    const Three_Vector& rotation)
00227 {
00228   Three_Vector offset;
00229   if (stator_file != "")
00230         {
00231           offset [1] += (m_side == RIGHT) ? stator_offset : -stator_offset;
00232         }
00233         
00234         //printf("%s, %s\n", slow_file.c_str(), fast_file.c_str());
00235         
00236         model.Load(slow_file,true);     
00237         
00238   /*if (m_slow_wheel_list != 0)
00239         {
00240           glDeleteLists (m_slow_wheel_list, 1);
00241         }
00242   m_slow_wheel_list = make_model (slow_file, scale, translation + offset, rotation);
00243 
00244   if (m_fast_wheel_list != 0)
00245         {
00246           glDeleteLists (m_fast_wheel_list, 1);
00247         }
00248   m_fast_wheel_list = 
00249         make_model (fast_file, scale, translation + offset, rotation);
00250 
00251   if (stator_file != "")
00252         {
00253           if (m_stator_list != 0)
00254                 {
00255                   glDeleteLists (m_stator_list, 1);
00256                 }
00257           m_stator_list = make_model (stator_file, scale, translation, rotation);
00258         }*/
00259         
00260         m_transition_speed = transition_speed;
00261 }
00262 
00263 extern GLfloat LightPosition[4];
00264 
00265 void Vamos_Body::
00266 Wheel::transform ()
00267 {
00268   glTranslatef (m_position [0], m_position [1], m_position [2]);
00269   double angle;
00270   Three_Vector axis = axis_angle (&angle);
00271         glRotatef (angle, axis [0], axis [1], axis [2]);
00272         glRotatef(m_rotation*(180/3.141593), 0, 1, 0);
00273         
00274         float lp[4];
00275         lp[0] = -LightPosition[0];
00276         lp[1] = LightPosition[2];
00277         lp[2] = LightPosition[1];
00278         lp[3] = 0;
00279         //glLightfv( GL_LIGHT1, GL_POSITION, lp );
00280 }
00281 
00282 // Draw the wheel.
00283 void Vamos_Body::
00284 Wheel::draw ()
00285 {
00286   glPushMatrix ();
00287   transform ();
00288         //model.Draw(0,0);
00289         if (m_side == RIGHT)
00290                 glScalef(1,-1,1);
00291         model.DrawStatic();
00292 /*  glCallList (m_stator_list);
00293   if (speed () < m_transition_speed)
00294         {
00295           glRotatef (rad_to_deg (m_rotation), 0.0, 1.0, 0.0);
00296           glCallList (m_slow_wheel_list);
00297         }
00298   else
00299         {
00300           glCallList (m_fast_wheel_list);
00301         }*/
00302   glPopMatrix ();
00303 glPushMatrix();
00304   mp_suspension->draw ();
00305         glPopMatrix();
00306 }
00307 
00308 Vamos_Body::Suspension * Vamos_Body::
00309 Wheel::suspension()
00310 {
00311         return mp_suspension;
00312 }
00313 
00314 Vamos_Geometry::Three_Vector Vamos_Body::
00315 Wheel::ang_velocity()
00316 {
00317         return m_ang_velocity;
00318 }

Generated on Thu Oct 19 04:05:55 2006 by  doxygen 1.4.6