[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" |
---|
[8907] | 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 | |
---|
[8907] | 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 | } |
---|
[8922] | 70 | |
---|
| 71 | |
---|
[8894] | 72 | float CR_MAX_WALK_HEIGHT = 2.0f; |
---|
| 73 | float CR_THRESHOLD = 0.2f; |
---|
| 74 | |
---|
[8928] | 75 | float height = 0; |
---|
| 76 | float front = 0; |
---|
| 77 | float side = 0; |
---|
[8894] | 78 | |
---|
[8925] | 79 | PRINTF(0)("collision raction======================================\n"); |
---|
[8922] | 80 | |
---|
[8924] | 81 | const std::vector<CollisionEvent*>* collisionEvents = &(collision->getCollisionEvents()); |
---|
| 82 | std::vector<CollisionEvent*>::const_iterator it = collisionEvents->begin(); |
---|
| 83 | for(; it != collisionEvents->end(); it++) |
---|
| 84 | { |
---|
[8922] | 85 | |
---|
[8924] | 86 | CollisionEvent* ce = (*it); |
---|
| 87 | Vector normal = ce->getGroundNormal(); |
---|
[8894] | 88 | |
---|
[8927] | 89 | // calculate the collision position |
---|
[8924] | 90 | Vector collPos = collision->getEntityB()->getAbsCoor() + box->center - ce->getCollisionPosition(); |
---|
[8922] | 91 | |
---|
[8894] | 92 | |
---|
[8924] | 93 | // test the 3 axis differently |
---|
| 94 | switch( ce->getType()) |
---|
| 95 | { |
---|
| 96 | // collision in the x-axis |
---|
| 97 | case COLLISION_TYPE_AXIS_X: |
---|
| 98 | front = collPos.x - box->halfLength[0]; |
---|
[8922] | 99 | |
---|
[8924] | 100 | // object is beneath the plane (ground) |
---|
| 101 | if( front <= 0.0f ) |
---|
| 102 | { |
---|
[8932] | 103 | Vector backoff = entity->getAbsDirX() * front; |
---|
| 104 | entity->shiftCoor(backoff); |
---|
[8924] | 105 | } |
---|
| 106 | // object is already in the wall |
---|
| 107 | else if( ce->isInWall()) |
---|
| 108 | { |
---|
| 109 | // entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 110 | } |
---|
| 111 | break; |
---|
[8922] | 112 | |
---|
[8894] | 113 | |
---|
[8924] | 114 | // collision in the y-axis |
---|
| 115 | case COLLISION_TYPE_AXIS_Y: |
---|
| 116 | // calulate the height above ground |
---|
| 117 | height = collPos.y - box->halfLength[1]; |
---|
[8894] | 118 | |
---|
| 119 | |
---|
[8924] | 120 | // object is beneath the plane (ground) |
---|
| 121 | if( height <= 0.0f ) |
---|
| 122 | { |
---|
| 123 | entity->shiftCoor(Vector(0.0f, -height, 0.0f)); |
---|
| 124 | } |
---|
| 125 | // object is already in the wall |
---|
| 126 | else if( ce->isInWall()) |
---|
| 127 | { |
---|
[8932] | 128 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
[8924] | 129 | } |
---|
| 130 | break; |
---|
[8894] | 131 | |
---|
| 132 | |
---|
[8924] | 133 | // collision in the z-axis |
---|
| 134 | case COLLISION_TYPE_AXIS_Z: |
---|
[8925] | 135 | |
---|
| 136 | side = collPos.z - box->halfLength[2]; |
---|
| 137 | |
---|
| 138 | // object is beneath the plane (ground) |
---|
| 139 | if( side <= 0.0f ) |
---|
| 140 | { |
---|
[8932] | 141 | entity->shiftCoor(Vector(0.0f, 0.0f, side)); |
---|
[8925] | 142 | } |
---|
| 143 | // object is already in the wall |
---|
| 144 | else if( ce->isInWall()) |
---|
| 145 | { |
---|
| 146 | // entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 147 | } |
---|
[8924] | 148 | break; |
---|
| 149 | } |
---|
| 150 | } |
---|
[8931] | 151 | PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side); |
---|
[8894] | 152 | |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | |
---|
[8922] | 156 | |
---|
[8924] | 157 | |
---|
[8894] | 158 | #if 0 |
---|
| 159 | if( box != NULL) |
---|
[8796] | 160 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 161 | else |
---|
| 162 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 163 | |
---|
| 164 | |
---|
[8922] | 165 | if( box != NULL) |
---|
| 166 | { |
---|
[8724] | 167 | |
---|
[8894] | 168 | |
---|
[8922] | 169 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 170 | { |
---|
[8796] | 171 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 172 | return; |
---|
| 173 | } |
---|
[8922] | 174 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 175 | { |
---|
[8796] | 176 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 177 | return; |
---|
| 178 | } |
---|
[8724] | 179 | |
---|
[8922] | 180 | if(ce->getGroundNormal().len() <= 0.1f) |
---|
| 181 | { |
---|
[8796] | 182 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 183 | return; |
---|
| 184 | } |
---|
[8894] | 185 | |
---|
| 186 | |
---|
[8922] | 187 | if(ce->getGroundNormal().len() >= 1.4f) |
---|
| 188 | { |
---|
[8724] | 189 | downspeed++; |
---|
| 190 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8796] | 191 | return; |
---|
[8724] | 192 | } |
---|
| 193 | |
---|
[8796] | 194 | |
---|
| 195 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 196 | { |
---|
[8894] | 197 | if(height.y < box->halfLength[1] + 2.3f) // Snap in |
---|
[8796] | 198 | { |
---|
| 199 | downspeed = 0; |
---|
| 200 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 201 | } else |
---|
| 202 | { |
---|
| 203 | downspeed++; |
---|
| 204 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 205 | } |
---|
| 206 | |
---|
[8724] | 207 | } |
---|
[8922] | 208 | else |
---|
| 209 | { |
---|
[8796] | 210 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 211 | { |
---|
| 212 | //if(downspeed <= 0) downspeed =1; |
---|
[8894] | 213 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f,0.0)); |
---|
[8796] | 214 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 215 | downspeed = 0; |
---|
| 216 | } |
---|
[8724] | 217 | |
---|
[8796] | 218 | } |
---|
[8724] | 219 | |
---|
[8796] | 220 | }// if(box!= NULL) |
---|
[8894] | 221 | #endif |
---|
[8724] | 222 | /* |
---|
[8341] | 223 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 224 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 225 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 226 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 227 | |
---|
[8724] | 228 | */ |
---|
[8894] | 229 | |
---|
[8256] | 230 | } |
---|
[8200] | 231 | |
---|
[8256] | 232 | |
---|
| 233 | |
---|
[8724] | 234 | |
---|
[8256] | 235 | /** |
---|
| 236 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 237 | */ |
---|
| 238 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
[8924] | 239 | {} |
---|
[8200] | 240 | |
---|
[8256] | 241 | |
---|