Enki  1.9
PhysicalEngine.h
Go to the documentation of this file.
1 /*
2  Enki - a fast 2D robot simulator
3  Copyright (C) 1999-2016 Stephane Magnenat <stephane at magnenat dot net>
4  Copyright (C) 2004-2005 Markus Waibel <markus dot waibel at epfl dot ch>
5  Copyright (c) 2004-2005 Antoine Beyeler <abeyeler at ab-ware dot com>
6  Copyright (C) 2005-2006 Laboratory of Intelligent Systems, EPFL, Lausanne
7  Copyright (C) 2006-2008 Laboratory of Robotics Systems, EPFL, Lausanne
8  See AUTHORS for details
9 
10  This program is free software; the authors of any publication
11  arising from research using this software are asked to add the
12  following reference:
13  Enki - a fast 2D robot simulator
14  http://home.gna.org/enki
15  Stephane Magnenat <stephane at magnenat dot net>,
16  Markus Waibel <markus dot waibel at epfl dot ch>
17  Laboratory of Intelligent Systems, EPFL, Lausanne.
18 
19  You can redistribute this program and/or modify
20  it under the terms of the GNU General Public License as published by
21  the Free Software Foundation; either version 2 of the License, or
22  (at your option) any later version.
23 
24  This program is distributed in the hope that it will be useful,
25  but WITHOUT ANY WARRANTY; without even the implied warranty of
26  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  GNU General Public License for more details.
28 
29  You should have received a copy of the GNU General Public License
30  along with this program; if not, write to the Free Software
31  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 */
33 
34 #ifndef __ENKI_PHYSICALENGINE_H
35 #define __ENKI_PHYSICALENGINE_H
36 
37 #include "Geometry.h"
38 #include "Types.h"
39 #include "Random.h"
40 #include "Interaction.h"
41 #include "BluetoothBase.h"
42 #include <iostream>
43 #include <set>
44 #include <vector>
45 #include <valarray>
46 
47 
124 namespace Enki
126 {
127  class World;
128 
130 
132  {
133  friend class World;
134 
135  public: // inner classes
136 
137  // User data
138 
140  class UserData
141  {
142  public:
144 
145  public:
147  void deleteIfRequired() { if (deletedWithObject) delete this; }
149  virtual ~UserData() {}
150  };
151 
154 
155  // Physics
156 
157  // physical constant
158  static const double g;
159 
160  // physical parameters constants
161 
170 
171  // physics state variables
172 
173  // space coordinates
174 
178  double angle;
179 
180  // space coordinates derivatives
181 
185  double angSpeed;
186 
187  // Geometry
188 
190  class Part
191  {
192  public:
194  Part(const Polygone& shape, double height);
196  Part(const Polygone& shape, double height, const Textures& textures);
198  Part(double l1, double l2, double height);
199 
201  void applyTransformation(const Matrix22& rot, const Point& trans, double* radius);
202 
203  // getters
204  inline double getHeight() const { return height; }
205  inline double getArea() const { return area; }
206  inline const Polygone& getShape() const { return shape; }
207  inline const Polygone& getTransformedShape() const { return transformedShape; }
208  inline const Point& getCentroid() const { return centroid; }
209  inline const Point& getTransformedCentroid() const { return transformedCentroid; }
210  inline const Textures& getTextures() const { return textures; }
211  inline bool isTextured() const { return !textures.empty(); }
212 
213  private:
214  friend class PhysicalObject;
215  // geometrical properties
216 
218  double height;
220  double area;
229 
230  // visual properties
231 
234 
235  private:
237  void computeAreaAndCentroid();
239  void computeTransformedShape(const Matrix22& rot, const Point& trans);
240  };
241 
243  struct Hull:std::vector<Part>
244  {
246  Hull() {}
248  Hull(const Part& part) : std::vector<Part>(1, part) {}
250  Polygone getConvexHull() const;
252  Hull operator+(const Hull& that) const;
254  Hull& operator+=(const Hull& that);
256  void applyTransformation(const Matrix22& rot, const Point& trans, double* radius = 0);
257  };
258 
259  private: // variables
260 
261  // Physics
262 
265 
268 
269  // mass and inertia tensor
270 
272  double mass;
275 
276  // Geometry
277 
281  double r;
283  double height;
286 
287  public: // methods
288 
290  PhysicalObject();
292  virtual ~PhysicalObject();
293 
294  // getters
295 
296  inline double getRadius() const { return r; }
297  inline double getHeight() const { return height; }
298  inline bool isCylindric() const { return hull.empty(); }
299  inline const Hull& getHull() const { return hull; }
300  inline const Color& getColor() const { return color; }
301  inline double getMass() const { return mass; }
302  inline double getMomentOfInertia() const { return momentOfInertia; }
303  inline double getInterlacedDistance() const { return interlacedDistance; }
304 
305  // setters
306 
308  void setCylindric(double radius, double height, double mass);
310  void setRectangular(double l1, double l2, double height, double mass);
312  void setCustomHull(const Hull& hull, double mass);
314  void setColor(const Color &color);
315 
316  enum ButtonCode
317  {
318  LEFT_MOUSE_BUTTON = 1<<0,
319  RIGHT_MOUSE_BUTTON = 1<<1,
320  MIDDLE_MOUSE_BUTTON = 1<<3
321  };
323  virtual void clickedInteraction(bool pressed, unsigned int buttonCode, double pointX, double pointY, double pointZ){};
324 
325  private: // setup methods
326 
328  void dirtyUserData();
330  void computeMomentOfInertia();
332  void setupCenterOfMass();
335 
336  protected: // physical actions
337 
338  /*//! A physics simulation step for this object. It is considered as deinterlaced. The position and orientation are updated.
339  virtual void physicsStep(double dt);*/
341  virtual void controlStep(double dt);
343  virtual void applyForces(double dt);
344 
346  virtual void collisionEvent(PhysicalObject *o) {}
347 
349  virtual void initLocalInteractions(double dt, World* w) { }
351  virtual void doLocalInteractions(double dt, World *w, PhysicalObject *o) { }
353  virtual void doLocalWallsInteraction(double dt, World* w) { }
355  virtual void finalizeLocalInteractions(double dt, World* w) { }
356 
358  virtual void initGlobalInteractions(double dt, World* w) { }
360  virtual void doGlobalInteractions(double dt, World* w) { }
362  virtual void finalizeGlobalInteractions(double dt, World* w) { }
363 
364  private: // physical actions
365 
367  void initPhysicsInteractions(double dt);
369  void finalizePhysicsInteractions(double dt);
370 
372  void collideWithStaticObject(const Vector &n, const Point &cp);
374  void collideWithObject(PhysicalObject &that, const Point &cp, const Vector &dist);
375  };
376 
378 
379  class Robot: public PhysicalObject
380  {
381  protected:
383  std::vector<LocalInteraction *> localInteractions;
385  std::vector<GlobalInteraction *> globalInteractions;
386 
387  public:
389  void addLocalInteraction(LocalInteraction *li);
391  void addGlobalInteraction(GlobalInteraction *gi) {globalInteractions.push_back(gi);}
393  virtual void initLocalInteractions(double dt, World* w);
395  virtual void doLocalInteractions(double dt, World *w, PhysicalObject *po);
397  virtual void doLocalWallsInteraction(double dt, World* w);
399  virtual void finalizeLocalInteractions(double dt, World* w);
400 
402  virtual void doGlobalInteractions(double dt, World* w);
404  void sortLocalInteractions(void);
405  };
406 
408 
411  class World
412  {
413  public:
416  {
417  WALLS_SQUARE = 0,
419  WALLS_NONE
420  };
421 
425  const double w;
427  const double h;
429  const double r;
430  /* Texture of world walls is disabled now, re-enable a proper support if required
432  Texture wallTextures[4];*/
434  const Color color;
435 
438  {
440  unsigned width;
442  unsigned height;
444  std::vector<uint32_t> data;
445 
447  GroundTexture();
449  GroundTexture(unsigned width, unsigned height, const uint32_t* data);
450  };
451 
454 
455  typedef std::set<PhysicalObject *> Objects;
456  typedef Objects::iterator ObjectsIterator;
457 
459  Objects objects;
462 
463  protected:
465  void collideCircleWithShape(PhysicalObject *circularObject, PhysicalObject *shapedObject, const Polygone &shape);
467  void collideObjects(PhysicalObject *object1, PhysicalObject *object2);
469  void collideWithSquareWalls(PhysicalObject *object);
471  void collideWithCircularWalls(PhysicalObject *object);
473  bool isPointInside(const Point &p, const Point &c, const Polygone &bs, Vector *distVector);
474 
475  public:
477  World(double width, double height, const Color& wallsColor = Color::gray, const GroundTexture& groundTexture = GroundTexture());
479  World(double r, const Color& wallsColor = Color::gray, const GroundTexture& groundTexture = GroundTexture());
481  World();
483  virtual ~World();
484 
486  bool hasGroundTexture() const;
488  Color getGroundColor(const Point& p) const;
489 
491  virtual void step(double dt, unsigned physicsOversampling = 1);
494  void addObject(PhysicalObject *o);
496  void removeObject(PhysicalObject *o);
498  void disconnectExternalObjectsUserData();
499 
501  void setRandomSeed(unsigned long seed);
503  void initBluetoothBase();
505  BluetoothBase* getBluetoothBase();
506 
508  static void takeObjectOwnership(bool doTake);
509 
510  protected:
512  virtual void controlStep(double dt) { }
513  };
514 
516  extern FastRandom random;
517 }
518 
519 #endif
const double r
The radius of the world, if wallsType is WALLS_CIRCLE.
Definition: PhysicalEngine.h:429
A vector in a 2D space.
Definition: Geometry.h:68
virtual void initGlobalInteractions(double dt, World *w)
Initialize the global interactions, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:358
double r
The radius of circular objects or, if hull is not empty, the bounding circle.
Definition: PhysicalEngine.h:281
WallsType
Type of walls around the world.
Definition: PhysicalEngine.h:415
virtual void doLocalWallsInteraction(double dt, World *w)
Do the interactions with the walls of world w, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:353
PhysicalObject()
Constructor.
Definition: PhysicalEngine.cpp:228
double interlacedDistance
How much this object did penetrate other objects in the course of physics steps since last control st...
Definition: PhysicalEngine.h:267
A 2x2 matrix.
Definition: Geometry.h:142
Header of the bluetooth base.
Basic useful types.
const double h
The height of the world, if wallsType is WALLS_SQUARE.
Definition: PhysicalEngine.h:427
User specific data that can be attached to any object in the world.
Definition: PhysicalEngine.h:140
double area
The area of this part.
Definition: PhysicalEngine.h:220
double dryFrictionCoefficient
The dry friction coefficient mu.
Definition: PhysicalEngine.h:165
Implementation of a Bluetooth base coordinating the Bluetooth modules.
Definition: BluetoothBase.h:54
void collideWithStaticObject(const Vector &n, const Point &cp)
Dynamics for collision with a static object at points cp with normal vector n.
Definition: PhysicalEngine.cpp:522
A part is one of the convex geometrical element that composes the physical object.
Definition: PhysicalEngine.h:190
Hull(const Part &part)
Construct a hull with a single part.
Definition: PhysicalEngine.h:248
A hull is a vector of Hull.
Definition: PhysicalEngine.h:243
double viscousFrictionCoefficient
The viscous friction coefficient. Premultiplied by mass. A value of k applies a force of -k * speed *...
Definition: PhysicalEngine.h:167
void collideWithObject(PhysicalObject &that, const Point &cp, const Vector &dist)
Dynamics for collision with that at point cp with a penetrated distance of dist.
Definition: PhysicalEngine.cpp:544
virtual void doGlobalInteractions(double dt, World *w)
Do the global interactions with the world, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:360
virtual void initLocalInteractions(double dt, World *w)
Initialize the object specific interactions, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:349
double collisionElasticity
Elasticity of collisions of this object. If 0, soft collision, 100% energy dissipation; if 1...
Definition: PhysicalEngine.h:163
const WallsType wallsType
type of walls this world is using
Definition: PhysicalEngine.h:423
virtual void doLocalInteractions(double dt, World *w, PhysicalObject *o)
Do the interactions with the other PhysicalObject, do nothing for PhysicalObject. ...
Definition: PhysicalEngine.h:351
virtual void clickedInteraction(bool pressed, unsigned int buttonCode, double pointX, double pointY, double pointZ)
called for robot if a click is performed on it
Definition: PhysicalEngine.h:323
Hull()
Construct an empty hull.
Definition: PhysicalEngine.h:246
virtual void finalizeLocalInteractions(double dt, World *w)
All interactions are finished, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:355
A situated object in the world with mass, geometry properties, physical properties, ...
Definition: PhysicalEngine.h:131
const double w
The width of the world, if wallsType is WALLS_SQUARE.
Definition: PhysicalEngine.h:425
Polygone shape
The shape of the part in object coordinates.
Definition: PhysicalEngine.h:222
A fast random generator.
Definition: Random.h:48
void setCylindric(double radius, double height, double mass)
Make the object cylindric with a given mass.
Definition: PhysicalEngine.cpp:259
void addGlobalInteraction(GlobalInteraction *gi)
Add a global interaction, just add it at the end of the vector.
Definition: PhysicalEngine.h:391
static const Color gray
gray (0.5, 0.5, 0.5)
Definition: Types.h:154
Enki is the namespace of the Enki simulator. All Enki functions and classes excepted the math one are...
Definition: BluetoothBase.cpp:44
unsigned height
the height of the ground texture, if any
Definition: PhysicalEngine.h:442
double viscousMomentFrictionCoefficient
The viscous friction moment coefficient. Premultiplied by momentOfInertia. A value of k applies a for...
Definition: PhysicalEngine.h:169
double mass
The mass of the object. If below zero, the object can&#39;t move (infinite mass).
Definition: PhysicalEngine.h:272
std::vector< Texture > Textures
Textures for all sides of an object.
Definition: Types.h:169
2-D Texture for ground
Definition: PhysicalEngine.h:437
BluetoothBase * bluetoothBase
Base for the Bluetooth connections between robots.
Definition: PhysicalEngine.h:461
The interfaces for the interactions.
void setRectangular(double l1, double l2, double height, double mass)
Make the object rectangular of size l1 x l2 with a given mass.
Definition: PhysicalEngine.cpp:277
Textures textures
Texture for several faces of this object.
Definition: PhysicalEngine.h:233
bool deletedWithObject
if true, deleted along with the physical object.
Definition: PhysicalEngine.h:143
void dirtyUserData()
When a physical parameter (color, shape, ...) has been changed, the user data must be updated...
Definition: PhysicalEngine.cpp:250
virtual void finalizeGlobalInteractions(double dt, World *w)
All global interactions are finished, do nothing for PhysicalObject.
Definition: PhysicalEngine.h:362
void computeMomentOfInertia()
Compute the moment of inertia tensor depending on radius, mass, height, and hull, assuming that the h...
Definition: PhysicalEngine.cpp:323
Point centroid
The centroid (barycenter) of the part in object coordinates.
Definition: PhysicalEngine.h:226
Objects objects
All the objects in the world.
Definition: PhysicalEngine.h:459
virtual void controlStep(double dt)
Can implement world specific control. By default do nothing.
Definition: PhysicalEngine.h:512
virtual void collisionEvent(PhysicalObject *o)
The object collided with o during the current physical step, if o is null, it collided with walls...
Definition: PhysicalEngine.h:346
double momentOfInertia
! The moment of inertia tensor
Definition: PhysicalEngine.h:274
std::vector< GlobalInteraction * > globalInteractions
Vector of global interactions.
Definition: PhysicalEngine.h:385
The mathematic classes for random work.
Point pos
The position of the object.
Definition: PhysicalEngine.h:176
The world is the container of all objects and robots.
Definition: PhysicalEngine.h:411
std::vector< LocalInteraction * > localInteractions
Vector of local interactions.
Definition: PhysicalEngine.h:383
Interacts with the whole world.
Definition: Interaction.h:93
double angSpeed
The rotation speed of the object, standard trigonometric orientation.
Definition: PhysicalEngine.h:185
void setColor(const Color &color)
Set the overall color of this object, if hull is empty or if it does not contain any texture...
Definition: PhysicalEngine.cpp:315
void finalizePhysicsInteractions(double dt)
All collisions are finished, deinterlace the object.
Definition: PhysicalEngine.cpp:514
const GroundTexture groundTexture
Current ground texture.
Definition: PhysicalEngine.h:453
UserData * userData
Data attached by the user to this physical object. If non-null, will be destroyed with the object...
Definition: PhysicalEngine.h:153
circle walls, use r for radius
Definition: PhysicalEngine.h:418
void setCustomHull(const Hull &hull, double mass)
Set a custom shape and mass to the object.
Definition: PhysicalEngine.cpp:295
virtual void applyForces(double dt)
Apply forces, typically friction to reduce speed, but one can override to change behaviour.
Definition: PhysicalEngine.cpp:455
virtual void controlStep(double dt)
Control step, not oversampled.
Definition: PhysicalEngine.cpp:450
A color in RGBA.
Definition: Types.h:55
Polygone, which is a vector of points. Anti-clockwise, standard trigonometric orientation.
Definition: Geometry.h:238
std::vector< uint32_t > data
the date of the ground texture, organised as scanlines of pixels in ARGB (0xAARRGGBB in little endian...
Definition: PhysicalEngine.h:444
double height
The height of circular object or, if hull is not empty, the maximum height.
Definition: PhysicalEngine.h:283
void setupCenterOfMass()
Compute the center of mass and move bounding surfaces accordingly. Does not update the moment of iner...
Definition: PhysicalEngine.cpp:355
Hull hull
The hull of this object, which can be composed of several Hull.
Definition: PhysicalEngine.h:279
Interacts with another object or wall only up to a certain distance.
Definition: Interaction.h:53
const Color color
The color of the world walls and ground.
Definition: PhysicalEngine.h:434
void initPhysicsInteractions(double dt)
Initialize the collision logic.
Definition: PhysicalEngine.cpp:501
Polygone transformedShape
The shape of the part in world coordinates, updated on initPhysicsInteractions(). ...
Definition: PhysicalEngine.h:224
void computeTransformedShape()
Compute the hull of this object in world coordinates.
Definition: PhysicalEngine.cpp:407
Color color
The overall color of this object, if hull is empty or if it does not contain any texture.
Definition: PhysicalEngine.h:285
Vector speed
The speed of the object.
Definition: PhysicalEngine.h:183
Vector posBeforeCollision
position before collision, used to compute interlacedDistance
Definition: PhysicalEngine.h:264
FastRandom random
Fast random for use by Enki.
Definition: PhysicalEngine.cpp:49
unsigned width
the width of the ground texture, if any
Definition: PhysicalEngine.h:440
Point transformedCentroid
The centroid (barycenter) of the part in world coordinates, updated on initPhysicsInteractions().
Definition: PhysicalEngine.h:228
virtual ~UserData()
Virtual destructor, call destructor of child classes.
Definition: PhysicalEngine.h:149
A robot is a PhysicalObject that has additional interactions and a controller.
Definition: PhysicalEngine.h:379
The mathematic classes for 2D geometry.
virtual ~PhysicalObject()
Destructor.
Definition: PhysicalEngine.cpp:242
double height
The height of the part, used for interaction with the sensors of other robots.
Definition: PhysicalEngine.h:218
void deleteIfRequired()
Ask the user data to garbage collect itself if appropriate (i.e. not a singleton) ...
Definition: PhysicalEngine.h:147
double angle
The orientation of the object in the world, standard trigonometric orientation.
Definition: PhysicalEngine.h:178