[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: |
---|
| 97 | front = collPos.x - box->halfLength[0]; // should be [0] |
---|
[8894] | 98 | |
---|
[9003] | 99 | // object is beneath the plane (ground) |
---|
| 100 | if( front <= 0.0f ) |
---|
| 101 | { |
---|
| 102 | Vector backoff = entity->getAbsDirX() * front; |
---|
| 103 | // entity->shiftCoor(backoff); |
---|
| 104 | } |
---|
| 105 | // object is already in the wall |
---|
| 106 | else if( ce->isInWall()) |
---|
| 107 | { |
---|
| 108 | // entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 109 | } |
---|
| 110 | break; |
---|
[8894] | 111 | |
---|
| 112 | |
---|
[9003] | 113 | // collision in the y-axis |
---|
| 114 | case COLLISION_TYPE_AXIS_Y: |
---|
| 115 | // calulate the height above ground |
---|
| 116 | height = collPos.y - box->halfLength[1]; |
---|
[8894] | 117 | |
---|
| 118 | |
---|
[9003] | 119 | // object is beneath the plane (ground) |
---|
| 120 | if( height <= 0.0f ) |
---|
| 121 | { |
---|
| 122 | entity->shiftCoor(Vector(0.0f, -height, 0.0f)); |
---|
| 123 | entity->setOnGround(true); |
---|
| 124 | } |
---|
| 125 | // object is already in the wall |
---|
| 126 | else if( ce->isInWall()) |
---|
| 127 | { |
---|
| 128 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
| 132 | // entity is not on ground |
---|
| 133 | entity->setOnGround(false); |
---|
| 134 | } |
---|
| 135 | break; |
---|
[8894] | 136 | |
---|
| 137 | |
---|
[9003] | 138 | // collision in the z-axis |
---|
| 139 | case COLLISION_TYPE_AXIS_Z: |
---|
[8894] | 140 | |
---|
[9003] | 141 | side = collPos.z - box->halfLength[2]; // should be [2] |
---|
[8894] | 142 | |
---|
[9003] | 143 | // object is beneath the plane (ground) |
---|
| 144 | if( side <= 0.0f ) |
---|
| 145 | { |
---|
| 146 | Vector backoff = entity->getAbsDirX() * side; |
---|
| 147 | // entity->shiftCoor(backoff); |
---|
| 148 | } |
---|
| 149 | // object is already in the wall |
---|
| 150 | else if( ce->isInWall()) |
---|
| 151 | { |
---|
| 152 | // entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 153 | } |
---|
| 154 | break; |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side); |
---|
[8894] | 158 | |
---|
[9003] | 159 | |
---|
| 160 | |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | |
---|
[8894] | 164 | #if 0 |
---|
| 165 | if( box != NULL) |
---|
[8796] | 166 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 167 | else |
---|
| 168 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 169 | |
---|
| 170 | |
---|
[9003] | 171 | if( box != NULL) |
---|
| 172 | { |
---|
[8724] | 173 | |
---|
[8894] | 174 | |
---|
[9003] | 175 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 176 | { |
---|
[8796] | 177 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 178 | return; |
---|
| 179 | } |
---|
[9003] | 180 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 181 | { |
---|
[8796] | 182 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 183 | return; |
---|
| 184 | } |
---|
[8724] | 185 | |
---|
[9003] | 186 | if(ce->getGroundNormal().len() <= 0.1f) |
---|
| 187 | { |
---|
[8796] | 188 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 189 | return; |
---|
| 190 | } |
---|
[8894] | 191 | |
---|
| 192 | |
---|
[9003] | 193 | if(ce->getGroundNormal().len() >= 1.4f) |
---|
| 194 | { |
---|
[8724] | 195 | downspeed++; |
---|
| 196 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8796] | 197 | return; |
---|
[8724] | 198 | } |
---|
| 199 | |
---|
[8796] | 200 | |
---|
| 201 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 202 | { |
---|
[8894] | 203 | if(height.y < box->halfLength[1] + 2.3f) // Snap in |
---|
[8796] | 204 | { |
---|
| 205 | downspeed = 0; |
---|
| 206 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 207 | } else |
---|
| 208 | { |
---|
| 209 | downspeed++; |
---|
| 210 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 211 | } |
---|
| 212 | |
---|
[8724] | 213 | } |
---|
[9003] | 214 | else |
---|
| 215 | { |
---|
[8796] | 216 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 217 | { |
---|
| 218 | //if(downspeed <= 0) downspeed =1; |
---|
[8894] | 219 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f,0.0)); |
---|
[8796] | 220 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 221 | downspeed = 0; |
---|
| 222 | } |
---|
[8724] | 223 | |
---|
[8796] | 224 | } |
---|
[8724] | 225 | |
---|
[8796] | 226 | }// if(box!= NULL) |
---|
[8894] | 227 | #endif |
---|
[8724] | 228 | /* |
---|
[8341] | 229 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 230 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 231 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 232 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 233 | |
---|
[8724] | 234 | */ |
---|
[8894] | 235 | |
---|
[8256] | 236 | } |
---|
[8200] | 237 | |
---|
[8256] | 238 | |
---|
| 239 | |
---|
[8724] | 240 | |
---|
[8256] | 241 | /** |
---|
| 242 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 243 | */ |
---|
| 244 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
[9003] | 245 | {} |
---|
[8894] | 246 | |
---|
[8200] | 247 | |
---|