src/object/physics.h

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Abstract class used for physical object (object with a size, mass,
00020  * etc.). This object can have differents state : ready, is moving, or ghost
00021  * (is outside of the world).
00022  *
00023  * You can : make the object move (with collision test), change state, etc.
00024  * If the object go outside of the world, it become a ghost.
00025  *****************************************************************************/
00026 
00027 #ifndef PHYSICS_H
00028 #define PHYSICS_H
00029 
00030 #include "../include/base.h"
00031 #include "../tool/euler_vector.h"
00032 #include "../tool/point.h"
00033 #include "object_cfg.h"
00034 
00035 typedef enum
00036 {
00037   NoMotion,
00038   FreeFall,
00039   Pendulum,
00040 } MotionType_t;
00041 
00042 class GameLoop;
00043 
00044 class Physics : private ObjectConfig
00045 {
00046 private:
00047   MotionType_t m_motion_type ;
00048   EulerVector m_pos_x;          // x0 = pos, x1 = speed, x2 = acc on the X axys
00049   EulerVector m_pos_y;          // x0 = pos, x1 = speed, x2 = acc on the Y axys
00050 
00051 protected:
00052   Point2d m_extern_force;  // External strength applyed to the object
00053   uint m_last_move;             // Time since last move
00054   double m_phys_width, m_phys_height;
00055 
00056   Point2d m_fix_point_gnd;   // Rope fixation point to the ground.
00057   Point2d m_fix_point_dxy;   // Rope delta to fixation point to the object
00058   EulerVector m_rope_angle;       // Rope angle.
00059   EulerVector m_rope_length;      // Rope length.
00060   double m_rope_elasticity;       // The smallest, the more elastic.
00061   double m_elasticity_damping;    // 0 means perpetual motion.
00062   double m_balancing_damping;     // 0 means perpetual balancing.
00063 
00064   // Define if the rope is elastic or not.
00065   bool m_elasticity_off ;
00066 
00067   // Other physics constants stored there :
00068   ObjectConfig m_cfg;
00069 public:
00070   Physics ();
00071   virtual ~Physics ();
00072 
00073   // Set/Get position
00074   void SetPhysXY (double x, double y);
00075   void SetPhysXY(const Point2d &position);
00076 
00077   double GetPhysX() const;
00078   double GetPhysY() const;
00079   Point2d GetPos() const;
00080 
00081   // Set size
00082   void SetPhysSize (double width, double height);
00083 
00084   void SetMass (double mass);
00085   const double GetMass() const { return m_mass; }
00086 
00087   void SetWindFactor (double wind_factor);
00088   const double GetWindFactor () const { return m_wind_factor; }
00089 
00090   void SetAirResistFactor (double factor);
00091   const double GetAirResistFactor () const{ return m_air_resist_factor; }
00092 
00093   void SetGravityFactor (double factor);
00094   const double GetGravityFactor () const { return m_gravity_factor; }
00095 
00096   void SetRebounding (bool rebounding) { m_rebounding = rebounding; }
00097   const bool GetRebounding () const { return m_rebounding; }
00098 
00099   // Reset the physics constant to the default values in the cfg
00100   void ResetConstants();
00101 
00102   // Set initial speed.
00103   void SetSpeed (double norme, double angle);
00104   void SetSpeedXY (Point2d vector);
00105 
00106   // Add a initial speed to the current speed.
00107   void AddSpeed (double norme, double angle);
00108   void AddSpeedXY (Point2d vector);
00109 
00110   // Get current object speed
00111   void GetSpeed (double &norm, double &angle) const;
00112   void GetSpeedXY (Point2d &vector) const;
00113   Point2d GetSpeed() const;
00114   double GetAngularSpeed() const;
00115   double GetSpeedAngle() const;
00116 
00117   // Add new strength
00118   void SetExternForce (double length, double angle);
00119   void SetExternForceXY (Point2d vector);
00120 
00121   // Add / Remove a fixation point.
00122   void SetPhysFixationPointXY(double g_x, double g_y,
00123                               double dx, double dy) ;
00124   void UnsetPhysFixationPoint() ;
00125   void ChangePhysRopeSize(double dl) ;
00126   double GetRopeAngle() ;
00127   double GetRopeLength();
00128 
00129   // Physical engine : update position (and state) with current time
00130   void RunPhysicalEngine();
00131 
00132   // Notify the son class that the object has moved.
00133   virtual void NotifyMove(Point2d oldPos, Point2d newPos) = 0 ;
00134 
00135   // Start moving
00136   void StartMoving();
00137 
00138   // Stop moving
00139   void StopMoving();
00140 
00141   // The object is moving ?
00142   bool IsMoving() const;
00143 
00144   // The object is falling ?
00145   bool IsFalling() const;
00146 
00147 protected:
00148   // Compute current (x,y) position
00149   Point2d ComputeNextXY(double delta_t);
00150 
00151   virtual void SignalDeath();
00152   virtual void SignalGhostState (bool was_already_dead);
00153   virtual void SignalDrowning();
00154   virtual void SignalRebound();
00155 
00156   // Make the object rebound
00157   void Rebound(Point2d contactPos, double contact_angle);
00158 private:
00159 
00160   void ComputeFallNextXY (double delta_t);
00161 
00162   void ComputePendulumNextXY (double delta_t);
00163 };
00164 
00165 #endif

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7