Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10013 in orxonox.OLD for trunk/src/lib/collision_reaction


Ignore:
Timestamp:
Dec 4, 2006, 6:42:46 PM (18 years ago)
Author:
patrick
Message:

merged the collision reaction branche back to trunk

Location:
trunk/src/lib/collision_reaction
Files:
2 deleted
16 edited
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/collision_reaction/Makefile.am

    r9235 r10013  
    55
    66libORXcr_a_SOURCES = cr_engine.cc \
     7                     collision_tube.cc \
    78                     collision.cc \
    89                     collision_event.cc \
    9                      collision_handle.cc \
     10                     collision_filter.cc \
    1011                     collision_reaction.cc \
    1112                     cr_object_damage.cc \
     
    1617
    1718noinst_HEADERS =     cr_engine.h \
     19                     collision_tube.h \
    1820                     collision.h \
    1921                     collision_event.h \
    20                      collision_handle.h \
     22                     collision_filter.h \
    2123                     cr_defs.h \
    2224                     collision_reaction.h \
  • trunk/src/lib/collision_reaction/collision.cc

    r9869 r10013  
    1919#include "debug.h"
    2020
    21 /**
    22  *  standard constructor
    23  */
    24 Collision::Collision ()
     21namespace CoRe
    2522{
    26   this->flushCollisionEvents();
    27 }
     23
     24  /**
     25   *  standard constructor
     26   */
     27  Collision::Collision ()
     28  {
     29    this->reset();
     30  }
    2831
    2932
    30 /**
    31  *  standard deconstructor
    32  */
    33 Collision::~Collision ()
    34 {
    35 }
     33  /**
     34   *  standard deconstructor
     35   */
     36  Collision::~Collision ()
     37  {}
    3638
    3739
    38 /**
    39  * flushes the CollisionEvent list
    40  */
    41 void Collision::flushCollisionEvents()
    42 {
    43   this->entityA = NULL;
    44   this->entityB = NULL;
     40  /**
     41   * resets all variable states to initial values
     42   */
     43  void Collision::reset()
     44  {
     45    this->_entityA = NULL;
     46    this->_entityB = NULL;
    4547
    46   this->entityACollide = false;
    47   this->entityBCollide = false;
     48    this->_entityACollide = false;
     49    this->_entityBCollide = false;
    4850
    49   this->bDispatched = true;
     51    this->_collisionEvents.clear();
     52  }
    5053
    51   this->collisionEvents.clear();
    5254}
    53 
  • trunk/src/lib/collision_reaction/collision.h

    r9869 r10013  
    11/*!
    22 * @file collision.h
    3  *  Definition of a collision as a two WE hit each other
     3 *  Definition of a collision relation of two WorldEntities
    44 *
    5  *  A is shared between two WorldEntity's CollisionHandles if both are subscribed to this event. In this case only one
     5 *  Is shared between two WorldEntity's CollisionHandles if both are subscribed to this event. In this case only one
    66 *  of the two CollisionHandles will calculate the CollisionReaction and the bDispatched flag will be set afterwards
    77 *  to signal that it's already cared about and should be ignored.
     8 *
     9 *  The collisions itself are saved in this container (CollisionEvent). Since there can be multiple collision events
     10 *  for one collision. Imagine: two objects are intersecting (this throws a Collision): many collision boxes will fire
     11 *  each of this boxes will "create" a CollisionEvent.
    812 */
    913
     
    1418#include <vector>
    1519
     20
     21
    1622class WorldEntity;
    1723class BoundingVolume;
    18 class CollisionEvent;
    1924
    20 //! A class representing a simple collision
    21 class Collision
     25namespace CoRe
    2226{
     27
     28  class CollisionEvent;
     29
     30  //! A class representing a simple collision
     31  class Collision
     32  {
     33
     34    typedef std::vector<CollisionEvent*>                  CollisionVector;  //!< the vector containing all collision events
     35    typedef std::vector<CollisionEvent*>::iterator        Iterator;         //!< iterator definition
     36    typedef std::vector<CollisionEvent*>::const_iterator  ConstIterator;   //!< constant iterator definition
     37
     38
    2339  public:
     40
    2441    Collision();
    2542    virtual ~Collision();
    2643
    2744    /** collides two WorldEntities @param entityA world entity A, @param entityB world entity B, @param bvA volume A @param bvB volumeB */
    28     inline void collide(WorldEntity* entityA, WorldEntity* entityB) { this->entityA = entityA; this->entityB = entityB; this->bDispatched = false; }
     45    inline void collide(WorldEntity* entityA, WorldEntity* entityB) { this->_entityA = entityA; this->_entityB = entityB; }
    2946
    3047
    31     /** @return Collision WorldEntity A */
    32     inline WorldEntity* getEntityA() const { return this->entityA; }
    33     /** @return Collision WorldEntity B */
    34     inline WorldEntity* getEntityB() const { return this->entityB; }
    35     /** @return true if Entity A collides */
    36     inline bool isEntityACollide() const { return this->entityACollide; }
    37     /** sets the flag if it reacts @param flag true if it should react on entityA*/
    38     inline void setEntityACollide(bool flag) { this->entityACollide = flag; }
    39     /** @return true if Entity B collides */
    40     inline bool isEntityBCollide() const { return this->entityBCollide; }
    41     /** sets the flag if it reacts @param flag true if it should react on entityB*/
    42     inline void setEntityBCollide(bool flag) { this->entityACollide = flag; }
     48    /* list stuff */
     49  public:
     50
     51    /** registers a @param event CollisionEvent to take place */
     52    inline void registerCollisionEvent(CollisionEvent* event) { this->_collisionEvents.push_back(event); }
     53
     54    void reset();
    4355
    4456
    45     /** @returns true if this Collision has already been dispatched */
    46     inline bool isDispatched() { return this->bDispatched; }
    47     /** sets the dispatched flag to true */
    48     inline void dispatched() { this->bDispatched = true; }
    49 
    50     /** registers a @param event CollisionEvent to take place */
    51     inline void registerCollisionEvent(CollisionEvent* event) { this->collisionEvents.push_back(event); this->bDispatched = false;}
    52     /** @returns a vector of collision events */
    53     inline const std::vector<CollisionEvent*>& getCollisionEvents() const { return this->collisionEvents; }
     57    /** @returns iterator pointing to the beginning of the collision event vector */
     58    inline Iterator       begin()        { return this->_collisionEvents.begin(); }
     59    /** @returns const iterator pointing to the beginning of the collision event vector */
     60    inline ConstIterator  begin() const  { return this->_collisionEvents.begin(); }
     61    /** @returns iterator pointing to the end of the collision event vector */
     62    inline Iterator       end()          { return this->_collisionEvents.end(); }
     63    /** @returns const iterator pointing to the end of the collision event vector */
     64    inline ConstIterator  end() const    { return this->_collisionEvents.end(); }
    5465
    5566
    56     void flushCollisionEvents();
     67    /* misc interface functions */
     68  public:
     69    /** @return Collision WorldEntity A */
     70    inline WorldEntity* getEntityA() const { return this->_entityA; }
     71    /** @return Collision WorldEntity B */
     72    inline WorldEntity* getEntityB() const { return this->_entityB; }
     73    inline bool same(const WorldEntity& entityA, WorldEntity& entityB) const {
     74      return ((this->_entityA == &entityA && this->_entityB == &entityB) || (this->_entityA == &entityB && this->_entityB == &entityA)); }
     75
     76    /** @return true if Entity A collides */
     77    inline bool isEntityACollide() const { return this->_entityACollide; }
     78    /** sets the flag if it reacts @param flag true if it should react on entityA*/
     79    inline void setEntityACollide(bool flag) { this->_entityACollide = flag; }
     80    /** @return true if Entity B collides */
     81    inline bool isEntityBCollide() const { return this->_entityBCollide; }
     82    /** sets the flag if it reacts @param flag true if it should react on entityB*/
     83    inline void setEntityBCollide(bool flag) { this->_entityBCollide = flag; }
    5784
    5885
    5986  private:
    60     WorldEntity*                 entityA;                       //!< the collision body A
    61     WorldEntity*                 entityB;                       //!< the collision body B
    62     bool                         entityACollide;                //!< true if entity A is subscribed for collision reaction
    63     bool                         entityBCollide;                //!< true if entity B is subscribed for collision reaction
     87    WorldEntity*                 _entityA;                       //!< the collision body A
     88    WorldEntity*                 _entityB;                       //!< the collision body B
     89    bool                         _entityACollide;                //!< true if entity A is subscribed for collision reaction
     90    bool                         _entityBCollide;                //!< true if entity B is subscribed for collision reaction
    6491
    65     bool                         bDispatched;                   //!< true if this collision has already been dispatched
     92    CollisionVector              _collisionEvents;               //!< the collision event list
    6693
    67     std::vector<CollisionEvent*> collisionEvents;               //!< the collision event list
    68 };
     94  };
     95}
    6996
    7097#endif /* _COLLISION_H */
  • trunk/src/lib/collision_reaction/collision_event.cc

    r9869 r10013  
    1919#include "debug.h"
    2020
    21 /**
    22  *  standard constructor
    23  */
    24 CollisionEvent::CollisionEvent ()
     21namespace CoRe
    2522{
    26   this->entityA = NULL;
    27   this->entityB = NULL;
    28   this->bvA = NULL;
    29   this->bvB = NULL;
     23
     24  /**
     25   *  standard constructor
     26   */
     27  CollisionEvent::CollisionEvent ()
     28  {
     29    this->entityA = NULL;
     30    this->entityB = NULL;
     31    this->bvA = NULL;
     32    this->bvB = NULL;
     33  }
     34
     35
     36  /**
     37   *  standard deconstructor
     38   */
     39  CollisionEvent::~CollisionEvent ()
     40  {}
     41
    3042}
    3143
    32 
    33 /**
    34  *  standard deconstructor
    35  */
    36 CollisionEvent::~CollisionEvent ()
    37 {
    38 }
    39 
    40 
    41 
  • trunk/src/lib/collision_reaction/collision_event.h

    r9869 r10013  
    22 * @file collision_event.h
    33 *  Definition of a collision event
     4 *
     5 *  A collision event represents a collision of two bouning boxes. Every CollisionEvent belongs to a Collision object that
     6 *  represents the collision of two WorldEntities.
     7 *
     8 *  There are different types of collisions: obb collisions and bsp collisions. Both collision types use different techniques
     9 *  to represent collisions. This is why this class saves bounding boxes from the OBB as well as the planes from BSP.
    410 */
    511
     
    814
    915#include "vector.h"
     16#include "cr_engine.h"
    1017
    1118#include "cr_defs.h"
     
    1623class Plane;
    1724
     25namespace CoRe
     26{
     27
     28  //! A class representing a simple collision
     29  class CollisionEvent
     30  {
     31  public:
     32    CollisionEvent();
     33    virtual ~CollisionEvent();
     34
     35    /** collides two WorldEntities @param entityA world entity A, @param entityB world entity B, @param bvA volume A @param bvB volumeB */
     36    inline void collide(CREngine::CollisionType type, WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB)
     37    { this->collisionType = type; this->entityA = entityA; this->entityB = entityB; this->bvA = bvA; this->bvB = bvB; }
     38    /** collides two WorldEntities @param entity world entity , @param ground ground plane, @param position position on the ground */
     39    inline void collide(CREngine::CollisionType type, WorldEntity* entity, WorldEntity* groundEntity, Vector normal, Vector position, bool bInWall)
     40    {
     41      this->collisionType = type;
     42      this->entityA = entity;
     43      this->entityB = groundEntity;
     44      this->groundNormal = normal;
     45      this->position = position;
     46      this->bInWall = bInWall;
     47    }
    1848
    1949
    20 //! A class representing a simple collision
    21 class CollisionEvent {
    22  public:
    23    CollisionEvent();
    24   virtual ~CollisionEvent();
     50    /** @return CollisionEvent WorldEntity A */
     51    inline const WorldEntity* getEntityA() const  { return this->entityA; }
     52    /** @return CollisionEvent WorldEntity B */
     53    inline const WorldEntity* getEntityB() const  { return this->entityB; }
     54    /** @return Bounding Volume from EntityA */
     55    inline const BoundingVolume* getBVA() const { return this->bvA; }
     56    /** @return Bounding Volume from EntityB */
     57    inline const BoundingVolume* getBVB() const {  return this->bvB;  }
    2558
    26   /** collides two WorldEntities @param entityA world entity A, @param entityB world entity B, @param bvA volume A @param bvB volumeB */
    27   inline void collide(int type, WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB)
    28   { this->collisionType = type; this->entityA = entityA; this->entityB = entityB; this->bvA = bvA; this->bvB = bvB; }
    29   /** collides two WorldEntities @param entity world entity , @param ground ground plane, @param position position on the ground */
    30   inline void collide(int type, WorldEntity* entity, WorldEntity* groundEntity, Vector normal, Vector position, bool bInWall)
    31   { this->collisionType = type; this->entityA = entity; this->entityB = groundEntity, this->groundNormal = normal; this->position = position; this->bInWall = bInWall; }
     59    /** @return ground plane if collided with bsp model */
     60    inline const Vector& getGroundNormal() const  { return this->groundNormal; }
     61
     62    /** @return position of the position, only accurate if this is a collision with the ground!!! */
     63    inline const Vector& getCollisionPosition() const  {  return this->position;  }
     64
     65    /** @return the type of the collision */
     66    inline int getType() const  {  return this->collisionType;  }
     67
     68    /** @return true if the entity is in the wall */
     69    inline bool isInWall() const  {  return this->bInWall;  }
    3270
    3371
    34   /** @return CollisionEvent WorldEntity A */
    35   inline WorldEntity* getEntityA() const { return this->entityA; }
    36   /** @return CollisionEvent WorldEntity B */
    37   inline WorldEntity* getEntityB() const { return this->entityB; }
    38   /** @return Bounding Volume from EntityA */
    39   inline BoundingVolume* getBVA() const { return this->bvA; }
    40   /** @return Bounding Volume from EntityB */
    41   inline BoundingVolume* getBVB() const { return this->bvB; }
     72  private:
     73    WorldEntity*      entityA;                       //!< the collision body A
     74    WorldEntity*      entityB;                       //!< the collision body B
    4275
    43   /** @return ground plane if collided with bsp model */
    44   inline Vector getGroundNormal() { return this->groundNormal; }
     76    BoundingVolume*   bvA;                           //!< reference to the bounding volume A
     77    BoundingVolume*   bvB;                           //!< reference to the bounding volume B
    4578
    46   /** @return position of the position, only accurate if this is a collision with the ground!!! */
    47   inline Vector getCollisionPosition() { return this->position; }
     79    Vector            groundNormal;                  //!< the ground plane with which it collides (only for bsp-model collisions
     80    Vector            position;                      //!< position of the collision on the ground plane
    4881
    49   /** @return the type of the collision */
    50   inline int getType() { return this->collisionType; }
     82    bool              bInWall;                       //!< true if is in wall
     83    int               collisionType;                 //!< collision type
     84  };
    5185
    52   /** @return true if the entity is in the wall */
    53   inline bool isInWall() { return this->bInWall; }
    54 
    55 
    56  private:
    57   WorldEntity*      entityA;                       //!< the collision body A
    58   WorldEntity*      entityB;                       //!< the collision body B
    59 
    60   BoundingVolume*   bvA;                           //!< reference to the bounding volume A
    61   BoundingVolume*   bvB;                           //!< reference to the bounding volume B
    62 
    63   Vector            groundNormal;                  //!< the ground plane with which it collides (only for bsp-model collisions
    64   Vector            position;                      //!< position of the collision on the ground plane
    65 
    66   bool              bInWall;                       //!< true if is in wall
    67   int               collisionType;                 //!< collision type
    68 };
    69 
     86}
    7087#endif /* _COLLISION_EVENT_H */
  • trunk/src/lib/collision_reaction/collision_reaction.cc

    r9869 r10013  
    1111   ### File Specific:
    1212   main-programmer: Patrick Boenzli
    13    co-programmer: ...
    1413*/
    1514
     
    1817#include "collision_reaction.h"
    1918
     19namespace CoRe
     20{
     21
     22  ObjectListDefinition(CollisionReaction);
     23
     24  /**
     25   *  standard constructor
     26   */
     27  CollisionReaction::CollisionReaction ()
     28      : BaseObject()
     29  {
     30    this->registerObject(this, CollisionReaction::_objectList);
     31  }
    2032
    2133
    22 ObjectListDefinition(CollisionReaction);
     34  /**
     35   *  standard deconstructor
     36   */
     37  CollisionReaction::~CollisionReaction ()
     38  {}
    2339
    24 /**
    25  *  standard constructor
    26  */
    27 CollisionReaction::CollisionReaction ()
    28   : BaseObject()
    29 {
    30   this->registerObject(this, CollisionReaction::_objectList);
    3140}
    32 
    33 
    34 /**
    35  *  standard deconstructor
    36  */
    37 CollisionReaction::~CollisionReaction ()
    38 {
    39 }
    40 
  • trunk/src/lib/collision_reaction/collision_reaction.h

    r9869 r10013  
    99#include "base_object.h"
    1010
    11 
    12 
    13 class Collision;
    1411class WorldEntity;
    1512
     13namespace CoRe
     14{
    1615
    17 //! A class representing a simple collision
    18 class CollisionReaction : public BaseObject
    19 {
    20   ObjectListDeclaration(CollisionReaction);
     16  class Collision;
     17
     18  //! A class representing a simple collision
     19  class CollisionReaction : public BaseObject
     20  {
     21    ObjectListDeclaration(CollisionReaction);
    2122  public:
    2223    CollisionReaction();
     
    2526    virtual void reactToCollision(Collision* collision) = 0;
    2627
    27     virtual void update(WorldEntity* owner) {}
    28 
    2928    /** use this to do some collision offline calculations, only called for bContinuousPoll == true */
    3029    inline bool isContinuousPoll() const { return this->bContinuousPoll; }
     
    3231  private:
    3332    bool                    bContinuousPoll;       //!< if true the collision rection function is also called, if there was no collision
    34 };
     33  };
    3534
     35}
    3636#endif /* _COLLISION_REACTION_H */
  • trunk/src/lib/collision_reaction/cr_defs.h

    r9062 r10013  
    2828
    2929
    30 //!< the collision axis x collision event
    31 #define COLLISION_TYPE_AXIS_X      1
    32 #define COLLISION_TYPE_AXIS_X_NEG  2
    33 //!< the collision axis y collision event
    34 #define COLLISION_TYPE_AXIS_Y      3
    35 #define COLLISION_TYPE_AXIS_Y_NEG  4
    36 //!< the collision axis z collision event
    37 #define COLLISION_TYPE_AXIS_Z      5
    38 #define COLLISION_TYPE_AXIS_Z_NEG  6
    39 //!< the collision is a obb collision
    40 #define COLLISION_TYPE_OBB      8
    4130
    42 
    43 #endif /* _NETWORK_MANAGER */
     31#endif /* _CR_DEFS_H */
  • trunk/src/lib/collision_reaction/cr_engine.cc

    r9869 r10013  
    1717
    1818
     19#include "cr_object_damage.h"
     20#include "cr_physics_full_walk.h"
     21#include "cr_physics_ground_walk.h"
    1922
    2023#include "collision.h"
    2124#include "collision_event.h"
    22 #include "collision_handle.h"
     25#include "collision_filter.h"
     26#include "collision_tube.h"
    2327#include "cr_defs.h"
    2428
     
    2731#include "debug.h"
    2832
    29 
    30 
    31 ObjectListDefinition(CREngine);
    32 /**
    33  * standard constructor
    34  */
    35 CREngine::CREngine ()
    36   : BaseObject()
     33namespace CoRe
    3734{
    38   this->registerObject(this, CREngine::_objectList);
    39    this->setName("CREngine");
    40 
    41    this->init();
     35
     36  ObjectListDefinition(CREngine);
     37
     38
     39  /**
     40   * standard constructor
     41   */
     42  CREngine::CREngine ()
     43      : BaseObject()
     44  {
     45    this->registerObject(this, CREngine::_objectList);
     46    this->setName("CREngine");
     47
     48    this->init();
     49  }
     50
     51  /**
     52   *  the singleton reference to this class
     53   */
     54  CREngine* CREngine::singletonRef = NULL;
     55
     56  /**
     57     @brief standard deconstructor
     58   */
     59  CREngine::~CREngine ()
     60  {
     61    CREngine::singletonRef = NULL;
     62
     63    if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
     64      PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
     65    if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
     66      PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
     67
     68    this->reset();
     69
     70    CollisionIterator it1 = this->collisionsUnused.begin();
     71    for(; it1 < this->collisionsUnused.end(); it1++)
     72      delete *it1;
     73    CollisionEventIterator it2 = this->collisionEventsUnused.begin();
     74    for(; it2 < this->collisionEventsUnused.end(); it2++)
     75      delete *it2;
     76
     77    this->collisionsUnused.clear();
     78    this->collisionEventsUnused.clear();
     79  }
     80
     81
     82  /**
     83   * inits the CREngine to a working state
     84   */
     85  void CREngine::init()
     86  {
     87    // precaching:
     88    // create a list of Collisions and CollisionEvents for fast object recycling purposes
     89    for( int i = 0; i < CR_MAX_COLLISIONS; i++)
     90      this->collisionsUnused.push_back(new Collision());
     91    for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
     92      this->collisionEventsUnused.push_back(new CollisionEvent());
     93
     94
     95    // push the collision reaction object on the list in the right order
     96
     97    // physical reactions
     98    this->_reactionList[CREngine::CR_PHYSICS_MOMENTUM]      = NULL;
     99    this->_reactionList[CREngine::CR_PHYSICS_STEP_BACK]     = NULL;
     100    this->_reactionList[CREngine::CR_PHYSICS_GROUND_WALK]   = new CRPhysicsGroundWalk();
     101    this->_reactionList[CREngine::CR_PHYSICS_FULL_WALK]     = new CRPhysicsFullWalk();
     102    this->_reactionList[CREngine::CR_PHYSICS_DAMAGE]        = NULL;
     103    // object based reactions
     104    this->_reactionList[CREngine::CR_OBJECT_DAMAGE]         = new CRObjectDamage();
     105    this->_reactionList[CREngine::CR_OBJECT_PICKUP]         = NULL;
     106    // misc reactions
     107    this->_reactionList[CREngine::CR_VERTEX_TRAFO]          = NULL;
     108    this->_reactionList[CREngine::CR_SPECIAL_CALLBACK]      = NULL;
     109  }
     110
     111
     112  /**
     113   * @returns an instance to a collision object. instead of creating new object this ones can be resycled
     114   */
     115  Collision* CREngine::popCollisionObject()
     116  {
     117    if( !this->collisionsUnused.empty())
     118    {
     119      this->collisionsUsed.push_back(this->collisionsUnused.back());
     120      this->collisionsUnused.pop_back();
     121      return this->collisionsUsed.back();
     122    }
     123    else
     124    {
     125      PRINTF(0)("There is no Collision Object left in the precache table, fatal error will cause segfault, change CR_MAX_COLLISIONS\n");
     126      assert(false);
     127      return NULL;
     128    }
     129  }
     130
     131
     132  /**
     133   * @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled
     134   */
     135  CollisionEvent* CREngine::popCollisionEventObject()
     136  {
     137    if( !this->collisionEventsUnused.empty())
     138    {
     139      this->collisionEventsUsed.push_back(this->collisionEventsUnused.back());
     140      this->collisionEventsUnused.pop_back();
     141      return this->collisionEventsUsed.back();
     142    }
     143    else
     144    {
     145      PRINTF(0)("There is no Collision Object left in the precache table, fatal error will cause segfault, change CR_MAX_COLLISION_EVENTS\n");
     146      assert(false);
     147      return NULL;
     148    }
     149  }
     150
     151
     152  /**
     153   * handles all collisions in registered in this tube
     154   */
     155  void CREngine::handleCollisions()
     156  {
     157    // for all collisions:
     158    CollisionIterator ci = CollisionTube::getInstance()->begin();
     159    for(; ci < CollisionTube::getInstance()->end(); ci++)
     160    {
     161      for( int i = CREngine::CR_PHYSICS_MOMENTUM; i < CREngine::CR_NUMBER; i++)
     162      {
     163        if( _reactionList[i] == NULL)
     164          continue;
     165
     166//         PRINTF(0)("CR CHECK: collision between: %s, %s\n", (*ci)->getEntityA()->getClassName().c_str(), (*ci)->getEntityB()->getClassName().c_str());
     167
     168        // check if entity A or B is subscibed for this event
     169        bool aReact = (*ci)->getEntityA()->isReactive(*(*ci)->getEntityB(), (CREngine::ReactionType)i);
     170        bool bReact = (*ci)->getEntityB()->isReactive(*(*ci)->getEntityA(), (CREngine::ReactionType)i);
     171
     172        // store this information
     173        (*ci)->setEntityACollide(aReact);
     174        (*ci)->setEntityBCollide(bReact);
     175
     176        // and execute the reaction
     177        if(  aReact || bReact)
     178        {
     179          this->_reactionList[i]->reactToCollision(*ci);
     180          //PRINTF(0)("executing reaction: %s, between %s - %s\n", this->_reactionList[i]->getClassName().c_str(), (*ci)->getEntityA()->getClassName().c_str(), (*ci)->getEntityB()->getClassName().c_str());
     181        }
     182      }
     183      (*ci)->reset();
     184    }
     185
     186    this->reset();
     187  }
     188
     189
     190  /**
     191   * flushes all the collision lists and puts them to their initial state
     192   */
     193  void CREngine::reset()
     194  {
     195    CollisionIterator it1 = this->collisionsUsed.begin();
     196    for(; it1 < this->collisionsUsed.end(); it1++)
     197      this->collisionsUnused.push_back(*it1);
     198
     199    CollisionEventIterator it2 = this->collisionEventsUsed.begin();
     200    for(; it2 < this->collisionEventsUsed.end(); it2++)
     201      this->collisionEventsUnused.push_back(*it2);
     202
     203    this->collisionsUsed.clear();
     204    this->collisionEventsUsed.clear();
     205
     206    CollisionTube::getInstance()->reset();
     207  }
     208
     209
     210  void CREngine::debug()
     211  {
     212  }
     213
    42214}
    43 
    44 /**
    45  *  the singleton reference to this class
    46  */
    47 CREngine* CREngine::singletonRef = NULL;
    48 
    49 /**
    50    @brief standard deconstructor
    51  */
    52 CREngine::~CREngine ()
    53 {
    54   CREngine::singletonRef = NULL;
    55 
    56   if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
    57     PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
    58   if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
    59     PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
    60 
    61   this->reset();
    62 
    63   std::vector<Collision*>::iterator it1 = this->collisionsUnused.begin();
    64   for(; it1 < this->collisionsUnused.end(); it1++)
    65     delete *it1;
    66   std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin();
    67   for(; it2 < this->collisionEventsUnused.end(); it2++)
    68     delete *it2;
    69 
    70   this->collisionsUnused.clear();
    71   this->collisionEventsUnused.clear();
    72 }
    73 
    74 /**
    75  * inits the CREngine to a working state
    76  */
    77 void CREngine::init()
    78 {
    79   // create a list of Collision events (precaching)
    80   for( int i = 0; i < CR_MAX_COLLISIONS; i++)
    81     this->collisionsUnused.push_back(new Collision());
    82   for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
    83     this->collisionEventsUnused.push_back(new CollisionEvent());
    84 }
    85 
    86 
    87 /**
    88  * flushes the CollisionHandles and restores the CREngine to the initial state
    89  */
    90 void CREngine::reset()
    91 {
    92   // first clear all CollisionHandles
    93 
    94   std::vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
    95   for(; it < this->collisionHandles.end(); it++)
    96   {
    97     (*it)->reset();
    98     delete *it;
    99   }
    100 
    101   this->collisionHandles.clear();
    102 }
    103 
    104 
    105 /**
    106  * subscribes a WorldEntity for a CollisionReaction
    107  *  @param owner: the WE to subscribe
    108  *  @param type: the type of collision reaction to perform
    109  *  @return the newly created CollisionHandle
    110  */
    111 CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
    112 {
    113   CollisionHandle* ch = new CollisionHandle(owner, type);
    114   this->collisionHandles.push_back(ch);
    115 
    116   return ch;
    117 }
    118 
    119 
    120 /**
    121  * unsubscribe reaction from the reaction list
    122  *  @param collisionHandle the CollisionHandle to remove
    123  *  @param returns true if worked collrectly
    124  */
    125 bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
    126 {
    127   std::vector<CollisionHandle*>::iterator it;
    128   for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)  {
    129     if( *it == collisionHandle) {
    130       this->collisionHandles.erase(it);
    131       delete collisionHandle;
    132       return true;
    133     }
    134   }
    135   return false;
    136 }
    137 
    138 
    139 /**
    140  * processes the collisions by calling the EventHandlers
    141  */
    142 void CREngine::handleCollisions()
    143 {
    144   std::vector<CollisionHandle*>::iterator it;
    145   for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
    146   {
    147     if( !(*it)->isDispatched() || (*it)->isContinuousPoll())  //does it have any collisions to report at all
    148     {
    149       (*it)->handleCollisions();
    150     }
    151   }
    152   this->flushCollisions();
    153 }
    154 
    155 
    156 /**
    157  * flushes all the collision lists and puts them to their initial state
    158  */
    159 void CREngine::flushCollisions()
    160 {
    161   std::vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
    162   for(; it1 < this->collisionsUsed.end(); it1++)
    163     this->collisionsUnused.push_back(*it1);
    164 
    165   std::vector<CollisionEvent*>::iterator it2 = this->collisionEventsUsed.begin();
    166   for(; it2 < this->collisionEventsUsed.end(); it2++)
    167     this->collisionEventsUnused.push_back(*it2);
    168 
    169   this->collisionsUsed.clear();
    170   this->collisionEventsUsed.clear();
    171 }
    172 
    173 
    174 void CREngine::debug()
    175 {
    176 
    177 }
    178 
  • trunk/src/lib/collision_reaction/cr_engine.h

    r9869 r10013  
    1616#include <vector>
    1717
    18 // FORWARD DECLARATION
    19 class CollisionHandle;
    20 class Collision;
    21 class CollisionEvent;
    2218class WorldEntity;
    2319
    24 //! A default singleton class.
    25 class CREngine : public BaseObject
     20
     21namespace CoRe
    2622{
    27   ObjectListDeclaration(CREngine);
     23  class Collision;
     24  class CollisionEvent;
     25  class CollisionReaction;
     26
     27
     28  //! A default singleton class.
     29  class CREngine : public BaseObject
     30  {
     31    ObjectListDeclaration(CREngine);
     32
     33    typedef std::vector<CollisionEvent*>             CollisionEventVector;
     34    typedef std::vector<Collision*>                  CollisionVector;
     35    typedef std::vector<Collision*>::iterator        CollisionIterator;
     36    typedef std::vector<CollisionEvent*>::iterator   CollisionEventIterator;
     37
    2838
    2939  public:
    30   typedef enum CRType {
    31     CR_PHYSICS_MOMENTUM   = 0,    //!< physical reaction: conservervation of momentum
    32     CR_PHYSICS_STEP_BACK,         //!< physical reaction: just go to the last position without collisions
    33     CR_PHYSICS_GROUND_WALK,       //!< physical reaction: stand on the ground, no movement: simulating simple normal force away from the gravity force
    34     CR_PHYSICS_FULL_WALK,         //!< physical reaction: walking on the ground (inkl. hills etc)
    35     CR_PHYSICS_DAMAGE,            //!< physical reaction: daling damage according to the object energy and their structural stability
     40    typedef enum ReactionType {
     41      CR_PHYSICS_MOMENTUM   = 0,           //!< physical reaction: conservervation of momentum
     42      CR_PHYSICS_STEP_BACK,                //!< physical reaction: just go to the last position without collisions
     43      CR_PHYSICS_GROUND_WALK,              //!< physical reaction: stand on the ground, no movement: simple normal force away from the gravity force
     44      CR_PHYSICS_FULL_WALK,                //!< physical reaction: walking on the ground (inkl. hills etc)
     45      CR_PHYSICS_DAMAGE,                   //!< physical reaction: daling damage according to the object energy and their structural stability
    3646
    37     CR_OBJECT_DAMAGE,             //!< object raction: deals damage according to the objects specific damage potential (like weapons, nukes, etc.)
    38     CR_OBJECT_PICKUP,             //!< object rection: calling the objects pickup functions, let them handle the collision (once!)
     47      CR_OBJECT_DAMAGE,                    //!< object raction: deals damage according to the objects specific damage potential (like weapons, nukes, etc.)
     48      CR_OBJECT_PICKUP,                    //!< object rection: calling the objects pickup functions, let them handle the collision (once!)
    3949
    40     CR_VERTEX_TRAFO,              //!< vertex trafo: transforming the vertex according to the damage
     50      CR_VERTEX_TRAFO,                     //!< vertex trafo: transforming the vertex according to the damage
    4151
    42     CR_SPECIAL_CALLBACK,          //!< special: call a callback function
     52      CR_SPECIAL_CALLBACK,                 //!< special: call a callback function
    4353
    44     CR_NUMBER
     54      CR_NUMBER
     55    };
     56
     57    typedef enum CollisionType {
     58      CR_COLLISION_TYPE_AXIS_X       = 0,  //!< collision on x axis
     59      CR_COLLISION_TYPE_AXIS_X_NEG,        //!< collision on negative x axis
     60      CR_COLLISION_TYPE_AXIS_Y,            //!< collision on y axis
     61      CR_COLLISION_TYPE_AXIS_Y_NEG,        //!< collision on negative y axis
     62      CR_COLLISION_TYPE_AXIS_Z,            //!< collision on z axis
     63      CR_COLLISION_TYPE_AXIS_Z_NEG,        //!< collision on negative z axis
     64      CR_COLLISION_TYPE_OBB,               //!< object aligned bounding box collide
     65
     66      CR_COLLISION_TYPE_NUMBER
     67    };
     68
     69
     70    virtual ~CREngine(void);
     71
     72    /** @returns a Pointer to the only object of this Class */
     73    inline static CREngine* getInstance() { if (!singletonRef) singletonRef = new CREngine();  return singletonRef; };
     74
     75    Collision* popCollisionObject();
     76    CollisionEvent* popCollisionEventObject();
     77
     78    void handleCollisions();
     79
     80    void debug();
     81
     82
     83  private:
     84    CREngine();
     85    void init();
     86
     87    void reset();
     88
     89
     90  private:
     91    static CREngine*                    singletonRef;             //!< the reference to the CREngine object (singleton)
     92
     93    CollisionVector                     collisionsUsed;           //!< a list of used, cached collisions
     94    CollisionVector                     collisionsUnused;         //!< a list of unused, cached collisions
     95
     96    CollisionEventVector                collisionEventsUsed;      //!< a list of used, cached collision events
     97    CollisionEventVector                collisionEventsUnused;    //!< a list of unused, cached collision events
     98
     99    CollisionReaction*                  _reactionList[CREngine::CR_NUMBER];  //!< the collision reaction list containing all reactions types
     100
     101
    45102  };
    46103
    47   virtual ~CREngine(void);
    48 
    49   /** @returns a Pointer to the only object of this Class */
    50   inline static CREngine* getInstance() { if (!singletonRef) singletonRef = new CREngine();  return singletonRef; };
    51 
    52   void reset();
    53 
    54 
    55   CollisionHandle* subscribeReaction(WorldEntity* worldEntity, CRType type);
    56 
    57   bool unsubscribeReaction(CollisionHandle* collisionHandle);
    58 
    59   void handleCollisions();
    60 
    61   /** @returns an instance to a collision object. instead of creating new object this ones can be resycled */
    62   inline Collision* popCollisionObject() {
    63     if( !this->collisionsUnused.empty()) {
    64       this->collisionsUsed.push_back(this->collisionsUnused.back()); this->collisionsUnused.pop_back(); return this->collisionsUsed.back(); } else return NULL; }
    65 
    66 
    67   /** @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled */
    68   inline CollisionEvent* popCollisionEventObject() {
    69     if( !this->collisionEventsUnused.empty()) {
    70       this->collisionEventsUsed.push_back(this->collisionEventsUnused.back()); this->collisionEventsUnused.pop_back(); return this->collisionEventsUsed.back(); } else return NULL; }
    71 
    72   void debug();
    73 
    74 
    75 private:
    76   CREngine();
    77   void init();
    78 
    79   void flushCollisions();
    80 
    81 
    82 private:
    83   std::vector<CollisionHandle*>       collisionHandles;         //!< list with the collision handles
    84 
    85   std::vector<Collision*>             collisionsUsed;           //!< a list of used, cached collisions
    86   std::vector<Collision*>             collisionsUnused;         //!< a list of unused, cached collisions
    87 
    88   std::vector<CollisionEvent*>        collisionEventsUsed;      //!< a list of used, cached collision events
    89   std::vector<CollisionEvent*>        collisionEventsUnused;    //!< a list of unused, cached collision events
    90 
    91   static CREngine*                    singletonRef;             //!< the reference to the CREngine object (singleton)
    92 };
    93 
     104}
    94105#endif /* _CR_ENGINE_ */
  • trunk/src/lib/collision_reaction/cr_object_damage.cc

    r9869 r10013  
    2626#include "debug.h"
    2727
     28namespace CoRe
     29{
     30
     31  ObjectListDefinition(CRObjectDamage);
     32  /**
     33   *  standard constructor
     34   */
     35  CRObjectDamage::CRObjectDamage ()
     36      : CollisionReaction()
     37  {
     38    this->registerObject(this, CRObjectDamage::_objectList);
     39  }
    2840
    2941
    30 ObjectListDefinition(CRObjectDamage);
    31 /**
    32  *  standard constructor
    33  */
    34 CRObjectDamage::CRObjectDamage ()
    35   : CollisionReaction()
    36 {
    37   this->registerObject(this, CRObjectDamage::_objectList);
    38 }
     42  /**
     43   *  standard deconstructor
     44   */
     45  CRObjectDamage::~CRObjectDamage ()
     46  {}
    3947
    4048
    41 /**
    42  *  standard deconstructor
    43  */
    44 CRObjectDamage::~CRObjectDamage ()
    45 {
    46 }
     49  /**
     50   * caluculates and applys the reaction to a specific collision
     51   *  @param collision the collision
     52   */
     53  void CRObjectDamage::reactToCollision(Collision* collision)
     54  {
     55    float damage = 0.0f;
    4756
     57    PRINTF(0)("Dealing damage - Handling collision: %s vs %s\n",
     58              collision->getEntityA()->getClassCName(),
     59              collision->getEntityB()->getClassCName());
    4860
    49 /**
    50  * caluculates and applys the reaction to a specific collision
    51  *  @param collision the collision
    52  */
    53 void CRObjectDamage::reactToCollision(Collision* collision)
    54 {
    55   float damage = 0.0f;
     61    // the collision damage been dealed by the entity
     62    if( collision->isEntityACollide())
     63    {
     64      damage = collision->getEntityB()->getDamage();
     65      collision->getEntityA()->hit(damage, collision->getEntityB());
     66      PRINTF(0)("Dealing damage - %f damage to %s \n", damage, collision->getEntityA()->getClassCName());
     67    }
    5668
    57   PRINTF(4)("Dealing damage - Handling collision: %s vs %s\n",
    58             collision->getEntityA()->getClassCName(),
    59             collision->getEntityB()->getClassCName());
     69    if( collision->isEntityBCollide())
     70    {
     71      damage = collision->getEntityA()->getDamage();
     72      collision->getEntityB()->hit(damage, collision->getEntityA());
     73      PRINTF(0)("Dealing damage - %f damage to %s \n", damage, collision->getEntityB()->getClassCName());
     74    }
    6075
    61   // the collision damage been dealed by the entity
    62   if( collision->isEntityACollide()) {
    63     damage = collision->getEntityB()->getDamage();
    64     collision->getEntityA()->hit(damage, collision->getEntityB());
    65     PRINTF(4)("Dealing damage - %f damage to %s \n", damage, collision->getEntityA()->getClassCName());
     76    //   const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
     77    //   std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
     78    //   for(; it != collisionEvents->end(); it++)
     79    //   {
     80    //     // go through the collisions and try to estimate the damage
     81    //     mass = (*it)->getEntityA()->getMass();
     82    //   }
     83
    6684  }
    6785
    68   if( collision->isEntityBCollide()) {
    69     damage = collision->getEntityA()->getDamage();
    70     collision->getEntityB()->hit(damage, collision->getEntityA());
    71     PRINTF(4)("Dealing damage - %f damage to %s \n", damage, collision->getEntityB()->getClassCName());
    72   }
    73 
    74   collision->flushCollisionEvents();
    75   collision->dispatched();
    76 
    77 //   const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
    78 //   std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
    79 //   for(; it != collisionEvents->end(); it++)
    80 //   {
    81 //     // go through the collisions and try to estimate the damage
    82 //     mass = (*it)->getEntityA()->getMass();
    83 //   }
    84 
    8586}
    86 
  • trunk/src/lib/collision_reaction/cr_object_damage.h

    r9869 r10013  
    77#define _CR_OBJECT_DAMAGE_H
    88
     9
    910#include "collision_reaction.h"
    1011
    1112
    12 class Collision;
     13namespace CoRe
     14{
    1315
    14 //! A class representing a reaction to a collision: dealing damage to an object
    15 class CRObjectDamage : public CollisionReaction
    16 {
    17   ObjectListDeclaration(CRObjectDamage);
     16  class Collision;
     17
     18  //! A class representing a reaction to a collision: dealing damage to an object
     19  class CRObjectDamage : public CollisionReaction
     20  {
     21    ObjectListDeclaration(CRObjectDamage);
    1822  public:
    1923    CRObjectDamage();
     
    2226    virtual void reactToCollision(Collision* collision);
    2327
    24   private:
    25 
    26 };
     28  };
     29}
    2730
    2831#endif /* _CR_OBJECT_DAMAGE_H */
  • trunk/src/lib/collision_reaction/cr_physics_full_walk.cc

    r9869 r10013  
    3333#include "cr_defs.h"
    3434
    35 
    36 
    37 ObjectListDefinition(CRPhysicsFullWalk);
    38 /**
    39  *  standard constructor
    40  */
    41 CRPhysicsFullWalk::CRPhysicsFullWalk ()
    42     : CollisionReaction()
     35namespace CoRe
    4336{
    44   this->registerObject(this, CRPhysicsFullWalk::_objectList);
     37
     38  ObjectListDefinition(CRPhysicsFullWalk);
     39  /**
     40   *  standard constructor
     41   */
     42  CRPhysicsFullWalk::CRPhysicsFullWalk ()
     43      : CollisionReaction()
     44  {
     45    this->registerObject(this, CRPhysicsFullWalk::_objectList);
     46  }
     47
     48
     49  /**
     50   *  standard deconstructor
     51   */
     52  CRPhysicsFullWalk::~CRPhysicsFullWalk ()
     53  {}
     54
     55
     56  /**
     57   * caluculates and applys the reaction to a specific collision
     58   *  @param collision the collision
     59   */
     60  void CRPhysicsFullWalk::reactToCollision(Collision* collision)
     61  {
     62
     63    AABB* box = collision->getEntityA()->getModelAABB();
     64    WorldEntity* entity = collision->getEntityA();
     65
     66    if( box == NULL)
     67    {
     68      PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
     69      return;
     70    }
     71
     72
     73    float CR_MAX_WALK_HEIGHT = 15.0f;
     74//    float CR_THRESHOLD = 0.2f;
     75
     76    float height = 0.0f;
     77    float front = 0.0f;
     78    float back = 0.0f;
     79    float right = 0.0f;
     80    float left = 0.0f;
     81
     82
     83    std::vector<CollisionEvent*>::const_iterator it = collision->begin();
     84    for(; it != collision->end(); it++)
     85    {
     86
     87      CollisionEvent* ce = (*it);
     88      Vector normal = ce->getGroundNormal();
     89
     90      // calculate the collision position
     91      Vector collPos =  collision->getEntityA()->getAbsCoor()  + box->center - ce->getCollisionPosition();
     92
     93      // test the 3 axis differently
     94      switch( ce->getType())
     95      {
     96          /* collision in the X-AXIS */
     97        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_X:
     98          front = collPos.len() - box->halfLength[0];
     99
     100          // object is beneath the plane (ground)
     101          if( front <= 0.0f )
     102          {
     103            Vector dirX = entity->getAbsDirX();
     104            dirX.y = 0.0f;
     105            dirX.normalize();
     106            Vector backoff = dirX * front;
     107
     108            entity->shiftCoor(backoff);
     109          }
     110          else if( ce->isInWall())
     111          {
     112            // object is already in the wall
     113            entity->setAbsCoor(entity->getLastAbsCoor());
     114          }
     115          break;
     116
     117        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_X_NEG:
     118          back = collPos.len() - box->halfLength[0];
     119
     120          // object is beneath the plane (ground)
     121          if( back <= 0.0f)
     122          {
     123            Vector dirX = entity->getAbsDirX();
     124            dirX.y = 0.0f;
     125            dirX.normalize();
     126            Vector backoff = dirX * back * -1.0f;
     127
     128            entity->shiftCoor(backoff);
     129          }
     130          else if( ce->isInWall())
     131          {
     132            // object is already in the wall
     133            entity->setAbsCoor(entity->getLastAbsCoor());
     134          }
     135          break;
     136
     137
     138          /* collision in the Y-AXIS */
     139        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_Y_NEG:
     140          // calulate the height above ground
     141          height = collPos.len() - box->halfLength[1];
     142
     143
     144          // object is beneath the plane (ground)
     145          //         if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
     146          if( height < 0.0f && -height < CR_MAX_WALK_HEIGHT)
     147          {
     148            entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
     149            entity->setOnGround(true);
     150          }
     151          // object is already in the wall
     152          else if( ce->isInWall())
     153          {
     154            entity->setAbsCoor(entity->getLastAbsCoor());
     155            PRINTF(0)("ground collision: reset pos\n");
     156          }
     157          else
     158          {
     159            // entity is not on ground
     160            entity->setOnGround(false);
     161          }
     162          break;
     163
     164
     165          /* collision in the Z-AXIS */
     166        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_Z:
     167
     168          right = collPos.len()  - box->halfLength[2];
     169
     170          // object is beneath the plane (ground)
     171          if( right <= 0.0f )
     172          {
     173            Vector dirZ = entity->getAbsDirZ();
     174            dirZ.y = 0.0f;
     175            dirZ.normalize();
     176            Vector backoff = dirZ * right;
     177            entity->shiftCoor(backoff);
     178          }
     179          else if( ce->isInWall())
     180          {
     181            // object is already in the wall
     182            entity->setAbsCoor(entity->getLastAbsCoor());
     183          }
     184          break;
     185
     186
     187          // collision in the z-axis
     188        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_Z_NEG:
     189
     190          left = collPos.len()  - box->halfLength[2];
     191
     192          // object is beneath the plane (ground)
     193          if( left <= 0.0f )
     194          {
     195            Vector dirZ = entity->getAbsDirZ();
     196            dirZ.y = 0.0f;
     197            dirZ.normalize();
     198            Vector backoff = dirZ * left*-1.0f;
     199            entity->shiftCoor(backoff);
     200          }
     201          // object is already in the wall
     202          else if( ce->isInWall())
     203          {
     204            entity->setAbsCoor(entity->getLastAbsCoor());
     205          }
     206          break;
     207      }
     208    }
     209    //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
     210  }
     211
     212
     213
     214
    45215}
    46216
    47 
    48 /**
    49  *  standard deconstructor
    50  */
    51 CRPhysicsFullWalk::~CRPhysicsFullWalk ()
    52 {}
    53 
    54 
    55 /**
    56  * caluculates and applys the reaction to a specific collision
    57  *  @param collision the collision
    58  */
    59 void CRPhysicsFullWalk::reactToCollision(Collision* collision)
    60 {
    61 
    62   AABB* box = collision->getEntityB()->getModelAABB();
    63   WorldEntity* entity = collision->getEntityB();
    64 
    65   if( box == NULL)
    66   {
    67     PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
    68     return;
    69   }
    70 
    71 
    72   float CR_MAX_WALK_HEIGHT = 15.0f;
    73   float CR_THRESHOLD = 0.2f;
    74 
    75   float height = 0.0f;
    76   float front = 0.0f;
    77   float back = 0.0f;
    78   float right = 0.0f;
    79   float left = 0.0f;
    80 
    81 
    82   const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
    83   std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
    84   for(; it != collisionEvents->end(); it++)
    85   {
    86 
    87     CollisionEvent* ce = (*it);
    88     Vector normal = ce->getGroundNormal();
    89 
    90     // calculate the collision position
    91     Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
    92 
    93     // test the 3 axis differently
    94     switch( ce->getType())
    95     {
    96         /* collision in the X-AXIS */
    97       case COLLISION_TYPE_AXIS_X:
    98         front = collPos.len() - box->halfLength[0];
    99 
    100         // object is beneath the plane (ground)
    101         if( front <= 0.0f )
    102         {
    103           Vector dirX = entity->getAbsDirX();
    104           dirX.y = 0.0f;
    105           dirX.normalize();
    106           Vector backoff = dirX * front;
    107 
    108           entity->shiftCoor(backoff);
    109         }
    110         else if( ce->isInWall())
    111         {
    112           // object is already in the wall
    113           entity->setAbsCoor(entity->getLastAbsCoor());
    114         }
    115         break;
    116 
    117       case COLLISION_TYPE_AXIS_X_NEG:
    118         back = collPos.len() - box->halfLength[0];
    119 
    120         // object is beneath the plane (ground)
    121         if( back <= 0.0f)
    122         {
    123           Vector dirX = entity->getAbsDirX();
    124           dirX.y = 0.0f;
    125           dirX.normalize();
    126           Vector backoff = dirX * back * -1.0f;
    127 
    128           entity->shiftCoor(backoff);
    129         }
    130         else if( ce->isInWall())
    131         {
    132           // object is already in the wall
    133           entity->setAbsCoor(entity->getLastAbsCoor());
    134         }
    135         break;
    136 
    137 
    138         /* collision in the Y-AXIS */
    139       case COLLISION_TYPE_AXIS_Y_NEG:
    140         // calulate the height above ground
    141         height = collPos.len() - box->halfLength[1];
    142 
    143 
    144         // object is beneath the plane (ground)
    145         //         if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
    146         if( height < 0.0f && -height < CR_MAX_WALK_HEIGHT)
    147         {
    148           entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
    149           entity->setOnGround(true);
    150         }
    151         // object is already in the wall
    152         else if( ce->isInWall())
    153         {
    154           entity->setAbsCoor(entity->getLastAbsCoor());
    155           PRINTF(0)("ground collision: reset pos\n");
    156         }
    157         else
    158         {
    159           // entity is not on ground
    160           entity->setOnGround(false);
    161         }
    162         break;
    163 
    164 
    165         /* collision in the Z-AXIS */
    166       case COLLISION_TYPE_AXIS_Z:
    167 
    168         right = collPos.len()  - box->halfLength[2];
    169 
    170         // object is beneath the plane (ground)
    171         if( right <= 0.0f )
    172         {
    173           Vector dirZ = entity->getAbsDirZ();
    174           dirZ.y = 0.0f;
    175           dirZ.normalize();
    176           Vector backoff = dirZ * right;
    177           entity->shiftCoor(backoff);
    178         }
    179         else if( ce->isInWall())
    180         {
    181           // object is already in the wall
    182           entity->setAbsCoor(entity->getLastAbsCoor());
    183         }
    184         break;
    185 
    186 
    187         // collision in the z-axis
    188       case COLLISION_TYPE_AXIS_Z_NEG:
    189 
    190         left = collPos.len()  - box->halfLength[2];
    191 
    192         // object is beneath the plane (ground)
    193         if( left <= 0.0f )
    194         {
    195           Vector dirZ = entity->getAbsDirZ();
    196           dirZ.y = 0.0f;
    197           dirZ.normalize();
    198           Vector backoff = dirZ * left*-1.0f;
    199           entity->shiftCoor(backoff);
    200         }
    201         // object is already in the wall
    202         else if( ce->isInWall())
    203         {
    204           entity->setAbsCoor(entity->getLastAbsCoor());
    205         }
    206         break;
    207     }
    208   }
    209   //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
    210 
    211 
    212 
    213 
    214 
    215 
    216 
    217 }
    218 
    219 
    220 
    221 
    222 /**
    223  * use this to do some collision offline calculations, only called for bContinuousPoll == true
    224  */
    225 void CRPhysicsFullWalk::update(WorldEntity* owner)
    226 {}
    227 
    228 
  • trunk/src/lib/collision_reaction/cr_physics_full_walk.h

    r9869 r10013  
    99#include "collision_reaction.h"
    1010
     11namespace CoRe
     12{
    1113
    12 class Collision;
     14  class Collision;
    1315
    14 //! A class representing a reaction to a collision: dealing damage to an object
    15 class CRPhysicsFullWalk : public CollisionReaction
    16 {
    17   ObjectListDeclaration(CRPhysicsFullWalk);
     16  //! A class representing a reaction to a collision: dealing damage to an object
     17  class CRPhysicsFullWalk : public CollisionReaction
     18  {
     19    ObjectListDeclaration(CRPhysicsFullWalk);
    1820  public:
    1921    CRPhysicsFullWalk();
     
    2123
    2224    virtual void reactToCollision(Collision* collision);
    23 
    24     virtual void update(WorldEntity* entity);
    25 
    26 
    27   private:
    28     float        downspeed;
    29 };
    30 
     25  };
     26}
    3127#endif /* _CR_PHYSICS_FULL_WALK_H */
  • trunk/src/lib/collision_reaction/cr_physics_ground_walk.cc

    r9869 r10013  
    3434
    3535
     36namespace CoRe
     37{
    3638
    37 ObjectListDefinition(CRPhysicsGroundWalk);
    38 /**
    39  *  standard constructor
    40  */
    41 CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
    42     : CollisionReaction()
    43 {
    44   this->registerObject(this, CRPhysicsGroundWalk::_objectList);
    45 }
     39  ObjectListDefinition(CRPhysicsGroundWalk);
    4640
    4741
    48 /**
    49  *  standard deconstructor
    50  */
    51 CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
    52 {}
    53 
    54 
    55 /**
    56  * caluculates and applys the reaction to a specific collision
    57  *  @param collision the collision
    58  */
    59 void CRPhysicsGroundWalk::reactToCollision(Collision* collision)
    60 {
    61 
    62   AABB* box = collision->getEntityB()->getModelAABB();
    63   WorldEntity* entity = collision->getEntityB();
    64 
    65   if( box == NULL)
     42  /**
     43   *  standard constructor
     44   */
     45  CRPhysicsGroundWalk::CRPhysicsGroundWalk ()
     46      : CollisionReaction()
    6647  {
    67     PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
    68     return;
     48    this->registerObject(this, CRPhysicsGroundWalk::_objectList);
    6949  }
    7050
    7151
    72   float CR_MAX_WALK_HEIGHT = 15.0f;
    73 
    74   float height = 0.0f;
     52  /**
     53   *  standard deconstructor
     54   */
     55  CRPhysicsGroundWalk::~CRPhysicsGroundWalk ()
     56  {}
    7557
    7658
    77   const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents());
    78   std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin();
    79   for(; it != collisionEvents->end(); it++)
     59  /**
     60   * caluculates and applys the reaction to a specific collision
     61   *  @param collision the collision
     62   */
     63  void CRPhysicsGroundWalk::reactToCollision(Collision* collision)
    8064  {
    8165
    82     CollisionEvent* ce = (*it);
    83     Vector normal = ce->getGroundNormal();
     66    AABB* box = collision->getEntityA()->getModelAABB();
     67    WorldEntity* entity = collision->getEntityA();
    8468
    85     // calculate the collision position
    86     Vector collPos =  collision->getEntityB()->getAbsCoor()  + box->center - ce->getCollisionPosition();
    87 
    88     // test the 3 axis differently
    89     switch( ce->getType())
     69    if( box == NULL)
    9070    {
    91         /* collision in the Y-AXIS */
    92       case COLLISION_TYPE_AXIS_Y_NEG:
    93         // calulate the height above ground
    94         height = collPos.len() - box->halfLength[1];
     71      PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n");
     72      return;
     73    }
    9574
    9675
    97         // object is beneath the plane (ground)
    98         //         if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
    99         if( height < 0.0f && -height < CR_MAX_WALK_HEIGHT)
    100         {
    101           entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
    102           entity->setOnGround(true);
    103         }
    104         // object is already in the wall
    105         else if( ce->isInWall())
    106         {
    107           entity->setAbsCoor(entity->getLastAbsCoor());
    108         }
    109         else
    110         {
    111           // entity is not on ground
    112           entity->setOnGround(false);
    113         }
    114         break;
     76    float CR_MAX_WALK_HEIGHT = 15.0f;
     77
     78    float height = 0.0f;
    11579
    11680
     81    std::vector<CollisionEvent*>::const_iterator it = collision->begin();
     82    for(; it != collision->end(); it++)
     83    {
     84
     85      CollisionEvent* ce = (*it);
     86      Vector normal = ce->getGroundNormal();
     87
     88      // calculate the collision position
     89      Vector collPos =  collision->getEntityA()->getAbsCoor()  + box->center - ce->getCollisionPosition();
     90
     91      // test the 3 axis differently
     92      switch( ce->getType())
     93      {
     94          /* collision in the Y-AXIS */
     95        case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_Y_NEG:
     96          // calulate the height above ground
     97          height = collPos.len() - box->halfLength[1];
     98
     99
     100          // object is beneath the plane (ground)
     101          //         if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing
     102          if( height < 0.0f && -height < CR_MAX_WALK_HEIGHT)
     103          {
     104            entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f));
     105            entity->setOnGround(true);
     106          }
     107          // object is already in the wall
     108          else if( ce->isInWall())
     109          {
     110            entity->setAbsCoor(entity->getLastAbsCoor());
     111          }
     112          else
     113          {
     114            // entity is not on ground
     115            entity->setOnGround(false);
     116          }
     117          break;
     118
     119
     120      }
    117121    }
     122    //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
     123
    118124  }
    119   //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);
    120 
    121 }
    122125
    123126
    124127
    125 
    126 /**
    127  * use this to do some collision offline calculations, only called for bContinuousPoll == true
    128  */
    129 void CRPhysicsGroundWalk::update(WorldEntity* owner)
    130 {}
    131 
    132 
     128}
  • trunk/src/lib/collision_reaction/cr_physics_ground_walk.h

    r9869 r10013  
    99#include "collision_reaction.h"
    1010
     11namespace CoRe
     12{
    1113
    12 class Collision;
     14  class Collision;
    1315
    14 //! A class representing a reaction to a collision: dealing damage to an object
    15 class CRPhysicsGroundWalk : public CollisionReaction
    16 {
    17   ObjectListDeclaration(CRPhysicsGroundWalk);
     16  //! A class representing a reaction to a collision: dealing damage to an object
     17  class CRPhysicsGroundWalk : public CollisionReaction
     18  {
     19    ObjectListDeclaration(CRPhysicsGroundWalk);
    1820  public:
    1921    CRPhysicsGroundWalk();
     
    2224    virtual void reactToCollision(Collision* collision);
    2325
    24     virtual void update(WorldEntity* entity);
     26  };
    2527
    26 
    27   private:
    28     float        downspeed;
    29 };
    30 
     28}
    3129#endif /* _CR_PHYSICS_GROUND_WALK_H */
Note: See TracChangeset for help on using the changeset viewer.