[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 | { |
---|
[8334] | 61 | CollisionEvent* ce = collision->getCollisionEvents().front(); |
---|
| 62 | Vector normal = ce->getGroundNormal(); |
---|
[8724] | 63 | // normal.normalize(); |
---|
[8288] | 64 | |
---|
[8323] | 65 | |
---|
[8894] | 66 | float height; |
---|
[8796] | 67 | AABB* box = collision->getEntityB()->getModelAABB(); |
---|
[8894] | 68 | WorldEntity* entity = collision->getEntityB(); |
---|
| 69 | |
---|
[8907] | 70 | if( box == NULL) |
---|
| 71 | { |
---|
| 72 | PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n"); |
---|
| 73 | return; |
---|
| 74 | } |
---|
[8922] | 75 | |
---|
| 76 | |
---|
[8894] | 77 | // collision position maths |
---|
| 78 | Vector collPos = collision->getEntityB()->getAbsCoor() + box->center - ce->getCollisionPosition(); |
---|
| 79 | |
---|
| 80 | float CR_MAX_WALK_HEIGHT = 2.0f; |
---|
| 81 | float CR_THRESHOLD = 0.2f; |
---|
| 82 | |
---|
[8910] | 83 | //height = collPos.y - box->halfLength[1]; |
---|
| 84 | //PRINTF(0)("height: %f , model height: %f\n", height, box->halfLength[1]); |
---|
| 85 | // PRINTF(0)(" ground normal: %f, %f, %f\n", normal.x, normal.y, normal.z); |
---|
[8922] | 86 | // |
---|
[8894] | 87 | |
---|
| 88 | switch( ce->getType()) |
---|
| 89 | { |
---|
[8922] | 90 | |
---|
[8910] | 91 | case COLLISION_TYPE_AXIS_X: |
---|
[8922] | 92 | |
---|
[8910] | 93 | break; |
---|
[8894] | 94 | |
---|
[8922] | 95 | case COLLISION_TYPE_AXIS_Y: |
---|
| 96 | |
---|
[8910] | 97 | height = collPos.y - box->halfLength[1]; |
---|
[8894] | 98 | |
---|
[8922] | 99 | PRINTF(0)("height: %f\n", height); |
---|
| 100 | PRINTF(0)("in wall %i\n", ce->isInWall()); |
---|
| 101 | |
---|
| 102 | // object is beneath the plane (ground) |
---|
| 103 | if( height <= 0.0f ) |
---|
| 104 | { |
---|
| 105 | entity->shiftCoor(Vector(0, -height, 0)); |
---|
| 106 | } |
---|
| 107 | // object is already in the wall |
---|
| 108 | else if( ce->isInWall()) |
---|
| 109 | { |
---|
| 110 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 111 | } |
---|
| 112 | break; |
---|
| 113 | break; |
---|
| 114 | |
---|
| 115 | |
---|
[8894] | 116 | case COLLISION_TYPE_AXIS_Z: |
---|
| 117 | |
---|
[8922] | 118 | break; |
---|
[8894] | 119 | |
---|
[8922] | 120 | } |
---|
[8894] | 121 | |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | |
---|
[8922] | 128 | |
---|
[8894] | 129 | #if 0 |
---|
| 130 | if( box != NULL) |
---|
[8796] | 131 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 132 | else |
---|
| 133 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 134 | |
---|
| 135 | |
---|
[8922] | 136 | if( box != NULL) |
---|
| 137 | { |
---|
[8724] | 138 | |
---|
[8894] | 139 | |
---|
[8922] | 140 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 141 | { |
---|
[8796] | 142 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 143 | return; |
---|
| 144 | } |
---|
[8922] | 145 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) |
---|
| 146 | { |
---|
[8796] | 147 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 148 | return; |
---|
| 149 | } |
---|
[8724] | 150 | |
---|
[8922] | 151 | if(ce->getGroundNormal().len() <= 0.1f) |
---|
| 152 | { |
---|
[8796] | 153 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 154 | return; |
---|
| 155 | } |
---|
[8894] | 156 | |
---|
| 157 | |
---|
[8922] | 158 | if(ce->getGroundNormal().len() >= 1.4f) |
---|
| 159 | { |
---|
[8724] | 160 | downspeed++; |
---|
| 161 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8796] | 162 | return; |
---|
[8724] | 163 | } |
---|
| 164 | |
---|
[8796] | 165 | |
---|
| 166 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 167 | { |
---|
[8894] | 168 | if(height.y < box->halfLength[1] + 2.3f) // Snap in |
---|
[8796] | 169 | { |
---|
| 170 | downspeed = 0; |
---|
| 171 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 172 | } else |
---|
| 173 | { |
---|
| 174 | downspeed++; |
---|
| 175 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 176 | } |
---|
| 177 | |
---|
[8724] | 178 | } |
---|
[8922] | 179 | else |
---|
| 180 | { |
---|
[8796] | 181 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 182 | { |
---|
| 183 | //if(downspeed <= 0) downspeed =1; |
---|
[8894] | 184 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f,0.0)); |
---|
[8796] | 185 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 186 | downspeed = 0; |
---|
| 187 | } |
---|
[8724] | 188 | |
---|
[8796] | 189 | } |
---|
[8724] | 190 | |
---|
[8796] | 191 | }// if(box!= NULL) |
---|
[8894] | 192 | #endif |
---|
[8724] | 193 | /* |
---|
[8341] | 194 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 195 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 196 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 197 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 198 | |
---|
[8724] | 199 | */ |
---|
[8894] | 200 | |
---|
[8256] | 201 | } |
---|
[8200] | 202 | |
---|
[8256] | 203 | |
---|
| 204 | |
---|
[8724] | 205 | |
---|
[8256] | 206 | /** |
---|
| 207 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 208 | */ |
---|
| 209 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
| 210 | { |
---|
[8200] | 211 | } |
---|
| 212 | |
---|
[8256] | 213 | |
---|