00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef __PHYSICALENGINE_H
00033 #define __PHYSICALENGINE_H
00034
00035 #include <iostream>
00036 #include <vector>
00037 #include <valarray>
00038 #include <an/Types.h>
00039 #include <an/Geometry.h>
00040 #include <an/Random.h>
00041 #include "Interaction.h"
00042
00114
00115 namespace Enki
00116 {
00117 class World;
00118
00120
00121 class PhysicalObject
00122 {
00123 public:
00125 An::Point pos;
00127 double height;
00129 double angle;
00131 An::Vector speed;
00133 double angSpeed;
00135 double mass;
00137 double staticFrictionThreshold;
00139 double viscousFrictionTau;
00141 double viscousMomentFrictionTau;
00143 double collisionAngularFrictionFactor;
00145 const An::Polygone *boundingSurface;
00147 An::Polygone absBoundingSurface;
00149 double r;
00151 An::Color color;
00153 std::valarray<An::Texture> textures;
00154
00155 protected:
00157 An::Vector deinterlaceVector;
00158
00160 void collideWithStaticObject(const An::Vector &n);
00162 void computeAbsBoundingSurface(void);
00163
00164 public:
00166 PhysicalObject();
00168 virtual ~PhysicalObject();
00169
00171 void setBoundingSurface(const An::Polygone *bs);
00173 const An::Polygone &getTrueBoundingSurface(void) const { return absBoundingSurface; }
00174
00175
00177 virtual void step(double dt);
00178
00180 virtual void initLocalInteractions();
00182 virtual void doLocalInteractions(World *w, PhysicalObject *o, double dt, bool firstInteraction);
00184 virtual void doLocalWallsInteraction(World *w);
00186 virtual void finalizeLocalInteractions(double dt);
00187
00189 virtual void initGlobalInteractions() { }
00191 virtual void doGlobalInteractions(World *w, double dt) { }
00193 virtual void finalizeGlobalInteractions() { }
00194
00196 void collideWithStaticObject(const An::Point &cp1, const An::Point &cp2, const An::Vector &n1, const An::Vector &n2, const An::Vector &dist);
00198 void collideWithObject(PhysicalObject &object, const An::Point &cp, const An::Vector &dist);
00199
00200 };
00201
00203
00204 class Robot: public PhysicalObject
00205 {
00206 protected:
00208 std::vector<LocalInteraction *> localInteractions;
00210 std::vector<GlobalInteraction *> globalInteractions;
00211
00212 public:
00214 Robot();
00215
00217 void addLocalInteraction(LocalInteraction *li);
00219 void addGlobalInteraction(GlobalInteraction *gi) {globalInteractions.push_back(gi);}
00221 virtual void initLocalInteractions();
00223 virtual void doLocalInteractions(World *w, PhysicalObject *po, double dt, bool firstInteraction);
00225 virtual void doLocalWallsInteraction(World *w);
00227 virtual void finalizeLocalInteractions(double dt);
00228
00230 virtual void doGlobalInteractions(World *w, double dt);
00232 void sortLocalInteractions(void);
00233 };
00234
00236
00239 class World
00240 {
00241 protected:
00243 bool collideEven;
00244
00245 public:
00247 std::vector<PhysicalObject *> objects;
00249 bool useWalls;
00251 const double w;
00253 const double h;
00255 An::Texture wallTextures[4];
00256
00258 bool getCollideEven() {return collideEven;}
00260 void collideCircleWithBS(PhysicalObject *circle, PhysicalObject *objectBS, const An::Polygone &bs);
00262 void collideObjects(PhysicalObject *object1, PhysicalObject *object2);
00264 void collideWithWalls(PhysicalObject *object);
00266 bool isPointInside(const An::Point &p, const An::Point &c, const An::Polygone &bs, An::Vector *distVector);
00267
00268 public:
00270 World(double width, double height);
00272 ~World();
00274 void step(double dt);
00276 void addObject(PhysicalObject *o);
00278 void removeObject(PhysicalObject *o);
00280 void setRandomSeed(unsigned long seed);
00281 };
00282
00284 extern An::FastRandom random;
00285 }
00286
00287 #endif