[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" |
---|
| 25 | |
---|
[8288] | 26 | #include <vector> |
---|
| 27 | |
---|
[8894] | 28 | #include "debug.h" |
---|
| 29 | |
---|
[8796] | 30 | #include "aabb.h" |
---|
| 31 | |
---|
[8894] | 32 | #include "cr_defs.h" |
---|
| 33 | |
---|
[8200] | 34 | using namespace std; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * standard constructor |
---|
| 39 | */ |
---|
| 40 | CRPhysicsGroundWalk::CRPhysicsGroundWalk () |
---|
[8724] | 41 | : CollisionReaction() |
---|
[8200] | 42 | { |
---|
[8203] | 43 | this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk"); |
---|
[8200] | 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * standard deconstructor |
---|
| 49 | */ |
---|
| 50 | CRPhysicsGroundWalk::~CRPhysicsGroundWalk () |
---|
[8724] | 51 | {} |
---|
[8200] | 52 | |
---|
| 53 | |
---|
| 54 | /** |
---|
| 55 | * caluculates and applys the reaction to a specific collision |
---|
| 56 | * @param collision the collision |
---|
| 57 | */ |
---|
| 58 | void CRPhysicsGroundWalk::reactToCollision(Collision* collision) |
---|
| 59 | { |
---|
[8334] | 60 | CollisionEvent* ce = collision->getCollisionEvents().front(); |
---|
| 61 | Vector normal = ce->getGroundNormal(); |
---|
[8724] | 62 | // normal.normalize(); |
---|
[8288] | 63 | |
---|
| 64 | // put it back |
---|
[8724] | 65 | // PRINTF(0)("putting it back to lastPos: \n"); |
---|
| 66 | // this->lastPositions[0].debug(); |
---|
| 67 | // PRINTF(0)("current pos:\n"); |
---|
| 68 | // collision->getEntityB()->getAbsCoor().debug(); |
---|
[8323] | 69 | |
---|
[8894] | 70 | float height; |
---|
[8796] | 71 | AABB* box = collision->getEntityB()->getModelAABB(); |
---|
[8894] | 72 | WorldEntity* entity = collision->getEntityB(); |
---|
| 73 | |
---|
| 74 | // collision position maths |
---|
| 75 | Vector collPos = collision->getEntityB()->getAbsCoor() + box->center - ce->getCollisionPosition(); |
---|
| 76 | |
---|
| 77 | float CR_MAX_WALK_HEIGHT = 2.0f; |
---|
| 78 | float CR_THRESHOLD = 0.2f; |
---|
| 79 | |
---|
| 80 | if( box == NULL) |
---|
| 81 | { |
---|
| 82 | PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n"); |
---|
| 83 | return; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | switch( ce->getType()) |
---|
| 88 | { |
---|
| 89 | case COLLISION_TYPE_AXIS_Y: |
---|
| 90 | |
---|
| 91 | height = collPos.y - box->halfLength[1]; |
---|
| 92 | // PRINTF(0)("height: %f , model height: %f\n", height, box->halfLength[1]); |
---|
| 93 | // PRINTF(0)(" ground normal: %f, %f, %f\n", normal.x, normal.y, normal.z); |
---|
| 94 | |
---|
| 95 | // object is beneath the plane (ground) |
---|
| 96 | if( height <= 0.0f ) |
---|
| 97 | { |
---|
| 98 | entity->shiftCoor(Vector(0, -height, 0)); |
---|
| 99 | } |
---|
| 100 | // object is already in the wall |
---|
| 101 | else if( ce->isInWall()) |
---|
| 102 | { |
---|
| 103 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 104 | } |
---|
| 105 | break; |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | case COLLISION_TYPE_AXIS_X: |
---|
| 109 | case COLLISION_TYPE_AXIS_Z: |
---|
| 110 | break; |
---|
| 111 | |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | #if 0 |
---|
| 122 | if( box != NULL) |
---|
[8796] | 123 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 124 | else |
---|
| 125 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 126 | |
---|
| 127 | |
---|
[8894] | 128 | if( box != NULL) { |
---|
[8724] | 129 | |
---|
[8894] | 130 | |
---|
[8796] | 131 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) { |
---|
| 132 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 133 | return; |
---|
| 134 | } |
---|
| 135 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) { |
---|
| 136 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 137 | return; |
---|
| 138 | } |
---|
[8724] | 139 | |
---|
[8796] | 140 | if(ce->getGroundNormal().len() <= 0.1f) { |
---|
| 141 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 142 | return; |
---|
| 143 | } |
---|
[8894] | 144 | |
---|
| 145 | |
---|
[8796] | 146 | if(ce->getGroundNormal().len() >= 1.4f) { |
---|
[8724] | 147 | downspeed++; |
---|
| 148 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8796] | 149 | return; |
---|
[8724] | 150 | } |
---|
| 151 | |
---|
[8796] | 152 | |
---|
| 153 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 154 | { |
---|
[8894] | 155 | if(height.y < box->halfLength[1] + 2.3f) // Snap in |
---|
[8796] | 156 | { |
---|
| 157 | downspeed = 0; |
---|
| 158 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 159 | } else |
---|
| 160 | { |
---|
| 161 | downspeed++; |
---|
| 162 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 163 | } |
---|
| 164 | |
---|
[8724] | 165 | } |
---|
[8796] | 166 | else { |
---|
| 167 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 168 | { |
---|
| 169 | //if(downspeed <= 0) downspeed =1; |
---|
[8894] | 170 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f,0.0)); |
---|
[8796] | 171 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 172 | downspeed = 0; |
---|
| 173 | } |
---|
[8724] | 174 | |
---|
[8796] | 175 | } |
---|
[8724] | 176 | |
---|
[8796] | 177 | }// if(box!= NULL) |
---|
[8894] | 178 | #endif |
---|
[8724] | 179 | /* |
---|
[8341] | 180 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 181 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 182 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 183 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 184 | |
---|
[8724] | 185 | */ |
---|
[8894] | 186 | |
---|
[8256] | 187 | } |
---|
[8200] | 188 | |
---|
[8256] | 189 | |
---|
| 190 | |
---|
[8724] | 191 | |
---|
[8256] | 192 | /** |
---|
| 193 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 194 | */ |
---|
| 195 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
| 196 | { |
---|
[8894] | 197 | |
---|
[8200] | 198 | } |
---|
| 199 | |
---|
[8256] | 200 | |
---|