Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 8190 in orxonox.OLD for trunk/src


Ignore:
Timestamp:
Jun 7, 2006, 3:00:01 PM (19 years ago)
Author:
patrick
Message:

trunk: merged the cr branche to trunk

Location:
trunk/src
Files:
2 deleted
16 edited
9 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/defs/class_id.h

    r8186 r8190  
     1
    12/*
    23   orxonox - the future of 3D-vertical-scrollers
     
    253254  CL_COLLISION                  =    0x00000711,
    254255  CL_COLLISION_HANDLE           =    0x00000712,
     256  CL_COLLISION_REACTION         =    0X00000713,
     257  CL_CR_PHYSICS_MOMENTUM        =    0X00000714,
     258  CL_CR_PHYSICS_GROUND          =    0X00000715,
     259  CL_CR_PHYSICS_GROUND_WALK     =    0X00000716,
     260  CL_CR_OBJECT_DAMAGE          =    0X00000717,
     261  CL_CR_OBJECT_PICKUP           =    0X00000718,
     262  CL_CR_VERTEX_TRAFO            =    0X00000719,
     263  CL_CR_SPECIAL_CALLBACK        =    0X00000720,
     264
     265
    255266  CL_BV_TREE                    =    0x00701000,
    256267  CL_BV_TREE_NODE               =    0x00702000,
    257   CL_OBB_TREE                   =    0x00000714,
    258   CL_OBB_TREE_NODE              =    0x00000715,
     268  CL_OBB_TREE                   =    0x00000721,
     269  CL_OBB_TREE_NODE              =    0x00000722,
    259270  CL_BOUNDING_VOLUME            =    0x00710000,
    260271  CL_OBB                        =    0x00720000,
  • trunk/src/lib/BuildLibs.am

    r8145 r8190  
    1313                $(LIB_PREFIX)/particles/libORXparticles.a \
    1414                $(LIB_PREFIX)/collision_detection/libORXcd.a \
     15                $(LIB_PREFIX)/collision_reaction/libORXcr.a \
    1516                $(LIB_PREFIX)/network/libORXnet.a \
    1617                $(LIB_PREFIX)/graphics/spatial_separation/libORXquadtree.a \
  • trunk/src/lib/collision_detection/Makefile.am

    r5750 r8190  
    55
    66libORXcd_a_SOURCES = cd_engine.cc \
    7                      collision.cc \
    87                     obb.cc \
    98                     obb_tree.cc \
     
    1716noinst_HEADERS =     cd_engine.h \
    1817                     collision_defs.h \
    19                      collision.h \
    2018                     obb.h \
    2119                     obb_tree.h \
  • trunk/src/lib/collision_detection/obb_tree_node.cc

    r7736 r8190  
    481481    PRINTF(5)("collision @ lvl %i, object %s vs. %s, (%p, %p)\n", this->depth, nodeA->getClassName(), nodeB->getClassName(), this->nodeLeft, this->nodeRight);
    482482
    483     /* check if left node overlaps */
     483
     484    // left node
    484485    if( this->nodeLeft != NULL )
    485486    {
    486       PRINTF(5)("Checking OBB %i vs %i: ", this->nodeLeft->getIndex(), treeNode->getIndex());
    487487      if( this->overlapTest(this->nodeLeft->bvElement, treeNode->bvElement, nodeA, nodeB))
    488488      {
    489         this->nodeLeft->collideWith(treeNode->nodeLeft, nodeA, nodeB);
    490         this->nodeLeft->collideWith(treeNode->nodeRight, nodeA, nodeB);
     489        if( treeNode->nodeLeft != NULL)
     490          this->nodeLeft->collideWith(treeNode->nodeLeft, nodeA, nodeB);
     491        if( treeNode->nodeRight != NULL)
     492          this->nodeLeft->collideWith(treeNode->nodeRight, nodeA, nodeB);
    491493      }
    492494    }
    493     /* check if right node overlaps */
    494     if( likely( this->nodeRight != NULL))
    495     {
    496       PRINTF(5)("Checking OBB %i vs %i: ", this->nodeRight->getIndex(), treeNode->getIndex());
    497       if(this->overlapTest(this->nodeRight->bvElement, treeNode->bvElement, nodeA, nodeB))
     495
     496    // right node
     497    if( this->nodeRight != NULL )
     498    {
     499      if( this->overlapTest(this->nodeRight->bvElement, treeNode->bvElement, nodeA, nodeB))
    498500      {
    499         this->nodeRight->collideWith(treeNode->nodeLeft, nodeA, nodeB);
    500         this->nodeRight->collideWith(treeNode->nodeRight, nodeA, nodeB);
     501        if( treeNode->nodeLeft != NULL)
     502          this->nodeRight->collideWith(treeNode->nodeLeft, nodeA, nodeB);
     503        if( treeNode->nodeRight != NULL)
     504          this->nodeRight->collideWith(treeNode->nodeRight, nodeA, nodeB);
    501505      }
    502506    }
    503507
    504     /* so there is a collision and this is the last box in the tree (i.e. leaf) */
    505     if( unlikely((this->nodeRight == NULL || this->nodeLeft == NULL) ||
    506                  (treeNode->nodeRight == NULL || treeNode->nodeLeft == NULL)) )
    507     {
    508       nodeA->collidesWith(nodeB, treeNode->bvElement->center);
    509       nodeB->collidesWith(nodeA, this->bvElement->center);
     508
     509    // hybrid mode: we reached the end of this obbtree, now reach the end of the other tree
     510    if( this->nodeLeft == NULL && this->nodeRight == NULL)
     511    {
     512      if( treeNode->nodeLeft != NULL)
     513        this->collideWith(treeNode->nodeLeft, nodeA, nodeB);
     514      if( treeNode->nodeRight != NULL)
     515        this->collideWith(treeNode->nodeRight, nodeA, nodeB);
     516    }
     517
     518
     519    // now check if we reached the end of both trees
     520    if( unlikely((this->nodeRight == NULL && this->nodeLeft == NULL) &&
     521        (treeNode->nodeRight == NULL && treeNode->nodeLeft == NULL)) )
     522    {
     523      nodeA->registerCollision(nodeA, nodeB, (BoundingVolume*)this->bvElement, (BoundingVolume*)treeNode->bvElement);
    510524    }
    511525
  • trunk/src/lib/collision_reaction/Makefile.am

    r7927 r8190  
    55
    66libORXcr_a_SOURCES = cr_engine.cc \
    7                      collision_handle.cc
     7                     collision.cc \
     8                     collision_event.cc \
     9                     collision_handle.cc \
     10                     collision_reaction.cc \
     11                     cr_object_damage.cc
    812
    913
    1014
    1115noinst_HEADERS =     cr_engine.h \
    12                                                                                  collision_handle.h
     16                     collision.h \
     17                     collision_event.h \
     18                     collision_handle.h \
     19                     cr_defs.h \
     20                     collision_reaction.h \
     21                     cr_object_damage.h
    1322
  • trunk/src/lib/collision_reaction/collision_handle.cc

    r7927 r8190  
    1717#include "collision_handle.h"
    1818
     19#include "world_entity.h"
     20
     21#include "collision.h"
     22#include "collision_event.h"
     23#include "collision_reaction.h"
     24
     25#include "cr_object_damage.h"
     26
    1927using namespace std;
    2028
     
    3139  this->type = type;
    3240
     41  this->bCollided = false;
     42  this->bDispatched = false;
     43
     44  if( this->type == CREngine::CR_PHYSICS_STEP_BACK)
     45    this->bContinuousPoll = false;
     46  else
     47    this->bContinuousPoll = true;
     48
     49  if( this->type == CREngine::CR_OBJECT_DAMAGE)
     50    this->bStopOnFirstCollision = true;
     51  else
     52   this->bStopOnFirstCollision = false;
     53
     54  switch( type)
     55  {
     56    case CREngine::CR_OBJECT_DAMAGE:
     57      this->collisionReaction = new CRObjectDamage();
     58      break;
     59    default:
     60      break;
     61  };
    3362}
    3463
     
    4170  // delete what has to be deleted here
    4271}
     72
     73/**
     74 * restores the CollisionHandle to its initial state
     75 */
     76void CollisionHandle::reset()
     77{
     78  this->flushCollisions();
     79}
     80
     81
     82/**
     83 * add more filter targets to this collision handle
     84 *  @param classID the classid to look for
     85 */
     86void CollisionHandle::addTarget(long target)
     87{
     88  // make sure there is no dublicate
     89  std::vector<long>::iterator it = this->targetList.begin();
     90  for( ; it < this->targetList.end(); it++)
     91    if( (*it) == target)
     92      return;
     93
     94  // add element
     95   PRINTF(0)("addTarget: %i \n", target);
     96   this->targetList.push_back(target);
     97}
     98
     99
     100/**
     101 * registers a new Collision Object
     102 *  @param entityA WorldEntity A of the collision
     103 *  @param entityB WorldEntity B of the collision
     104 * if a there is already a collision object with the same stats
     105 * registration will be skipped and the last collision object is returned
     106 */
     107Collision* CollisionHandle::registerCollision(WorldEntity* entityA, WorldEntity* entityB)
     108{
     109  //first get the collision object, multiple sources
     110  Collision* c;
     111  if( this->collisionList.empty() ||
     112      ((this->collisionList.back())->getEntityA() != entityA && (this->collisionList.back())->getEntityB() != entityB )) {
     113    c = CREngine::getInstance()->popCollisionObject();
     114    c->collide(entityA, entityB);
     115    this->collisionList.push_back(c);
     116
     117    // now register it as a shared collision with the other collision entity
     118    CollisionHandle* ch = entityB->getCollisionHandle(this->type);
     119    if( ch != NULL)
     120      ch->registerSharedCollision(c);
     121  }
     122  else
     123    c = this->collisionList.back();
     124
     125  return c;
     126}
     127
     128
     129/**
     130 * register a Collision to the Collision handle.
     131 *  @param collision the collision object to register
     132 *
     133 * This is used for internal collision registration: sharing the collision objects between Collision Reactions
     134 * Therefore dispatching it only once
     135 */
     136void CollisionHandle::registerSharedCollision(Collision* collision)
     137{
     138  // fist check if we are listening for this Collision
     139  if( !this->filterCollision(collision))
     140    return;
     141
     142  // set the state to not dispatched
     143  this->bDispatched = false;
     144  this->bCollided = true;
     145  collision->setEntityBCollide(true);
     146
     147  this->collisionList.push_back(collision);
     148}
     149
     150
     151/**
     152 * this is the function to be called on a collision event for this handle
     153 *  @param collision the collision objects containing all collision informations
     154 */
     155void CollisionHandle::registerCollisionEvent(CollisionEvent* collisionEvent)
     156{
     157  if( !this->filterCollisionEvent(collisionEvent))
     158    return;
     159
     160  // set the state to not dispatched
     161  this->bDispatched = false;
     162  this->bCollided = true;
     163
     164  // checks if these WorldEntities have already collided or if its a new collision -> create a new Collision object
     165 Collision* c = this->registerCollision(collisionEvent->getEntityA(), collisionEvent->getEntityB());
     166 c->setEntityACollide(true);
     167
     168 c->registerCollisionEvent(collisionEvent);
     169}
     170
     171
     172/**
     173 * flushes the collision list
     174 */
     175void CollisionHandle::flushCollisions()
     176{
     177  this->collisionList.clear();
     178}
     179
     180
     181/**
     182 * handles the collisions and react according to algorithm
     183 */
     184void CollisionHandle::handleCollisions()
     185{
     186  // collision reaction calculations (for every collision there will be a reaction)
     187  vector<Collision*>::iterator it = this->collisionList.begin();
     188  for(; it < this->collisionList.end(); it++) {
     189    if( !(*it)->isDispatched())
     190    {
     191      this->collisionReaction->reactToCollision(*it);
     192      (*it)->flushCollisionEvents();
     193    }
     194  }
     195
     196  // now set state to dispatched
     197  this->bDispatched = true;
     198  this->bCollided = false;
     199
     200  this->flushCollisions();
     201}
     202
     203
     204/**
     205 * filter out the CollisionEvents that are not wanted
     206 *  @param collisionEvent the collision event to filter
     207 */
     208bool CollisionHandle::filterCollisionEvent(CollisionEvent* collisionEvent)
     209{
     210  vector<long>::iterator it = this->targetList.begin();
     211  for(; it < this->targetList.end(); it++)
     212  {
     213    if( collisionEvent->getEntityA() == this->owner) {
     214      if( collisionEvent->getEntityA()->isA((ClassID)(*it)))
     215        return true; }
     216    else {
     217      if( collisionEvent->getEntityB()->isA((ClassID)(*it)))
     218        return true; }
     219  }
     220
     221  return false;
     222}
     223
     224
     225/**
     226 * filter Collisions that are not wanted to be reacted to
     227 *  @param collision the collision object to filter
     228 */
     229bool CollisionHandle::filterCollision(Collision* collision)
     230{
     231  vector<long>::iterator it = this->targetList.begin();
     232  for(; it < this->targetList.end(); it++)
     233  {
     234    if( collision->getEntityA() == this->owner) {
     235      if( collision->getEntityA()->isA((ClassID)(*it)))
     236        return true; }
     237      else {
     238        if( collision->getEntityB()->isA((ClassID)(*it)))
     239          return true; }
     240  }
     241
     242  return false;
     243}
     244
     245
     246
     247
     248
     249
     250
  • trunk/src/lib/collision_reaction/collision_handle.h

    r7927 r8190  
    1111
    1212#include <vector>
     13#include <list>
    1314
    1415
    1516class Collision;
    1617class WorldEntity;
     18class CollisionReaction;
    1719
    18 // struct CRType;
    1920
    2021
    2122//! A class for defining collision reactions and storing events
    22 class CollisionHandle : public BaseObject {
     23class CollisionHandle : public BaseObject
     24{
    2325
    24  public:
    25   CollisionHandle(WorldEntity* owner, CREngine::CRType type);
    26   virtual ~CollisionHandle();
     26  public:
     27    CollisionHandle(WorldEntity* owner, CREngine::CRType type);
     28    virtual ~CollisionHandle();
     29
     30    void reset();
     31
     32    void addTarget(long target);
     33    Collision* registerCollision(WorldEntity* entityA, WorldEntity* entityB);
     34    void registerSharedCollision(Collision* collision);
     35    void registerCollisionEvent(CollisionEvent* collisionEvent);
     36
     37    /** @returns true if regiestered some new collision events in this tick frame */
     38    inline bool isCollided() const { return this->bCollided; }
     39    /** @returns true if this collision handle has already been dispatched */
     40    inline bool isDispatched() const { return this->bDispatched; }
     41    /** @returns true if this handle should be pulled also if there are no collisions */
     42    inline bool isContinuousPoll() const { return this->bContinuousPoll; }
     43
     44    void handleCollisions();
    2745
    2846
    29   void addTarget();
    30 
    31   void registerCollision(Collision* collision);
    32 
    33   void handleCollisions();
     47  private:
     48    void flushCollisions();
     49    bool filterCollisionEvent(CollisionEvent* collisionEvent);
     50    bool filterCollision(Collision* collision);
    3451
    3552
    36  private:
    37    WorldEntity*                  owner;                   //!< the worldenity this reaction will be applied on
    38    CREngine::CRType              type;                    //!< the reaction type
    3953
    40    bool                          bDispatched;             //!< true if this handle has already been dispatched
    41    bool                          bStopOnFirstCollision;   //!< true if the cd of this object should be terminated after one match
     54  private:
     55    WorldEntity*                  owner;                   //!< the worldenity this reaction will be applied on
     56    CREngine::CRType              type;                    //!< the reaction type
    4257
    43    std::vector<Collision*>       collisionList;           //!< a list full of collisions
    44    std::vector<long>             targetList;              //!< a list of target classes for filtering
     58    bool                          bContinuousPoll;         //!< if this is true
     59    bool                          bDispatched;             //!< true if this handle has already been dispatched
     60    bool                          bStopOnFirstCollision;   //!< true if the cd of this object should be terminated after one match
     61    bool                          bCollided;               //!< true if the CollsionHandle has registered some new collisions
     62
     63    std::vector<Collision*>       collisionList;           //!< a list full of collisions
     64    std::vector<long>             targetList;              //!< a list of target classes for filtering
     65
     66    CollisionReaction*            collisionReaction;       //!< reference to the collision reaction object
    4567
    4668};
  • trunk/src/lib/collision_reaction/cr_engine.cc

    r7927 r8190  
    1616#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION
    1717
     18
     19
     20#include "collision.h"
     21#include "collision_event.h"
     22#include "collision_handle.h"
     23#include "cr_defs.h"
     24
    1825#include "cr_engine.h"
    1926
     
    2532 */
    2633CREngine::CREngine ()
     34  : BaseObject()
    2735{
    2836   this->setClassID(CL_CR_ENGINE, "CREngine");
    2937   this->setName("CREngine");
    3038
     39   this->init();
    3140}
    3241
     
    4251{
    4352  CREngine::singletonRef = NULL;
     53
     54  if( this->collisionsUnused.size() != CR_MAX_COLLISIONS)
     55    PRINTF(0)("CollisionReaction Error: Collision cache size missmatch: %i of %i\n", this->collisionsUnused.size(), CR_MAX_COLLISIONS);
     56  if( this->collisionEventsUnused.size() != CR_MAX_COLLISION_EVENTS)
     57    PRINTF(0)("CollisionReaction Error: CollisionEvent cache size missmatch: %i of %i\n", this->collisionEventsUnused.size(), CR_MAX_COLLISION_EVENTS);
     58
     59  this->reset();
     60
     61  vector<Collision*>::iterator it1 = this->collisionsUnused.begin();
     62  for(; it1 < this->collisionsUnused.end(); it1++)
     63    delete *it1;
     64  vector<CollisionEvent*>::iterator it2 = this->collisionEventsUnused.begin();
     65  for(; it2 < this->collisionEventsUnused.end(); it2++)
     66    delete *it2;
     67
     68  this->collisionsUnused.clear();
     69  this->collisionEventsUnused.clear();
     70}
     71
     72/**
     73 * inits the CREngine to a working state
     74 */
     75void CREngine::init()
     76{
     77  // create a list of Collision events (precaching)
     78  for( int i = 0; i < CR_MAX_COLLISIONS; i++)
     79    this->collisionsUnused.push_back(new Collision());
     80  for( int i = 0; i < CR_MAX_COLLISION_EVENTS; i++)
     81    this->collisionEventsUnused.push_back(new CollisionEvent());
    4482}
    4583
    4684
     85/**
     86 * flushes the CollisionHandles and restores the CREngine to the initial state
     87 */
     88void CREngine::reset()
     89{
     90  // first clear all CollisionHandles
    4791
     92  vector<CollisionHandle*>::iterator it = this->collisionHandles.begin();
     93  for(; it < this->collisionHandles.end(); it++)
     94  {
     95    (*it)->reset();
     96    delete *it;
     97  }
    4898
    49 CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type, int nrOfTargets, ...)
    50 {
    51 
    52   va_list itemlist;
    53   va_start (itemlist, type);
    54 //  for (int i = 0; i < nrOfTargets; i++)
    55   //  this->targetList.push_back(va_arg(itemlist, int));
    56   va_end(itemlist);
     99  this->collisionHandles.clear();
    57100}
    58101
    59102
     103/**
     104 * subscribes a WorldEntity for a CollisionReaction
     105 *  @param owner: the WE to subscribe
     106 *  @param type: the type of collision reaction to perform
     107 *  @return the newly created CollisionHandle
     108 */
     109CollisionHandle* CREngine::subscribeReaction(WorldEntity* owner, CRType type)
     110{
     111  CollisionHandle* ch = new CollisionHandle(owner, type);
     112  this->collisionHandles.push_back(ch);
     113
     114  return ch;
     115}
    60116
    61117
     118/**
     119 * unsubscribe reaction from the reaction list
     120 *  @param collisionHandle the CollisionHandle to remove
     121 *  @param returns true if worked collrectly
     122 */
     123bool CREngine::unsubscribeReaction(CollisionHandle* collisionHandle)
     124{
     125  std::vector<CollisionHandle*>::iterator it;
     126  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
     127  {
     128    if( *it == collisionHandle)
     129    {
     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 */
     142void CREngine::handleCollisions()
     143{
     144  std::vector<CollisionHandle*>::iterator it;
     145  for( it = this->collisionHandles.begin(); it != this->collisionHandles.end(); it++)
     146  {
     147    if( (*it)->isCollided() || (*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 */
     159void CREngine::flushCollisions()
     160{
     161  vector<Collision*>::iterator it1 = this->collisionsUsed.begin();
     162  for(; it1 < this->collisionsUsed.end(); it1++)
     163    this->collisionsUnused.push_back(*it1);
     164
     165  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
     174void CREngine::debug()
     175{
     176
     177}
     178
  • trunk/src/lib/collision_reaction/cr_engine.h

    r8145 r8190  
    22 * @file cr_engine.h
    33 * @brief The collision reaction engine, defining generic collision reactions to collision events
    4 */
     4 *
     5 * some parts of this module are tuned for efficiency. They are probably not self-explenatory anymore :D
     6 *  - Collision/ CollisionEvent objects recycling: This class contains a class of precached objects of these types
     7 *      they are used for fast registration of collision events: These objects can be get by the interface functions and
     8 *      are returned after one cycle automaticly by reseting the cached lists to its initial state. So do not wonder :D
     9 */
    510
    611#ifndef _CR_ENGINE_
     
    813
    914#include "base_object.h"
    10 #include <stdarg.h>
     15
    1116#include <vector>
    1217
     
    1419class CollisionHandle;
    1520class Collision;
     21class CollisionEvent;
    1622class WorldEntity;
    1723
     
    2228  public:
    2329  typedef enum CRType {
    24     CR_PHYSICS_MOMENTUM   = 0,
    25     CR_PHYSICS_GROUND,
    26     CR_PHYSICS_GROUND_WALK,
     30    CR_PHYSICS_MOMENTUM   = 0,    //!< physical reaction: conservervation of momentum
     31    CR_PHYSICS_STEP_BACK,         //!< physical reaction: just go to the last position without collisions
     32    CR_PHYSICS_GROUND,            //!< physical reaction: stand on the ground, no movement: simulating simple normal force away from the gravity force
     33    CR_PHYSICS_GROUND_WALK,       //!< physical reaction: walking on the ground (inkl. hills etc)
     34    CR_PHYSICS_DAMAGE,            //!< physical reaction: daling damage according to the object energy and their structural stability
    2735
    28     CR_OBJECT_DAMAGE,
    29     CR_OBJECT_PICKUP,
     36    CR_OBJECT_DAMAGE,             //!< object raction: deals damage according to the objects specific damage potential (like weapons, nukes, etc.)
     37    CR_OBJECT_PICKUP,             //!< object rection: calling the objects pickup functions, let them handle the collision (once!)
    3038
    31     CR_VERTEX_TRAFO,
     39    CR_VERTEX_TRAFO,              //!< vertex trafo: transforming the vertex according to the damage
    3240
    33     CR_SPECIAL_CALLBACK,
     41    CR_SPECIAL_CALLBACK,          //!< special: call a callback function
    3442
    3543    CR_NUMBER
     
    3947
    4048  /** @returns a Pointer to the only object of this Class */
    41   inline static CREngine* getInstance(void) { if (!singletonRef) singletonRef = new CREngine();  return singletonRef; };
     49  inline static CREngine* getInstance() { if (!singletonRef) singletonRef = new CREngine();  return singletonRef; };
     50
     51  void reset();
    4252
    4353
    44   CollisionHandle* subscribeReaction(WorldEntity* worldEntity, CRType type, int nrOfTargets, ...);
     54  CollisionHandle* subscribeReaction(WorldEntity* worldEntity, CRType type);
    4555
    46   bool unsubscribeReaction(WorldEntity* worldEntity);
    4756  bool unsubscribeReaction(CollisionHandle* collisionHandle);
    48 
    4957
    5058  void handleCollisions();
    5159
    5260  /** @returns an instance to a collision object. instead of creating new object this ones can be resycled */
    53   inline Collision* getCollisionObject() { /* return the first element of the cache list*/ }
    54   /** @param collision: returns the Collision object back to the cache list */
    55   inline void putCollisionObject(Collision* collision) { this->cachedCollisions.push_back(collision); }
     61  inline Collision* popCollisionObject() {
     62    if( !this->collisionsUnused.empty()) {
     63      this->collisionsUsed.push_back(this->collisionsUnused.back()); this->collisionsUnused.pop_back(); return this->collisionsUsed.back(); } else return NULL; }
     64
     65
     66  /** @return an instanco of a CollisionEvent object. instead of creating a new object this ones can be used and resycled */
     67  inline CollisionEvent* popCollisionEventObject() {
     68    if( !this->collisionEventsUnused.empty()) {
     69      this->collisionEventsUsed.push_back(this->collisionEventsUnused.back()); this->collisionEventsUnused.pop_back(); return this->collisionEventsUsed.back(); } else return NULL; }
     70
     71  void debug();
    5672
    5773
    5874private:
    59   CREngine(void);
     75  CREngine();
     76  void init();
     77
     78  void flushCollisions();
    6079
    6180
    6281private:
    6382  std::vector<CollisionHandle*>       collisionHandles;         //!< list with the collision handles
    64   std::vector<Collision*>             cachedCollisions;         //!< a list of unused, cached collision events
     83
     84  std::vector<Collision*>             collisionsUsed;           //!< a list of used, cached collisions
     85  std::vector<Collision*>             collisionsUnused;         //!< a list of unused, cached collisions
     86
     87  std::vector<CollisionEvent*>        collisionEventsUsed;      //!< a list of used, cached collision events
     88  std::vector<CollisionEvent*>        collisionEventsUnused;    //!< a list of unused, cached collision events
    6589
    6690  static CREngine*                    singletonRef;             //!< the reference to the CREngine object (singleton)
  • trunk/src/lib/graphics/effects/atmospheric_engine.cc

    r7836 r8190  
    127127    for (it = weatherEffects->begin(); it != weatherEffects->end(); it++)
    128128    {
    129       printf("%s::%s \n", (*it)->getClassName(), (*it)->getName());
     129/*      printf("%s::%s \n", (*it)->getClassName(), (*it)->getName());*/
    130130      dynamic_cast<WeatherEffect*>(*it)->tick(dt);
    131131    }
  • trunk/src/lib/physics/physics_interface.h

    r6616 r8190  
    3939  inline float getTotalMass( void ) const { return mass + massChildren; };
    4040
     41  /** @returns the velocity */
     42  inline const Vector getVelocity() { return this->velocity; }
     43  /** @returns the acceleration */
     44  inline const Vector getAcceleration() { return this->acceleration; }
     45
    4146  virtual void applyForce(const Vector& force);
    4247  virtual void applyField(Field* field);
     
    5156  float          massChildren;          //!< Sum of the masses of the children nodes
    5257
     58  Vector         velocity;              //!< the velocity of the masspoint
     59  Vector         acceleration;          //!< acceleration of the masspoint
     60
    5361  Vector         forceSum;              //!< Total central force for this tick
    5462  Quaternion     momentumSum;           //!< Total momentum in this tick
  • trunk/src/story_entities/game_world.cc

    r8037 r8190  
    136136  AnimationPlayer::getInstance();
    137137  PhysicsEngine::getInstance();
    138 
     138  CREngine::getInstance();
    139139}
    140140
     
    199199  delete AnimationPlayer::getInstance();
    200200  delete PhysicsEngine::getInstance();
     201  delete CREngine::getInstance();
    201202
    202203  State::setCurrentStoryEntity(NULL);
     
    278279    /* process time */
    279280    this->tick ();
    280     /* process collision */
    281     this->collide ();
     281    /* collision detection */
     282    this->collisionDetection ();
     283    /* collision reaction */
     284    this->collisionReaction ();
    282285    /* update the state */
    283286    this->update ();
     
    408411 * kicks the CDEngine to detect the collisions between the object groups in the world
    409412 */
    410 void GameWorld::collide()
     413void GameWorld::collisionDetection()
    411414{
    412415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
     
    422425      this->dataTank->objectManager->getObjectList(OM_COMMON));
    423426
     427}
     428
     429
     430void GameWorld::collisionReaction()
     431{
     432  CREngine::getInstance()->handleCollisions();
    424433}
    425434
  • trunk/src/story_entities/game_world.h

    r7919 r8190  
    6868    virtual void update();
    6969    virtual void checkGameRules();
    70     virtual void collide();
     70    virtual void collisionDetection();
     71    virtual void collisionReaction();
    7172
    7273    void drawEntityList(const ObjectManager::EntityList& drawList ) const;
  • trunk/src/world_entities/projectiles/projectile.cc

    r7460 r8190  
    3838  this->target = NULL;
    3939  this->removeNode();
     40
     41  /* character attributes */
     42  this->setHealth(1.0f);
     43  this->setDamage(100.0f); // default damage of a projectile set to 100.0 damage points
    4044
    4145  this->explosionBuffer = NULL;
  • trunk/src/world_entities/world_entity.cc

    r8186 r8190  
    3232#include "camera.h"
    3333
    34 #include "cr_engine.h"
    3534#include "collision_handle.h"
     35#include "collision_event.h"
     36
     37#include <stdarg.h>
    3638
    3739
     
    5961  this->healthMax = 1.0f;
    6062  this->health = 1.0f;
     63  this->damage = 0.0f; // no damage dealt by a default entity
    6164  this->scaling = 1.0f;
    6265
     
    6871  this->objectListIterator = NULL;
    6972
     73  // reset all collision handles to NULL == unsubscribed state
     74  for(int i = 0; i < CREngine::CR_NUMBER; ++i)
     75    this->collisionHandles[i] = NULL;
     76  this->bReactive = false;
     77
     78  // registering default reactions:
     79  this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, CL_WORLD_ENTITY);
     80  this->subscribeReaction(CREngine::CR_PHYSICS_GROUND, CL_TERRAIN);
     81
    7082  this->toList(OM_NULL);
    7183
     
    91103  if (this->healthWidget != NULL)
    92104    delete this->healthWidget;
     105
     106  this->unsubscribeReaction();
    93107}
    94108
     
    249263 * subscribes this world entity to a collision reaction
    250264 *  @param type the type of reaction to subscribe to
     265 *  @param target1 a filter target (classID)
     266 */
     267void WorldEntity::subscribeReaction(CREngine::CRType type, long target1)
     268{
     269  this->subscribeReaction(type);
     270
     271  // add the target filter
     272  this->collisionHandles[type]->addTarget(target1);
     273}
     274
     275
     276/**
     277 * subscribes this world entity to a collision reaction
     278 *  @param type the type of reaction to subscribe to
     279 *  @param target1 a filter target (classID)
     280 */
     281void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2)
     282{
     283  this->subscribeReaction(type);
     284
     285  // add the target filter
     286  this->collisionHandles[type]->addTarget(target1);
     287  this->collisionHandles[type]->addTarget(target2);
     288}
     289
     290
     291/**
     292 * subscribes this world entity to a collision reaction
     293 *  @param type the type of reaction to subscribe to
     294 *  @param target1 a filter target (classID)
     295 */
     296void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3)
     297{
     298  this->subscribeReaction(type);
     299
     300  // add the target filter
     301  this->collisionHandles[type]->addTarget(target1);
     302  this->collisionHandles[type]->addTarget(target2);
     303  this->collisionHandles[type]->addTarget(target3);
     304}
     305
     306
     307/**
     308 * subscribes this world entity to a collision reaction
     309 *  @param type the type of reaction to subscribe to
     310 *  @param target1 a filter target (classID)
     311 */
     312void WorldEntity::subscribeReaction(CREngine::CRType type, long target1, long target2, long target3, long target4)
     313{
     314  this->subscribeReaction(type);
     315
     316  // add the target filter
     317  this->collisionHandles[type]->addTarget(target1);
     318  this->collisionHandles[type]->addTarget(target2);
     319  this->collisionHandles[type]->addTarget(target3);
     320  this->collisionHandles[type]->addTarget(target4);
     321}
     322
     323
     324/**
     325 * subscribes this world entity to a collision reaction
     326 *  @param type the type of reaction to subscribe to
    251327 *  @param nrOfTargets number of target filters
    252328 *  @param ... the targets as classIDs
    253329 */
    254 void WorldEntity::subscribeReaction(CREngine::CRType type, int nrOfTargets, ...)
    255 {}
     330void WorldEntity::subscribeReaction(CREngine::CRType type)
     331{
     332  if( this->collisionHandles[type] != NULL)  {
     333    PRINTF(2)("Registering for a CollisionReaction already subscribed to! Skipping\n");
     334    return;
     335  }
     336
     337  this->collisionHandles[type] = CREngine::getInstance()->subscribeReaction(this, type);
     338
     339  // now there is at least one collision reaction subscribed
     340  this->bReactive = true;
     341}
     342
     343
     344/**
     345 * unsubscribes a specific reaction from the worldentity
     346 *  @param type the reaction to unsubscribe
     347 */
     348void WorldEntity::unsubscribeReaction(CREngine::CRType type)
     349{
     350  if( this->collisionHandles[type] == NULL)
     351    return;
     352
     353  CREngine::getInstance()->unsubscribeReaction(this->collisionHandles[type]);
     354  this->collisionHandles[type] = NULL;
     355
     356  // check if there is still any handler registered
     357  for(int i = 0; i < CREngine::CR_NUMBER; ++i)
     358  {
     359    if( this->collisionHandles[i] != NULL)
     360    {
     361      this->bReactive = true;
     362      return;
     363    }
     364  }
     365  this->bReactive = false;
     366}
     367
     368
     369/**
     370 * unsubscribes all collision reactions
     371 */
     372void WorldEntity::unsubscribeReaction()
     373{
     374  for( int i = 0; i < CREngine::CR_NUMBER; i++)
     375    this->unsubscribeReaction((CREngine::CRType)i);
     376
     377  // there are no reactions subscribed from now on
     378  this->bReactive = false;
     379}
     380
     381
     382/**
     383 * registers a new collision event to this world entity
     384 *  @param entityA entity of the collision
     385 *  @param entityB entity of the collision
     386 *  @param bvA colliding bounding volume of entityA
     387 *  @param bvB colliding bounding volume of entityA
     388 */
     389bool WorldEntity::registerCollision(WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB)
     390{
     391  // is there any handler listening?
     392  if( !this->bReactive)
     393    return false;
     394
     395  // get a collision event
     396  CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject();
     397  assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h
     398  c->collide(entityA, entityB, bvA, bvB);
     399
     400  for( int i = 0; i < CREngine::CR_NUMBER; ++i)
     401    if( this->collisionHandles[i] != NULL)
     402      this->collisionHandles[i]->registerCollisionEvent(c);
     403}
     404
     405
     406/**
     407 * registers a new collision event to this woeld entity
     408 *  @param entity the entity that collides
     409 *  @param plane it stands on
     410 *  @param position it collides on the plane
     411 */
     412bool WorldEntity::registerCollision(WorldEntity* entity, Plane* plane, Vector position)
     413{
     414  // is there any handler listening?
     415  if( !this->bReactive)
     416    return false;
     417
     418  // get a collision event
     419  CollisionEvent* c = CREngine::getInstance()->popCollisionEventObject();
     420  assert(c != NULL); // if this should fail: we got not enough precached CollisionEvents: alter value in cr_defs.h
     421  c->collide(entity, plane, position);
     422
     423  for( int i = 0; i < CREngine::CR_NUMBER; ++i)
     424    if( this->collisionHandles[i] != NULL)
     425      this->collisionHandles[i]->registerCollisionEvent(c);
     426}
    256427
    257428
     
    324495void WorldEntity::collidesWithGround(const Vector& feet, const Vector& ray_1, const Vector& ray_2)
    325496{
    326  
     497
    327498  // PRINTF(0)("BSP_GROUND: Player collides \n", this->getClassName() );
    328  
     499
    329500  Vector v = this->getAbsDirX();
    330501  v.x *= 10;
     
    332503  v.z *= 10;
    333504  Vector u = this->getAbsDirY();
    334  
     505
    335506  if(feet.x == (u.x+this->getAbsCoor().x) &&  feet.y == u.y +this->getAbsCoor().y && feet.z == this->getAbsCoor().z)
    336507  {
    337    
     508
    338509  this->setAbsCoor(ray_2 - v);
    339510  }
     
    342513    if(ray_1.x == this->getAbsCoor().x + v.x && ray_1.y == this->getAbsCoor().y + v.y + 0.1 && ray_1.z ==this->getAbsCoor().z + v.z)
    343514    {
    344       this->setAbsCoor(feet -u ); 
    345     }
    346      
    347     this->setAbsCoor(ray_2 - v); 
    348    
     515      this->setAbsCoor(feet -u );
     516    }
     517
     518    this->setAbsCoor(ray_2 - v);
     519
    349520  }
    350521}
  • trunk/src/world_entities/world_entity.h

    r8186 r8190  
    1515#include "glincl.h"
    1616#include <vector>
    17 #include <stdarg.h>
     17
     18#include "physics_interface.h"
    1819
    1920
     
    2425
    2526class BVTree;
     27class BoundingVolume;
    2628class Model;
    2729class CollisionHandle;
     30class Collision;
     31class Plane;
    2832
    2933
     
    7276
    7377  /* --- Collision Reaction Block --- */
    74   void subscribeReaction(CREngine::CRType type, int nrOfTargets, ...);
     78  void subscribeReaction(CREngine::CRType type);
     79  void subscribeReaction(CREngine::CRType type, long target1);
     80  void subscribeReaction(CREngine::CRType type, long target1, long target2);
     81  void subscribeReaction(CREngine::CRType type, long target1, long target2, long target3);
     82  void subscribeReaction(CREngine::CRType type, long target1, long target2, long target3, long target4);
     83
     84  void unsubscribeReaction(CREngine::CRType type);
     85  void unsubscribeReaction();
     86
     87  bool registerCollision(WorldEntity* entityA, WorldEntity* entityB, BoundingVolume* bvA, BoundingVolume* bvB);
     88  bool registerCollision(WorldEntity* entity, Plane* plane, Vector position);
     89  /** @return true if there is at least on collision reaction subscribed */
     90  inline bool isReactive() const { return this->bReactive; }
     91
     92  CollisionHandle* getCollisionHandle(CREngine::CRType type) const { return this->collisionHandles[type]; }
    7593
    7694
     
    98116
    99117  /* --- Character Attribute Block --- */
     118  /** @returns the damage dealt by this world entity */
     119  float getDamage() const { return this->damage; }
     120  /** sets the damage dealt to @param damage damage per second */
     121  void setDamage(float damage) { this->damage = damage; }
    100122  /** @returns the Energy of the entity */
    101123  float getHealth() const { return this->health; };
     
    107129  OrxGui::GLGuiWidget* getHealthWidget();
    108130  bool hasHealthWidget() const { return this->healthWidget; };
    109  
     131
    110132  virtual void varChangeHandler( std::list<int> & id );
     133
     134
     135  //FIXME: the PhysicsInterface and the whole PEngine should probably be reviewed. Here its just used to store the vars
     136  /* --- Physics Interface --- */
     137  inline PhysicsInterface getPhysicsInterface() const { return this->physicsInterface; }
     138  inline float getMass() const { return this->physicsInterface.getMass(); }
     139  inline float getTotalMass() const { return this->physicsInterface.getTotalMass(); }
     140
    111141
    112142  /* --- Misc Stuff Block --- */
     
    128158private:
    129159  /// TODO maybe we will move the following three entries and the corresponding functions to Playable AND NPC
     160  float                   damage;             //!< the damage dealt to other objects by colliding.
    130161  float                   health;             //!< The Energy of this Entity, if the Entity has any energy at all.
    131162  float                   healthMax;          //!< The Maximal energy this entity can take.
     
    140171  bool                    bVisible;           //!< If it should be visible.
    141172
    142   OM_LIST                           objectListNumber;   //!< The ObjectList from ObjectManager this Entity is in.
    143   ObjectManager::EntityList::iterator objectListIterator; //!< The iterator position of this Entity in the given list of the ObjectManager.
     173  OM_LIST                 objectListNumber;                //!< The ObjectList from ObjectManager this Entity is in.
     174  ObjectManager::EntityList::iterator objectListIterator;  //!< The iterator position of this Entity in the given list of the ObjectManager.
    144175
    145  
    146   //network stuff
    147  
    148   float       scaling;                         //!< model's scaling factor
    149   int         scaling_handle;                  //!< handle for syncing var
    150  
    151   std::string modelFileName;                  //!< model's file name
    152   int         modelFileName_handle;           //!< handle for syncing var
    153   CollisionHandle**       collisionHandles;
    154176
     177
     178  float                   scaling;                         //!< model's scaling factor
     179  int                     scaling_handle;                  //!< handle for syncing var
     180
     181  std::string             modelFileName;                   //!< model's file name
     182  int                     modelFileName_handle;            //!< handle for syncing var
     183
     184  CollisionHandle*        collisionHandles[CREngine::CR_NUMBER];  //!< the list of the collision reactions
     185  bool                    bReactive;                              //!< true if there is at least one collision reaction subscibed
     186
     187  PhysicsInterface        physicsInterface;                //!< the physics object of the WorldEntity
    155188
    156189};
Note: See TracChangeset for help on using the changeset viewer.