include/vamos/body/Car.h

Go to the documentation of this file.
00001 //  Car.h - a body with wheels.
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 #ifndef _CAR_H_
00022 #define _CAR_H_
00023 
00024 #define USE_XML false
00025 #define USE_CAR true
00026 
00027 #include <vamos/geometry/Three_Vector.h>
00028 #include <vamos/geometry/Two_Point.h>
00029 // #include <vamos/geometry/XML_Parser.h>
00030 #include <vamos/body/Rigid_Body.h>
00031 #include <vamos/body/Drivetrain.h>
00032 
00033 #include <vector>
00034 #include <utility>
00035 
00036 #include "settings.h"
00037 #include "configfile.h"
00038 #include "model.h"
00039 #include "globals.h"
00040 
00041 class BEZIER;
00042 
00043 class slSample;
00044 namespace Vamos_Geometry
00045 {
00046         class Sample;
00047 }
00048 
00049 namespace Vamos_Body
00050 {
00051 //      class Car_Reader;
00052         class Dashboard;
00053         class Fuel_Tank;
00054         class Particle;
00055         class Wheel;
00056 
00057         //* A class that handles gradual application of a control that's
00058         // operated by a button, such as the clutch.  If you're using a
00059         // keyboard instead of a joystick, it also handles steering, gas and
00060         // brake.
00061         class Key_Control
00062         {
00063                 // True if the next target is not set until the current one is
00064                 // reached. 
00065                 bool m_block;
00066 
00067                 // True if a target is waiting.
00068                 bool m_target_pending;
00069                 
00070                 // The current setting of this control.
00071                 double m_value;
00072 
00073                 // The desired setting of this control.
00074                 double m_target;
00075                 double m_next_target;
00076 
00077                 // How fast `m_value' approaches `m_target'.
00078                 double m_rate;
00079                 double m_next_rate;
00080 
00081                 // How long to wait before starting to change the setting.
00082                 double m_delay;
00083                 double m_next_delay;
00084 
00085                 // The time needed for the control to reach its target.
00086                 double m_time;
00087                 double m_next_time;
00088 
00089         public:
00090                 //** Constructor
00091                 Key_Control (bool block = false);
00092                 
00093                 // Set the target setting of this control.  NEW_TARGET is the
00094                 // desired setting.  TIME is how long it should take for the
00095                 // setting to go from 0.0 to 1.0 after waiting for DELAY.
00096                 // `m_rate' is calculated in this function.
00097                 void target (double new_target, double time = 0.0, double delay = 0.0);
00098 
00099                 // Update the setting of this control.  The setting move toward
00100                 // `m_target' by the ammount `m_rate' * TIME.
00101                 double update (double time);
00102 
00103                 // Return the current value of this control.
00104                 double value () const { return m_value; }
00105 
00106                 // Go immediately to the target.
00107                 void end ();
00108         
00109         bool target_pending() {return m_target_pending;}
00110         };
00111 
00112 
00113         //* A body with wheels.
00114         class Car
00115         {
00116 //              friend class Car_Reader;
00117 
00118         private:
00119                 std::string m_data_dir;
00120 
00121                 struct Crash_Box
00122                 {
00123                   double front;
00124                   double back;
00125                   double left;
00126                   double right;
00127                   double top;
00128                   double bottom;
00129 
00130                   // Return true if the position is within the crash box.
00131                   bool within (const Vamos_Geometry::Three_Vector& position) const;
00132                 };
00133                 Crash_Box m_crash_box;
00134         
00135                 CONFIGFILE m_car_definition;
00136                 bool LoadCarDefinition(string carfile);
00137                 bool LoadParts();
00138                 bool LoadPart(string partfile);
00139                 std::vector <Vamos_Geometry::Two_Point> m_tpoints;
00140 
00141         protected:
00142                 std::string m_car_file;
00143                 Rigid_Body m_chassis;
00144 
00145                 // A pointer to the car's drivetrain.
00146                 Drivetrain* mp_drivetrain;
00147 
00148                 // A pointer to the car's fuel tank.
00149                 Fuel_Tank* mp_fuel_tank;
00150 
00151                 // The fraction of braking torque applied to the front wheels.
00152                 double m_front_brake_bias;
00153 
00154                 // The maximum angle for the steered wheels.
00155                 double m_max_steer_angle;
00156 
00157                 // Steering non-linearity
00158                 double m_steer_exponent;
00159 
00160                 // Set the amount of decrease in sensitivity with speed.
00161                 double m_steer_speed_sensitivity;
00162 
00163                 // The sum of the sliding speeds of the tires.
00164                 double m_slide;
00165 
00166                 // True if a shift has been requested but not yet made due to the
00167                 // delay `m_shift_delay'.
00168                 bool m_shift_pending;
00169 
00170                 // The amount of time elapsed after the shift request.
00171                 double m_shift_timer;
00172 
00173                 // How long to wait between getting a shift request and actually
00174                 // shifting. 
00175                 double m_shift_delay;
00176 
00177                 // The gear to shift to when `m_shift_timer' reaches
00178                 // `m_shift_delay'. 
00179                 int m_new_gear;
00180 
00181                 // The gear we were in before the last shift was requested.
00182                 int m_last_gear;
00183 
00184                 // The control that gradually applies steering when the keyboard
00185                 // is used.
00186                 Key_Control m_steer_key_control;
00187 
00188                 // The control that gradually applies the throttle when the
00189                 // keyboard is used.
00190                 Key_Control m_gas_key_control;
00191 
00192                 // The control that gradually applies braking when the keyboard
00193                 // is used.
00194                 Key_Control m_brake_key_control;
00195                 
00196                 // The control that gradually applies handbraking when the keyboard
00197                 // is used.
00198                 Key_Control m_handbrake_key_control;
00199 
00200                 // The control that gradually applies the clutch when the keyboard
00201                 // is used.
00202                 Key_Control m_clutch_key_control;
00203 
00204                 // The control that gradually pans the view.
00205                 Key_Control m_pan_key_control;
00206 
00207                 // A pointer to the frontmost particle of the car.
00208                 Particle* mp_front_particle;
00209 
00210                 // The total distance traveled since the start of the simulation.
00211                 double m_distance_traveled;
00212 
00213                 // The position of the driver's eyes.
00214                 Vamos_Geometry::Three_Vector m_driver_view;
00215 
00216                 // The driver's field of view.
00217                 double m_field_of_view;
00218 
00219                 // The maximum pan angle.
00220                 double m_pan_angle;
00221 
00222                 // Display additional information if true.
00223                 bool m_show_dashboard_extras;
00224 
00225                 // Perform operations common to both reset() methods. 
00226                 void private_reset ();
00227         
00228                 std::vector <Wheel*> m_wheels;
00229         
00230                 int controller;
00231                 
00232                 int m_sector;
00233                 BEZIER * m_colpatches[4];
00234 
00235         public:
00236           
00237                 Vamos_Geometry::Three_Vector car_lastpos;
00238           
00239                 int get_controller() {return controller;}
00240                 void set_controller(int newc) {controller = newc;}
00241           
00242                 //** Constructor
00243                 Car (const Vamos_Geometry::Three_Vector& pos);
00244 
00245                 float brakesetting;
00246         
00247                 //** Destructor
00248                 virtual ~Car ();
00249                 
00250                 virtual void SetReflectionTexture(TEXTURE_HANDLE * reftid) {cout << "warning:  Car::SetReflectionTexture called instead of Gl_Car::SetReflectionTexture" << endl;}
00251 
00253                 Rigid_Body& chassis () { return m_chassis; }
00254 
00255                 // Read the car definition file.
00256                 void read (std::string data_dir = "", std::string car_file = "");
00257 
00258                 // Define a sound for the engine.
00259                 virtual void engine_sound (std::string file, 
00260                                            double volume, 
00261                                            double throttle_volume_factor, 
00262                                            double engine_speed_volume_factor, 
00263                                            double pitch) = 0;
00264 
00265                 // Set the 3D models.
00266                 virtual void 
00267                 exterior_model (std::string file, double scale,
00268                                 const Vamos_Geometry::Three_Vector& translation,
00269                                 const Vamos_Geometry::Three_Vector& rotation) = 0;
00270                 virtual void 
00271                 interior_model (std::string file, double scale,
00272                                 const Vamos_Geometry::Three_Vector& translation,
00273                                 const Vamos_Geometry::Three_Vector& rotation) = 0;
00274 
00275                 // Set the dashboard.
00276                 virtual void dashboard (Dashboard* dash) = 0;
00277         
00278                 // Advance the car in time by TIME.
00279                 virtual void propagate (double time);
00280         
00281                 // Pan the view.
00282                 void pan (double factor, double time = 0.0);
00283 
00284                 // Change the steering angle to ANGLE with a time constant of TIME.
00285                 void steer (double angle, double time = 0.0);
00286 
00287                 // Change the throttle to FACTOR with a time constant of TIME.
00288                 void gas (double factor, double time = 0.0);
00289 
00290                 // Change the brakes to FACTOR with a time constant of TIME.
00291                 void brake (double factor, double time = 0.0);
00292                 
00293                 // Return the steering angle
00294                 double steer() { return m_steer_key_control.value(); }
00295 
00296                 // Return the gas value
00297                 double gas() { return m_gas_key_control.value(); }
00298 
00299                 // Return the brake value
00300                 double brake() { return m_brake_key_control.value(); }
00301 
00302                 // Return max steering angle
00303                 double max_steer_angle() { return m_max_steer_angle; }
00304                 // Change the brakes to FACTOR with a time constant of TIME.
00305                 void handbrake (double factor, double time = 0.0);
00306 
00307                 // Shift to the next lower gear.  The chosen gear is returned.
00308                 int shift_down ();
00309 
00310                 // Shift to the next higher gear.  The chosen gear is returned.
00311                 int shift_up ();
00312 
00313                 // Shift to GEAR.  The chosen gear is returned.
00314                 int shift (int gear);
00315 
00316                 void clutch (double factor, double time = 0.0);
00317 
00318                 void engage_clutch (double time);
00319                 void disengage_clutch (double time);
00320                 void start_engine ();
00321         
00322                 // Set the front brake bias to BIAS.
00323                 void brake_bias (double bias);
00324 
00325                 // Set the largest possible steering angle.
00326                 void max_steer_angle (double degree_angle) 
00327                 { m_max_steer_angle = degree_angle; }
00328 
00329                 // Set the steering non-linearity.
00330                 void steer_exponent (double exponent) { m_steer_exponent = exponent; }
00331                 
00332                 // Set the amount of decrease in sensitivity with speed.
00333                 void steer_speed_sensitivity (double sensitivity)
00334                 { m_steer_speed_sensitivity = sensitivity; }
00335 
00336                 // Set the amount of time for gear changes.
00337                 void shift_delay (double time) { m_shift_delay = time; }
00338 
00339                 // Set the amount of time for operating the clutch.  The first
00340                 // time is for shifting from nuetral.
00341                 void clutch_time (double from_neutral, double others);
00342 
00343                 // Return the sum of the sliding speeds of the tires.
00344                 double slide () const { return m_slide; }
00345 
00346                 // Return the pointer to the WHEEL_INDEXth wheel.
00347                 Wheel* wheel (int wheel_index) const;
00348 
00349                 // Return the pointer to the front-most particle.
00350                 Particle* front_particle () const { return mp_front_particle; }
00351 
00352                 // Return a pointer to the engine.
00353                 Engine* engine () { return mp_drivetrain->engine (); }
00354 
00355                 // Return a pointer to the transmission.
00356                 Transmission* transmission () { return mp_drivetrain->transmission (); }
00357 
00358                 // Return a pointer to the fuel tank.
00359                 Fuel_Tank* fuel_tank () { return mp_fuel_tank; }
00360 
00361                 // Return the most recent gear selected.  The shift may not have
00362                 // occurred yet due to the shift delay specified in the call to
00363                 // `shift_up ()', `shift_down ()', or `shift ()'.
00364                 int gear () const { return m_new_gear; }
00365 
00366                 // Retrun the previous gear selected.
00367                 int last_gear () const { return m_last_gear; }
00368 
00369                 //** Enhance Body's reset methods by re-initializing the engine
00370                 // and gearbox.
00371 
00372                 // Restore the initial conditions.
00373                 void reset ();
00374 
00375                 // Restore the initial conditions and then set the position to
00376                 // POSITION and the orientation to ORIENTATION.
00377                 void reset (const Vamos_Geometry::Three_Vector& position, 
00378                             const Vamos_Geometry::Three_Matrix& orientation);
00379 
00380                 // Return the total distance traveled since the start of the
00381                 // simulation.
00382                 double distance_traveled () const { return m_distance_traveled; }
00383 
00384                 void drivetrain (Drivetrain* drive);
00385 
00386                 // Set the position of the driver's eyes relative to the origin.
00387                 void view_position (const Vamos_Geometry::Three_Vector& driver_view) 
00388                 { m_driver_view = driver_view; }
00389 
00390                 // Set the driver's field of view in degrees.
00391                 void field_of_view (double field) { m_field_of_view = field; }
00392 
00393                 virtual void set_view (const Vamos_Geometry::Three_Vector& position,
00394                                        double field_of_view,
00395                                        double near_plane, double far_plane,
00396                                        double pan_angle) {};
00397 
00398                 virtual void set_perspective (double aspect) {};
00399 
00400                 // Add a rearview mirror.
00401                 virtual void add_rear_view (const Vamos_Geometry::Three_Vector& position,
00402                                             double width, double height,
00403                                             double direction, double field,
00404                                             double near_plane, double far_plane,
00405                                             std::string mask_file) {};
00406 
00407                 // Return the driver's field of view in degrees.
00408                 double field_of_view () const { return m_field_of_view; }
00409 
00410                 // Return the current pan angle.
00411                 double pan () const { return m_pan_key_control.value (); }
00412 
00413                 // Return the position of the viewpont.
00414                 Vamos_Geometry::Three_Vector view_position () const;
00415 
00416                 //* Methods to be defined in derived classes.
00417 
00418                 // Render the car according to its current position and
00419                 // orientation.
00420                 virtual TEXTURE_HANDLE * shadow_texture() {cerr << "ERROR:  virtual call to shadow_texture!" << endl; return NULL;}
00421                 virtual void draw (bool transform) {};
00422                 virtual void draw_interior () {};
00423                 virtual void draw_rear_view (double aspect, int index) {};
00424                 virtual void make_rear_view_mask (int window_width, 
00425                                                   int window_height) {};
00426                 virtual int get_n_mirrors () const { return 0; }
00427 
00428                 // Perform the transformations for the driver's view.
00429                 virtual void view (double pan, 
00430                                    const Vamos_Geometry::Three_Vector& view_position) {};
00431                 virtual void view (double pan) {};
00432 
00433                 // Return the PLIB sound object for the engine.  Can't be const.
00434                 virtual Vamos_Geometry::Sample* engine_sound () = 0;
00435 
00436                 // Return the sound parameters.
00437                 virtual double engine_pitch () { return 0.0; }
00438                 virtual double engine_volume () { return 0.0; }
00439         
00440                 virtual int GetSoundSource() {return -12337;}
00441                 virtual int GetTireSoundSource(int i) {return -1337;}
00442 
00443                 // Return true if there is no shift delay.
00444                 bool fast_shift () const { return m_shift_delay <= 0.0; }
00445 
00446                 void show_dashboard_extras (bool show) { m_show_dashboard_extras = show; }
00447 
00448                 // Return true if the position is within the crash box.
00449                 bool collision (const Vamos_Geometry::Three_Vector& position) const;
00450         
00451                 //bool ShiftPending() {return m_shift_pending;}
00452                 bool ShiftPending() {return (m_shift_pending || m_clutch_key_control.target_pending());}
00453         
00454                 void GetState(Vamos_Geometry::Three_Vector &chassispos, 
00455                 Vamos_Geometry::Three_Matrix &chassisorientation,
00456                 Vamos_Geometry::Three_Vector &chassisvel,
00457                 Vamos_Geometry::Three_Vector &chassisangvel,
00458                 double *suspdisp,
00459                 double *suspcompvel,
00460                 Vamos_Geometry::Three_Vector *whlangvel,
00461                 int &gear,
00462                 double &enginespeed,
00463                 double &clutchspeed,
00464                 double &enginedrag,
00465                 double *tirespeed
00466                 );
00467         
00468                 void SetState(Vamos_Geometry::Three_Vector chassispos, 
00469                 Vamos_Geometry::Three_Matrix chassisorientation,
00470                 Vamos_Geometry::Three_Vector chassisvel,
00471                 Vamos_Geometry::Three_Vector chassisangvel,
00472                 double *suspdisp,
00473                 double *suspcompvel,
00474                 Vamos_Geometry::Three_Vector *whlangvel,
00475                 int gear,
00476                 double enginespeed,
00477                 double clutchspeed,
00478                 double enginedrag,
00479                 double * tirespeed
00480                 );
00481                 
00482                 virtual JOEMODEL * GetCollisionModel() {return NULL;}
00483                 
00484                 void SetSector(int newsector) {m_sector = newsector;}
00485                 int GetSector() {return m_sector;}
00486                 void SetColPatch(int i, BEZIER * newcolpatch) {if (i < 4 && i >= 0) m_colpatches[i] = newcolpatch;}
00487                 BEZIER * GetColPatch(int i) {if (i < 4 && i >= 0) return m_colpatches[i]; else return NULL;}
00488                 //void SetColFriction(int i, float f1, float f2) {if (i < 4 && i >= 0) {m_colfriction1[i] = f1;m_colfriction2[i] = f2;}}
00489                 void SetColParams(int i, double f1, double f2, double rr, double rd);
00490         };
00491 
00492         class Facade;
00493         class Gauge;
00494         class Gear_Indicator;
00495         class Steering_Wheel;
00496 
00497         struct Model_Info
00498         {
00499                 std::string file;
00500                 double scale;
00501                 Vamos_Geometry::Three_Vector translate;
00502                 Vamos_Geometry::Three_Vector rotate;
00503 
00504                 Model_Info (std::string file_in, double scale_in,
00505                             const Vamos_Geometry::Three_Vector& translate_in,
00506                             const Vamos_Geometry::Three_Vector& rotate_in)
00507                   : file (file_in), 
00508                     scale (scale_in), 
00509                     translate (translate_in),
00510                     rotate (rotate_in)
00511                 {};
00512         };
00513 /*
00514         class Car_Reader : public Vamos_Geometry::XML_Parser
00515         {
00516                 void on_start_tag (const Vamos_Geometry::XML_Tag& tag); 
00517                 void on_end_tag (const Vamos_Geometry::XML_Tag& tag); 
00518                 void on_data (std::string data_string);
00519 
00520                 std::string m_tag;
00521                 std::string m_path;
00522 
00523                 std::vector <int> m_ints;
00524                 std::vector <double> m_doubles;
00525                 std::vector <std::string> m_strings;
00526                 std::vector <Vamos_Geometry::Three_Vector> m_vectors;
00527                 std::vector <Vamos_Geometry::Two_Point> m_points;
00528                 std::vector <std::pair <int, double> > m_gears;
00529                 std::vector <bool> m_bools;
00530 
00531                 std::vector <double> m_long_parameters;
00532                 std::vector <double> m_trans_parameters;
00533                 std::vector <double> m_align_parameters;
00534 
00535                 std::string m_slow_model;
00536                 std::string m_fast_model;
00537                 std::string m_stator_model;
00538                 double m_transition;
00539                 double m_stator_offset;
00540                 double m_scale;
00541                 Vamos_Geometry::Three_Vector m_translation;
00542                 Vamos_Geometry::Three_Vector m_rotation;
00543                 std::vector <Model_Info*> m_models;
00544                 bool m_first_model_for_this_wheel;
00545 
00546                 std::string m_data_dir;
00547                 Car* mp_car;
00548                 Engine* mp_engine;
00549                 Clutch* mp_clutch;
00550                 Transmission* mp_transmission;
00551                 Differential* mp_differential;
00552 
00553                 std::vector <Facade*> ma_mirrors;
00554                 Gauge* mp_tachometer;
00555                 Gauge* mp_speedometer;
00556                 Gauge* mp_fuel_gauge;
00557                 Gear_Indicator* mp_gear_indicator;
00558                 Steering_Wheel* mp_steering_wheel;
00559 
00560                 std::string m_tachometer_type;
00561                 std::string m_speedometer_type;
00562                 std::string m_fuel_gauge_type;
00563 
00564         public:
00565                 Car_Reader (std::string data_dir, 
00566                             std::string car_file, 
00567                             Car* car);
00568                 ~Car_Reader ();
00569         };
00570 */
00571 }
00572 
00573 #endif // not _CAR_H_

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