[8200] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[8894] | 3 | |
---|
[8200] | 4 | Copyright (C) 2004 orx |
---|
[8894] | 5 | |
---|
[8200] | 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[8894] | 10 | |
---|
[8200] | 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_COLLISION_REACTION |
---|
| 17 | |
---|
| 18 | #include "collision.h" |
---|
| 19 | #include "collision_event.h" |
---|
| 20 | |
---|
| 21 | #include "physics_interface.h" |
---|
| 22 | |
---|
| 23 | #include "world_entity.h" |
---|
| 24 | #include "cr_physics_ground_walk.h" |
---|
[9003] | 25 | #include "collision_reaction.h" |
---|
[8200] | 26 | |
---|
[8288] | 27 | #include <vector> |
---|
| 28 | |
---|
[8894] | 29 | #include "debug.h" |
---|
| 30 | |
---|
[8796] | 31 | #include "aabb.h" |
---|
| 32 | |
---|
[8894] | 33 | #include "cr_defs.h" |
---|
| 34 | |
---|
[8200] | 35 | using namespace std; |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * standard constructor |
---|
| 40 | */ |
---|
| 41 | CRPhysicsGroundWalk::CRPhysicsGroundWalk () |
---|
[8724] | 42 | : CollisionReaction() |
---|
[8200] | 43 | { |
---|
[8203] | 44 | this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk"); |
---|
[8200] | 45 | } |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * standard deconstructor |
---|
| 50 | */ |
---|
| 51 | CRPhysicsGroundWalk::~CRPhysicsGroundWalk () |
---|
[8724] | 52 | {} |
---|
[8200] | 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 | { |
---|
[8288] | 61 | |
---|
[8796] | 62 | AABB* box = collision->getEntityB()->getModelAABB(); |
---|
[8894] | 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 | |
---|
[9003] | 72 | float CR_MAX_WALK_HEIGHT = 2.0f; |
---|
| 73 | float CR_THRESHOLD = 0.2f; |
---|
| 74 | |
---|
| 75 | float height = 0; |
---|
| 76 | float front = 0; |
---|
| 77 | float side = 0; |
---|
| 78 | |
---|
| 79 | //PRINTF(0)("collision raction======================================\n"); |
---|
| 80 | |
---|
| 81 | const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents()); |
---|
| 82 | std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin(); |
---|
| 83 | for(; it != collisionEvents->end(); it++) |
---|
[8894] | 84 | { |
---|
| 85 | |
---|
[9003] | 86 | CollisionEvent* ce = (*it); |
---|
| 87 | Vector normal = ce->getGroundNormal(); |
---|
[8894] | 88 | |
---|
[9003] | 89 | // calculate the collision position |
---|
| 90 | Vector collPos = collision->getEntityB()->getAbsCoor() + box->center - ce->getCollisionPosition(); |
---|
[8894] | 91 | |
---|
[9003] | 92 | // test the 3 axis differently |
---|
| 93 | switch( ce->getType()) |
---|
| 94 | { |
---|
| 95 | // collision in the x-axis |
---|
| 96 | case COLLISION_TYPE_AXIS_X: |
---|
[9050] | 97 | front = collPos.len() - box->halfLength[0]; // should be [0] |
---|
[8894] | 98 | |
---|
[9003] | 99 | // object is beneath the plane (ground) |
---|
| 100 | if( front <= 0.0f ) |
---|
| 101 | { |
---|
[9050] | 102 | Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize(); |
---|
| 103 | Vector backoff = dirX * front; |
---|
[9054] | 104 | |
---|
| 105 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 106 | // entity->shiftCoor(backoff); |
---|
[9003] | 107 | } |
---|
| 108 | // object is already in the wall |
---|
| 109 | else if( ce->isInWall()) |
---|
| 110 | { |
---|
[9054] | 111 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
[9003] | 112 | } |
---|
| 113 | break; |
---|
[9058] | 114 | |
---|
| 115 | case COLLISION_TYPE_AXIS_X_NEG: |
---|
| 116 | front = collPos.len() - box->halfLength[0]; // should be [0] |
---|
[8894] | 117 | |
---|
[9058] | 118 | // object is beneath the plane (ground) |
---|
| 119 | if( front <= 0.0f ) |
---|
| 120 | { |
---|
| 121 | Vector dirX = entity->getAbsDirX(); dirX.y = 0.0f; dirX.normalize(); |
---|
| 122 | Vector backoff = dirX * front * -1.0f; |
---|
| 123 | |
---|
| 124 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 125 | // entity->shiftCoor(backoff); |
---|
| 126 | } |
---|
| 127 | // object is already in the wall |
---|
| 128 | else if( ce->isInWall()) |
---|
| 129 | { |
---|
| 130 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 131 | } |
---|
| 132 | break; |
---|
[8894] | 133 | |
---|
[9058] | 134 | |
---|
[9003] | 135 | // collision in the y-axis |
---|
[9058] | 136 | case COLLISION_TYPE_AXIS_Y_NEG: |
---|
[9003] | 137 | // calulate the height above ground |
---|
| 138 | height = collPos.y - box->halfLength[1]; |
---|
[8894] | 139 | |
---|
| 140 | |
---|
[9003] | 141 | // object is beneath the plane (ground) |
---|
[9050] | 142 | if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing |
---|
| 143 | else if( height < 0.0f ) |
---|
[9003] | 144 | { |
---|
[9050] | 145 | entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f)); |
---|
[9003] | 146 | entity->setOnGround(true); |
---|
| 147 | } |
---|
| 148 | // object is already in the wall |
---|
| 149 | else if( ce->isInWall()) |
---|
| 150 | { |
---|
| 151 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 152 | } |
---|
| 153 | else |
---|
| 154 | { |
---|
| 155 | // entity is not on ground |
---|
| 156 | entity->setOnGround(false); |
---|
| 157 | } |
---|
| 158 | break; |
---|
[8894] | 159 | |
---|
| 160 | |
---|
[9003] | 161 | // collision in the z-axis |
---|
| 162 | case COLLISION_TYPE_AXIS_Z: |
---|
[8894] | 163 | |
---|
[9050] | 164 | side = collPos.len() - box->halfLength[2]; // should be [2] |
---|
[8894] | 165 | |
---|
[9003] | 166 | // object is beneath the plane (ground) |
---|
| 167 | if( side <= 0.0f ) |
---|
| 168 | { |
---|
[9054] | 169 | entity->setAbsCoor(entity->getAbsCoor()); |
---|
[9058] | 170 | Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize(); |
---|
| 171 | Vector backoff = dirZ * side; |
---|
| 172 | entity->shiftCoor(backoff); |
---|
[9003] | 173 | } |
---|
| 174 | // object is already in the wall |
---|
| 175 | else if( ce->isInWall()) |
---|
| 176 | { |
---|
[9054] | 177 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
[9003] | 178 | } |
---|
| 179 | break; |
---|
[9058] | 180 | |
---|
| 181 | |
---|
| 182 | // collision in the z-axis |
---|
| 183 | case COLLISION_TYPE_AXIS_Z_NEG: |
---|
| 184 | |
---|
| 185 | side = collPos.len() - box->halfLength[2]; // should be [2] |
---|
| 186 | |
---|
| 187 | // object is beneath the plane (ground) |
---|
| 188 | if( side <= 0.0f ) |
---|
| 189 | { |
---|
| 190 | |
---|
| 191 | Vector dirZ = entity->getAbsDirZ(); dirZ.y = 0.0f; dirZ.normalize(); |
---|
| 192 | Vector backoff = dirZ * side*-1.0f; |
---|
| 193 | entity->shiftCoor(backoff); |
---|
| 194 | } |
---|
| 195 | // object is already in the wall |
---|
| 196 | else if( ce->isInWall()) |
---|
| 197 | { |
---|
| 198 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 199 | } |
---|
| 200 | break; |
---|
[9003] | 201 | } |
---|
| 202 | } |
---|
[9057] | 203 | //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side); |
---|
[8894] | 204 | |
---|
[9003] | 205 | |
---|
| 206 | |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | |
---|
[8894] | 210 | #if 0 |
---|
| 211 | if( box != NULL) |
---|
[8796] | 212 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 213 | else |
---|
| 214 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 215 | |
---|
| 216 | |
---|
[9003] | 217 | if( box != NULL) |
---|
| 218 | { |
---|
[8724] | 219 | |
---|
[8894] | 220 | |
---|
[9003] | 221 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 222 | { |
---|
[8796] | 223 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 224 | return; |
---|
| 225 | } |
---|
[9003] | 226 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 227 | { |
---|
[8796] | 228 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 229 | return; |
---|
| 230 | } |
---|
[8724] | 231 | |
---|
[9003] | 232 | if(ce->getGroundNormal().len() <= 0.1f) |
---|
| 233 | { |
---|
[8796] | 234 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 235 | return; |
---|
| 236 | } |
---|
[8894] | 237 | |
---|
| 238 | |
---|
[9003] | 239 | if(ce->getGroundNormal().len() >= 1.4f) |
---|
| 240 | { |
---|
[8724] | 241 | downspeed++; |
---|
| 242 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8796] | 243 | return; |
---|
[8724] | 244 | } |
---|
| 245 | |
---|
[8796] | 246 | |
---|
| 247 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 248 | { |
---|
[8894] | 249 | if(height.y < box->halfLength[1] + 2.3f) // Snap in |
---|
[8796] | 250 | { |
---|
| 251 | downspeed = 0; |
---|
| 252 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 253 | } else |
---|
| 254 | { |
---|
| 255 | downspeed++; |
---|
| 256 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 257 | } |
---|
| 258 | |
---|
[8724] | 259 | } |
---|
[9003] | 260 | else |
---|
| 261 | { |
---|
[8796] | 262 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 263 | { |
---|
| 264 | //if(downspeed <= 0) downspeed =1; |
---|
[8894] | 265 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f,0.0)); |
---|
[8796] | 266 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 267 | downspeed = 0; |
---|
| 268 | } |
---|
[8724] | 269 | |
---|
[8796] | 270 | } |
---|
[8724] | 271 | |
---|
[8796] | 272 | }// if(box!= NULL) |
---|
[8894] | 273 | #endif |
---|
[8724] | 274 | /* |
---|
[8341] | 275 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 276 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 277 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 278 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 279 | |
---|
[8724] | 280 | */ |
---|
[8894] | 281 | |
---|
[8256] | 282 | } |
---|
[8200] | 283 | |
---|
[8256] | 284 | |
---|
| 285 | |
---|
[8724] | 286 | |
---|
[8256] | 287 | /** |
---|
| 288 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 289 | */ |
---|
| 290 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
[9003] | 291 | {} |
---|
[8894] | 292 | |
---|
[8200] | 293 | |
---|