include/vamos/body/Rigid_Body.h

Go to the documentation of this file.
00001 //  Rigid_Body.h - a rigid body.
00002 //
00003 //  Copyright (C) 2001--2002 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 #ifndef _RIGID_BODY_H_
00022 #define _RIGID_BODY_H_
00023 
00024 #include <vamos/geometry/Three_Vector.h>
00025 #include <vamos/geometry/Three_Matrix.h>
00026 #include <vamos/geometry/Material.h>
00027 #include <vamos/geometry/Inertia_Tensor.h>
00028 #include <vamos/body/Frame.h>
00029 #include <vamos/body/Contact_Point.h>
00030 
00031 #include <vector>
00032 
00033 namespace Vamos_Body
00034 {
00035   struct Contact_Parameters
00036   {
00037         Contact_Parameters ();
00038 
00039         Particle* mp_contact_point;
00040         double m_distance;
00041         Vamos_Geometry::Three_Vector m_normal;
00042         Vamos_Geometry::Material_Handle m_material;
00043   };
00044 
00045   //* A rigid body.
00046   class Rigid_Body : public Frame
00047   {
00048         // The body's initial position vector, used by reset ().
00049         Vamos_Geometry::Three_Vector m_initial_position;
00050 
00051         Vamos_Geometry::Three_Vector m_last_position;
00052 
00053         // The velocity of the center of mass.
00054         Vamos_Geometry::Three_Vector m_cm_velocity;
00055 
00056         Vamos_Geometry::Three_Vector m_last_cm_velocity;
00057 
00058         Vamos_Geometry::Three_Vector m_last_velocity;
00059 
00060         Vamos_Geometry::Three_Matrix m_last_orientation;
00061 
00062         Vamos_Geometry::Three_Vector m_last_ang_velocity;
00063 
00064         // The acceleration due to gravity, distance/time^2.  The units
00065         // determine the distance and time units used in the rest of the
00066         // simulation.
00067         Vamos_Geometry::Three_Vector m_gravity;
00068 
00069         // The elapsed time since the last time step.
00070         double m_delta_time;
00071 
00072         // The total mass of the body.
00073         double m_mass;
00074 
00075         // Common code for the two reset () methods.
00076         void private_reset ();
00077 
00078         Contact_Parameters m_contact_parameters;
00079         
00080         bool valid;
00081 
00082   protected:
00083         // The inertia tensor for the body.
00084         Vamos_Geometry::Inertia_Tensor m_inertia;
00085 
00086         // A vector of pointers to the force and torque producers that
00087         // make up the body
00088         std::vector <Particle*> m_particles;
00089 
00090         // The position of the center of mass of the body relative to the
00091         // origin of the body.
00092         Vamos_Geometry::Three_Vector m_body_cm;
00093   
00094         Vamos_Geometry::Three_Vector m_last_body_cm;
00095 
00096   public:
00097         //** Constructors
00098 
00099         // Specify the position and orientation of the body.
00100         Rigid_Body (const Vamos_Geometry::Three_Vector& pos, 
00101                 const Vamos_Geometry::Three_Matrix& orient);
00102 
00103         // Specify the position, the orientation is the same as the parent.
00104         Rigid_Body (const Vamos_Geometry::Three_Vector& pos);
00105 
00106         // The body is coincident with the parent.
00107         Rigid_Body ();
00108 
00109         //** Destructor
00110         virtual ~Rigid_Body ();
00111         //~Rigid_Body();
00112 
00113     Vamos_Geometry::Three_Vector center_of_mass () const { return m_body_cm; }
00114 
00115         // Return the position of the center of mass of the body with
00116         // respect to the world.
00117         Vamos_Geometry::Three_Vector cm_position ();
00118 
00119         // Provide access to the frame's position method.  Return the
00120         // origin of the body.
00121         Vamos_Geometry::Three_Vector position () const 
00122         { return Frame::position (); }
00123         
00124         bool IsValid() {return valid;}
00125 
00126         // Return the contact position of the particle with respect to the
00127         // world.
00128         Vamos_Geometry::Three_Vector contact_position (Particle* contact_point);
00129         Vamos_Geometry::Three_Vector last_contact_position (Particle* contact_point);
00130         
00131         // Return the smallest contact position z-value of the particles.
00132         double lowest_contact_position () const;
00133 
00134         // Add a particle to the body.
00135         void add_particle (Particle* const comp) 
00136         { m_particles.push_back (comp); } 
00137 
00138         // Calculate the center of mass, the ineritia tensor, and its
00139         // inverse.
00140         void update_center_of_mass ();
00141 
00142         // Provide access to the particles.
00143         std::vector <Particle*>& particles () { return m_particles; }
00144 
00145         void find_forces ();
00146 
00147         // Advance the body in time by TIME.
00148         void propagate (double time);
00149         
00150         void propagate_contact ();
00151 
00152         // Undo the last propagation.
00153         void rewind ();
00154 
00155         // Finish the timestep.
00156         void end_timestep ();
00157 
00158         // Called by the world to tell the body what the acceleration due
00159         // to gravity is.
00160         void gravity (const Vamos_Geometry::Three_Vector& grav) 
00161         { m_gravity = grav; }
00162 
00163         // Return the velocity of the particle in the parent frame.
00164         // With no argument, return the velocity of the origin of the body
00165         // frame.
00166         Vamos_Geometry::Three_Vector velocity (Particle* particle);
00167 
00168         // Return the velocity of the center of mass.
00169         Vamos_Geometry::Three_Vector cm_velocity () const { return m_cm_velocity; }
00170         Vamos_Geometry::Three_Vector cm_last_velocity () const { return m_last_cm_velocity; }
00171 
00172         // Set the velocity of the center of mass.
00173         void cm_velocity (Vamos_Geometry::Three_Vector vel) 
00174         { m_cm_velocity = vel;}
00175 
00176         // Handle a collision.
00177         void contact (Particle* contact_point, 
00178                                   double distance,
00179                                   const Vamos_Geometry::Three_Vector& normal,
00180                                   Vamos_Geometry::Material_Handle material);
00181 
00182         // Transform the wind into the body frame and send it to the 
00183         // aerodynamic device.
00184         void wind (Particle* aero_device, 
00185                            const Vamos_Geometry::Three_Vector& wind_vector, 
00186                            double density);
00187 
00188         // Return the total mass.
00189         double mass () const { return m_mass; }
00190 
00191         // This function is defined by subclasses that work with a
00192         // graphics system.
00193         virtual void draw () {};
00194 
00195         // Return the body to its initial state at its initial position.
00196         virtual void reset ();
00197 
00198         // Return the body to its initial state at a particular position and
00199         // orientation.
00200         virtual void reset (const Vamos_Geometry::Three_Vector& position, 
00201                                                 const Vamos_Geometry::Three_Matrix& orientation);
00202         
00203         void set_position(Vamos_Geometry::Three_Vector np) {place(np);m_last_position = np;}
00204         void set_orientation(Vamos_Geometry::Three_Matrix no) {orient(no);m_last_orientation = no;}
00205         void set_angvel(Vamos_Geometry::Three_Vector nav) {set_ang_velocity(nav); m_last_ang_velocity = nav;}
00206         void set_velocity(Vamos_Geometry::Three_Vector vel) {m_cm_velocity = vel; m_velocity = vel; m_last_velocity = vel; m_last_cm_velocity = vel;}
00207         
00208         //set current state to last state
00209         void roll_back();
00210         
00211         //set velocities to zero
00212         void kill_vel();
00213         
00214         //respond to a single point contact
00215         void single_point_contact(Vamos_Geometry::Three_Vector worldposition,
00216                 double distance,
00217                 Vamos_Geometry::Three_Vector normal,
00218                 Vamos_Geometry::Material_Handle material, double time);
00219   };
00220 }
00221 
00222 #endif // !_BODY_H_

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