Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

cylinder.h

Go to the documentation of this file.
00001 /*
00002   svas_server -- virtual World Server of Svas
00003   Copyright (c) 2001, 2002 David Moreno Montero
00004  
00005  
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010  
00011   This program is distributed in the hope that it will be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015  
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019   02111-1307, USA.  
00020 
00021 */
00022 
00023 #ifndef __CYLINDER__
00024 #define __CYLINDER__
00025 
00026 #include "vector3d.h"
00027 #include "matrix3x3.h"
00028 #include "quaternion.h"
00029 #include "action.h"
00030 #include <vector>
00031 
00032 class Agent;
00033 class World;
00034 class Collision;
00035 
00036 /** 
00037  * This is the main physical body of the world. The cylinders. Each
00038  * agent is composed of one or more cylinders.
00039  *
00040  * @see Vector3d
00041  */
00042 class Cylinder{
00043  protected:
00044   /**
00045    * @name Cylinder Physical Status
00046    * The physical status of the Cylinder
00047    * @{
00048    */
00049   /// Height of the cylinder (length)
00050   union{
00051     double height;
00052     double length;
00053   };
00054   /// Width of the cylinder (cincunference RADIUS) 
00055   union{
00056     double width;
00057     double radius;
00058   };
00059   /// Inertia tensor
00060   Matrix3x3 inertia;
00061   /// Inertia tensor inversed
00062   Matrix3x3 inertiaInverse;
00063 
00064 
00065   /// position of the cylinder respect to the parent cylinder, in
00066   /// world coordinates, if any, or (0,0,0) if none Indicate the
00067   /// position that the cylinder should have (and that the glue force
00068   /// sets)
00069   Point3d gluePosition;
00070   /// position of the cylinder with respect to the father, if any, in
00071   /// body coordinates in cylindrical "svas-coordinates": 
00072   /// (height, angle,0)
00073   Point3d gluePositionBody;
00074   
00075 
00076   /// position of the cylinder respect to the parent cylinder, in world coordinates, if any, or world if none
00077   Point3d position;
00078   /// Base for the orientation of the cylinder
00079   Quaternion orientationBase;
00080   /// Orientation of the cylinder, in world coordinates
00081   Vector3d orientationWorld;
00082   /// Velocity
00083   Vector3d velocity;
00084   /// Angular Velocity
00085   Vector3d angularVelocity;
00086   /// Force
00087   Vector3d force;
00088   /// Torque
00089   Vector3d moment;
00090 
00091 
00092 
00093   /// Energy of the cylinder
00094   double energy;
00095   /// Mass of the cylinder
00096   double mass;
00097   /** @} */
00098 
00099   /**
00100    * @name Old Physical properties (for undo)
00101    * @{
00102    */
00103   /// position of the cylinder in world coordinates
00104   Point3d oldPosition;
00105   /// Orientation base of the cylinder (maybe should be a quaternion?)
00106   Quaternion oldOrientationBase;
00107   /// Velocity
00108   Vector3d oldVelocity;
00109   /// Angular Velocity
00110   Vector3d oldAngularVelocity;
00111   /// Force
00112   Vector3d oldForce;
00113   /// Torque
00114   Vector3d oldMoment;
00115   /** @} */
00116 
00117 
00118 
00119   /** Pointers to sons */
00120   vector<Cylinder *> son;
00121   /** The father of the cylinder, if it's on the same agent (else NULL) */
00122   Cylinder *father;
00123   /** The agent to which this cylinder belongs */
00124   Agent *agent;
00125   /// The world from which the cylinder cames.
00126   World *world;
00127 
00128   /** The identifier of this cylinder, on this agent */
00129   unsigned char cylinderId;
00130 
00131   /** The action that the cylinder wants to perform in this cycle */
00132   Action desiredAction;
00133 
00134   /**
00135    * @name Change status
00136    * Methods to change the status of the cylinder. 
00137    *
00138    * All them returns the energy cylinders that is generated when
00139    * energy is consummed 
00140    * @{
00141    */
00142   Cylinder *grow(GrowType type, unsigned short int energy);
00143   Cylinder *move(MoveType type, unsigned short int energy);
00144   Cylinder *drop(unsigned char son, const char *creationMessage, unsigned long int messageSize);
00145   Cylinder *createCylinder(double angle, double height, unsigned short int energy);
00146   Cylinder *sendEnergyTo(unsigned char son, unsigned short int energy);
00147   Cylinder *sendMessage(unsigned long int agentId, const char *message, unsigned long int size);
00148   /** @} */
00149 
00150   /// Second part of operator()(unsigned short int);
00151   Cylinder *operator()(unsigned short int, unsigned short int &actual);
00152 
00153  public:
00154   Cylinder(World *_world, Cylinder *_father,
00155        double newEnergy, double newWidth, double newHeight, 
00156        Vector3d newGluePositionBody, bool onAgent);
00157   ~Cylinder();
00158 
00159   /** 
00160    * @name Get Properties
00161    * Methods to get several properties of the cylinder
00162    * @{
00163    */
00164   Point3d getPosition(bool relative=false);
00165   Point3d getGluePosition(bool relative=false);
00166   Point3d getCentreOfGravity(void);
00167 
00168   Vector3d getOrientation(){ return orientationWorld; };
00169   Cylinder *getFather(){ return father; };
00170   Agent *getAgent(){ return agent; };
00171   double  getHeight(){ return height; };
00172   double  getWidth(){ return width; };
00173 
00174   double getEnergy(){ return energy; };
00175   double getMass(bool pending=false);
00176 
00177   unsigned char getId(){ return cylinderId; };
00178 
00179   /// Search by id
00180   Cylinder *operator[](unsigned short int);
00181   /// Search by number
00182   Cylinder *operator()(unsigned short int n){ unsigned short int a=0; return (*this)(n,a); }
00183   unsigned short int getNumCylinders();
00184   /// Returns the logical distance between two cylinders (-1 if cyl is not a son)
00185   signed short int dist(Cylinder *cyl);
00186   World *getWorld();
00187   /// Updates the moment of inertia of the cylinder.
00188   void updateMomentOfInertia();
00189   /** @} */
00190 
00191   /// Changes parent agent of the cylinder (and it's sons)
00192   void setNewAgent(Agent *);
00193 
00194   /// Returns a energy cylinder of desired energy (that's energy+mass)
00195   Cylinder *dropEnergy(double energyMass);
00196   /// New intention of the cylinder for this turn
00197   void newIntention(const Action &a){ desiredAction=a; };
00198   /// A new cycle begins, we should reset forces and moments
00199   void newCycle();
00200   
00201   /// Really performs desired actions
00202   Cylinder *performAction();
00203 
00204   /**
00205    * @name Physics 
00206    * @{
00207    */
00208   /// Apply a force over the cylinder, from a world coordinate, it
00209   /// calculates the position of the body
00210   void applyForce(const Vector3d &force, const Point3d &where);
00211   /// Apply a force over the cylinder, on a given body position
00212   void applyForceOnBody(const Vector3d &force, const Point3d &where);
00213   /// Move the cylinders
00214   void applyVelocity(double dtime);
00215   /// Undo last velocity (only applicable once)
00216   void undoVelocity(void);
00217   /// sets the cylinder to the glue position
00218   void setToGluePosition(void);
00219   /// moves the position of the cylinder, but with care about father
00220   void movePosition(const Vector3d &ammount);
00221   /** @} */
00222 
00223 
00224   /**
00225    * @name Collission checkers
00226    *
00227    * Several checks about colissions
00228    * @{
00229    */
00230   /// Checks all collisions
00231   vector<Collision *> checkCollisions();
00232   /// Check collisions against ground
00233   Collision *checkGroundCollision(void);
00234   /// Check collisions against other cylinder
00235   Collision *checkCylinderCollision(Cylinder *other);
00236   /** @} */
00237 
00238   friend class Collision;
00239 
00240 };
00241 
00242 #endif

Generated on Mon Jun 17 19:53:44 2002 for Svas Server by doxygen1.2.16