Changeset 10013 in orxonox.OLD for trunk/src/lib/collision_reaction
- Timestamp:
- Dec 4, 2006, 6:42:46 PM (18 years ago)
- 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 5 5 6 6 libORXcr_a_SOURCES = cr_engine.cc \ 7 collision_tube.cc \ 7 8 collision.cc \ 8 9 collision_event.cc \ 9 collision_ handle.cc \10 collision_filter.cc \ 10 11 collision_reaction.cc \ 11 12 cr_object_damage.cc \ … … 16 17 17 18 noinst_HEADERS = cr_engine.h \ 19 collision_tube.h \ 18 20 collision.h \ 19 21 collision_event.h \ 20 collision_ handle.h \22 collision_filter.h \ 21 23 cr_defs.h \ 22 24 collision_reaction.h \ -
trunk/src/lib/collision_reaction/collision.cc
r9869 r10013 19 19 #include "debug.h" 20 20 21 /** 22 * standard constructor 23 */ 24 Collision::Collision () 21 namespace CoRe 25 22 { 26 this->flushCollisionEvents(); 27 } 23 24 /** 25 * standard constructor 26 */ 27 Collision::Collision () 28 { 29 this->reset(); 30 } 28 31 29 32 30 /** 31 * standard deconstructor 32 */ 33 Collision::~Collision () 34 { 35 } 33 /** 34 * standard deconstructor 35 */ 36 Collision::~Collision () 37 {} 36 38 37 39 38 /**39 * flushes the CollisionEvent list40 */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; 45 47 46 this->entityACollide = false;47 this->entityBCollide = false;48 this->_entityACollide = false; 49 this->_entityBCollide = false; 48 50 49 this->bDispatched = true; 51 this->_collisionEvents.clear(); 52 } 50 53 51 this->collisionEvents.clear();52 54 } 53 -
trunk/src/lib/collision_reaction/collision.h
r9869 r10013 1 1 /*! 2 2 * @file collision.h 3 * Definition of a collision as a two WE hit each other3 * Definition of a collision relation of two WorldEntities 4 4 * 5 * A is shared between two WorldEntity's CollisionHandles if both are subscribed to this event. In this case only one5 * Is shared between two WorldEntity's CollisionHandles if both are subscribed to this event. In this case only one 6 6 * of the two CollisionHandles will calculate the CollisionReaction and the bDispatched flag will be set afterwards 7 7 * 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. 8 12 */ 9 13 … … 14 18 #include <vector> 15 19 20 21 16 22 class WorldEntity; 17 23 class BoundingVolume; 18 class CollisionEvent;19 24 20 //! A class representing a simple collision 21 class Collision 25 namespace CoRe 22 26 { 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 23 39 public: 40 24 41 Collision(); 25 42 virtual ~Collision(); 26 43 27 44 /** 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; } 29 46 30 47 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(); 43 55 44 56 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(); } 54 65 55 66 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; } 57 84 58 85 59 86 private: 60 WorldEntity* entityA; //!< the collision body A61 WorldEntity* entityB; //!< the collision body B62 bool entityACollide; //!< true if entity A is subscribed for collision reaction63 bool entityBCollide; //!< true if entity B is subscribed for collision reaction87 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 64 91 65 bool bDispatched; //!< true if this collision has already been dispatched92 CollisionVector _collisionEvents; //!< the collision event list 66 93 67 std::vector<CollisionEvent*> collisionEvents; //!< the collision event list68 } ;94 }; 95 } 69 96 70 97 #endif /* _COLLISION_H */ -
trunk/src/lib/collision_reaction/collision_event.cc
r9869 r10013 19 19 #include "debug.h" 20 20 21 /** 22 * standard constructor 23 */ 24 CollisionEvent::CollisionEvent () 21 namespace CoRe 25 22 { 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 30 42 } 31 43 32 33 /**34 * standard deconstructor35 */36 CollisionEvent::~CollisionEvent ()37 {38 }39 40 41 -
trunk/src/lib/collision_reaction/collision_event.h
r9869 r10013 2 2 * @file collision_event.h 3 3 * 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. 4 10 */ 5 11 … … 8 14 9 15 #include "vector.h" 16 #include "cr_engine.h" 10 17 11 18 #include "cr_defs.h" … … 16 23 class Plane; 17 24 25 namespace 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 } 18 48 19 49 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; } 25 58 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; } 32 70 33 71 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 42 75 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 45 78 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 48 81 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 }; 51 85 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 } 70 87 #endif /* _COLLISION_EVENT_H */ -
trunk/src/lib/collision_reaction/collision_reaction.cc
r9869 r10013 11 11 ### File Specific: 12 12 main-programmer: Patrick Boenzli 13 co-programmer: ...14 13 */ 15 14 … … 18 17 #include "collision_reaction.h" 19 18 19 namespace 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 } 20 32 21 33 22 ObjectListDefinition(CollisionReaction); 34 /** 35 * standard deconstructor 36 */ 37 CollisionReaction::~CollisionReaction () 38 {} 23 39 24 /**25 * standard constructor26 */27 CollisionReaction::CollisionReaction ()28 : BaseObject()29 {30 this->registerObject(this, CollisionReaction::_objectList);31 40 } 32 33 34 /**35 * standard deconstructor36 */37 CollisionReaction::~CollisionReaction ()38 {39 }40 -
trunk/src/lib/collision_reaction/collision_reaction.h
r9869 r10013 9 9 #include "base_object.h" 10 10 11 12 13 class Collision;14 11 class WorldEntity; 15 12 13 namespace CoRe 14 { 16 15 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); 21 22 public: 22 23 CollisionReaction(); … … 25 26 virtual void reactToCollision(Collision* collision) = 0; 26 27 27 virtual void update(WorldEntity* owner) {}28 29 28 /** use this to do some collision offline calculations, only called for bContinuousPoll == true */ 30 29 inline bool isContinuousPoll() const { return this->bContinuousPoll; } … … 32 31 private: 33 32 bool bContinuousPoll; //!< if true the collision rection function is also called, if there was no collision 34 };33 }; 35 34 35 } 36 36 #endif /* _COLLISION_REACTION_H */ -
trunk/src/lib/collision_reaction/cr_defs.h
r9062 r10013 28 28 29 29 30 //!< the collision axis x collision event31 #define COLLISION_TYPE_AXIS_X 132 #define COLLISION_TYPE_AXIS_X_NEG 233 //!< the collision axis y collision event34 #define COLLISION_TYPE_AXIS_Y 335 #define COLLISION_TYPE_AXIS_Y_NEG 436 //!< the collision axis z collision event37 #define COLLISION_TYPE_AXIS_Z 538 #define COLLISION_TYPE_AXIS_Z_NEG 639 //!< the collision is a obb collision40 #define COLLISION_TYPE_OBB 841 30 42 43 #endif /* _NETWORK_MANAGER */ 31 #endif /* _CR_DEFS_H */ -
trunk/src/lib/collision_reaction/cr_engine.cc
r9869 r10013 17 17 18 18 19 #include "cr_object_damage.h" 20 #include "cr_physics_full_walk.h" 21 #include "cr_physics_ground_walk.h" 19 22 20 23 #include "collision.h" 21 24 #include "collision_event.h" 22 #include "collision_handle.h" 25 #include "collision_filter.h" 26 #include "collision_tube.h" 23 27 #include "cr_defs.h" 24 28 … … 27 31 #include "debug.h" 28 32 29 30 31 ObjectListDefinition(CREngine); 32 /** 33 * standard constructor 34 */ 35 CREngine::CREngine () 36 : BaseObject() 33 namespace CoRe 37 34 { 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 42 214 } 43 44 /**45 * the singleton reference to this class46 */47 CREngine* CREngine::singletonRef = NULL;48 49 /**50 @brief standard deconstructor51 */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 state76 */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 state89 */90 void CREngine::reset()91 {92 // first clear all CollisionHandles93 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 CollisionReaction107 * @param owner: the WE to subscribe108 * @param type: the type of collision reaction to perform109 * @return the newly created CollisionHandle110 */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 list122 * @param collisionHandle the CollisionHandle to remove123 * @param returns true if worked collrectly124 */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 EventHandlers141 */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 all148 {149 (*it)->handleCollisions();150 }151 }152 this->flushCollisions();153 }154 155 156 /**157 * flushes all the collision lists and puts them to their initial state158 */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 16 16 #include <vector> 17 17 18 // FORWARD DECLARATION19 class CollisionHandle;20 class Collision;21 class CollisionEvent;22 18 class WorldEntity; 23 19 24 //! A default singleton class. 25 class CREngine : public BaseObject 20 21 namespace CoRe 26 22 { 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 28 38 29 39 public: 30 typedef enum CRType {31 CR_PHYSICS_MOMENTUM = 0,//!< physical reaction: conservervation of momentum32 CR_PHYSICS_STEP_BACK,//!< physical reaction: just go to the last position without collisions33 CR_PHYSICS_GROUND_WALK, //!< physical reaction: stand on the ground, no movement: simulatingsimple normal force away from the gravity force34 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 stability40 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 36 46 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!) 39 49 40 CR_VERTEX_TRAFO,//!< vertex trafo: transforming the vertex according to the damage50 CR_VERTEX_TRAFO, //!< vertex trafo: transforming the vertex according to the damage 41 51 42 CR_SPECIAL_CALLBACK,//!< special: call a callback function52 CR_SPECIAL_CALLBACK, //!< special: call a callback function 43 53 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 45 102 }; 46 103 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 } 94 105 #endif /* _CR_ENGINE_ */ -
trunk/src/lib/collision_reaction/cr_object_damage.cc
r9869 r10013 26 26 #include "debug.h" 27 27 28 namespace CoRe 29 { 30 31 ObjectListDefinition(CRObjectDamage); 32 /** 33 * standard constructor 34 */ 35 CRObjectDamage::CRObjectDamage () 36 : CollisionReaction() 37 { 38 this->registerObject(this, CRObjectDamage::_objectList); 39 } 28 40 29 41 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 {} 39 47 40 48 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; 47 56 57 PRINTF(0)("Dealing damage - Handling collision: %s vs %s\n", 58 collision->getEntityA()->getClassCName(), 59 collision->getEntityB()->getClassCName()); 48 60 49 /** 50 * caluculates and applys the reaction to a specific collision51 * @param collision the collision52 */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 } 56 68 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 } 60 75 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 66 84 } 67 85 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 damage82 // mass = (*it)->getEntityA()->getMass();83 // }84 85 86 } 86 -
trunk/src/lib/collision_reaction/cr_object_damage.h
r9869 r10013 7 7 #define _CR_OBJECT_DAMAGE_H 8 8 9 9 10 #include "collision_reaction.h" 10 11 11 12 12 class Collision; 13 namespace CoRe 14 { 13 15 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); 18 22 public: 19 23 CRObjectDamage(); … … 22 26 virtual void reactToCollision(Collision* collision); 23 27 24 private: 25 26 }; 28 }; 29 } 27 30 28 31 #endif /* _CR_OBJECT_DAMAGE_H */ -
trunk/src/lib/collision_reaction/cr_physics_full_walk.cc
r9869 r10013 33 33 #include "cr_defs.h" 34 34 35 36 37 ObjectListDefinition(CRPhysicsFullWalk); 38 /** 39 * standard constructor 40 */ 41 CRPhysicsFullWalk::CRPhysicsFullWalk () 42 : CollisionReaction() 35 namespace CoRe 43 36 { 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 45 215 } 46 216 47 48 /**49 * standard deconstructor50 */51 CRPhysicsFullWalk::~CRPhysicsFullWalk ()52 {}53 54 55 /**56 * caluculates and applys the reaction to a specific collision57 * @param collision the collision58 */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 position91 Vector collPos = collision->getEntityB()->getAbsCoor() + box->center - ce->getCollisionPosition();92 93 // test the 3 axis differently94 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 wall113 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 wall133 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 ground141 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 nothing146 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 wall152 else if( ce->isInWall())153 {154 entity->setAbsCoor(entity->getLastAbsCoor());155 PRINTF(0)("ground collision: reset pos\n");156 }157 else158 {159 // entity is not on ground160 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 wall182 entity->setAbsCoor(entity->getLastAbsCoor());183 }184 break;185 186 187 // collision in the z-axis188 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 wall202 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 == true224 */225 void CRPhysicsFullWalk::update(WorldEntity* owner)226 {}227 228 -
trunk/src/lib/collision_reaction/cr_physics_full_walk.h
r9869 r10013 9 9 #include "collision_reaction.h" 10 10 11 namespace CoRe 12 { 11 13 12 class Collision;14 class Collision; 13 15 14 //! A class representing a reaction to a collision: dealing damage to an object15 class CRPhysicsFullWalk : public CollisionReaction16 {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); 18 20 public: 19 21 CRPhysicsFullWalk(); … … 21 23 22 24 virtual void reactToCollision(Collision* collision); 23 24 virtual void update(WorldEntity* entity); 25 26 27 private: 28 float downspeed; 29 }; 30 25 }; 26 } 31 27 #endif /* _CR_PHYSICS_FULL_WALK_H */ -
trunk/src/lib/collision_reaction/cr_physics_ground_walk.cc
r9869 r10013 34 34 35 35 36 namespace CoRe 37 { 36 38 37 ObjectListDefinition(CRPhysicsGroundWalk); 38 /** 39 * standard constructor 40 */ 41 CRPhysicsGroundWalk::CRPhysicsGroundWalk () 42 : CollisionReaction() 43 { 44 this->registerObject(this, CRPhysicsGroundWalk::_objectList); 45 } 39 ObjectListDefinition(CRPhysicsGroundWalk); 46 40 47 41 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() 66 47 { 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); 69 49 } 70 50 71 51 72 float CR_MAX_WALK_HEIGHT = 15.0f; 73 74 float height = 0.0f; 52 /** 53 * standard deconstructor 54 */ 55 CRPhysicsGroundWalk::~CRPhysicsGroundWalk () 56 {} 75 57 76 58 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) 80 64 { 81 65 82 CollisionEvent* ce = (*it);83 Vector normal = ce->getGroundNormal();66 AABB* box = collision->getEntityA()->getModelAABB(); 67 WorldEntity* entity = collision->getEntityA(); 84 68 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) 90 70 { 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 } 95 74 96 75 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; 115 79 116 80 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 } 117 121 } 122 //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side); 123 118 124 } 119 //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side);120 121 }122 125 123 126 124 127 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 9 9 #include "collision_reaction.h" 10 10 11 namespace CoRe 12 { 11 13 12 class Collision;14 class Collision; 13 15 14 //! A class representing a reaction to a collision: dealing damage to an object15 class CRPhysicsGroundWalk : public CollisionReaction16 {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); 18 20 public: 19 21 CRPhysicsGroundWalk(); … … 22 24 virtual void reactToCollision(Collision* collision); 23 25 24 virtual void update(WorldEntity* entity);26 }; 25 27 26 27 private: 28 float downspeed; 29 }; 30 28 } 31 29 #endif /* _CR_PHYSICS_GROUND_WALK_H */
Note: See TracChangeset
for help on using the changeset viewer.