[8200] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[8724] | 3 | |
---|
[8200] | 4 | Copyright (C) 2004 orx |
---|
[8724] | 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. |
---|
[8724] | 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 | |
---|
[8780] | 28 | #include "aabb.h" |
---|
| 29 | |
---|
[8200] | 30 | using namespace std; |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * standard constructor |
---|
| 35 | */ |
---|
| 36 | CRPhysicsGroundWalk::CRPhysicsGroundWalk () |
---|
[8724] | 37 | : CollisionReaction() |
---|
[8200] | 38 | { |
---|
[8203] | 39 | this->setClassID(CL_CR_PHYSICS_GROUND_WALK, "CRPhysicsGroundWalk"); |
---|
[8200] | 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * standard deconstructor |
---|
| 45 | */ |
---|
| 46 | CRPhysicsGroundWalk::~CRPhysicsGroundWalk () |
---|
[8724] | 47 | {} |
---|
[8200] | 48 | |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * caluculates and applys the reaction to a specific collision |
---|
| 52 | * @param collision the collision |
---|
| 53 | */ |
---|
| 54 | void CRPhysicsGroundWalk::reactToCollision(Collision* collision) |
---|
| 55 | { |
---|
[8334] | 56 | CollisionEvent* ce = collision->getCollisionEvents().front(); |
---|
| 57 | Vector normal = ce->getGroundNormal(); |
---|
[8724] | 58 | // normal.normalize(); |
---|
[8288] | 59 | |
---|
| 60 | // put it back |
---|
[8724] | 61 | // PRINTF(0)("putting it back to lastPos: \n"); |
---|
| 62 | // this->lastPositions[0].debug(); |
---|
| 63 | // PRINTF(0)("current pos:\n"); |
---|
| 64 | // collision->getEntityB()->getAbsCoor().debug(); |
---|
[8323] | 65 | |
---|
[8780] | 66 | Vector height; |
---|
| 67 | AABB* box = collision->getEntityB()->getModelAABB(); |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | if(box!=NULL) |
---|
| 72 | height = ( ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() )*(-1.0f) ; |
---|
| 73 | else |
---|
| 74 | height = ce->getCollisionPosition() - collision->getEntityB()->getAbsCoor() ; |
---|
[8724] | 75 | |
---|
[8780] | 76 | if(box!=NULL) { |
---|
[8724] | 77 | |
---|
| 78 | |
---|
[8780] | 79 | if(ce->getCollisionPosition().x <= 0.9 && ce->getGroundNormal().len() <= 1.4f) { |
---|
| 80 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 81 | return; |
---|
| 82 | } |
---|
| 83 | if(ce->getCollisionPosition().z <= 0.9 && ce->getGroundNormal().len() <= 1.4f) { |
---|
| 84 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 85 | return; |
---|
| 86 | } |
---|
[8724] | 87 | |
---|
[8780] | 88 | if(ce->getGroundNormal().len() <= 0.1f) { |
---|
| 89 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getLastAbsCoor()); |
---|
| 90 | return; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | if(ce->getGroundNormal().len() >= 1.4f) { |
---|
[8724] | 95 | downspeed++; |
---|
| 96 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
[8780] | 97 | return; |
---|
[8724] | 98 | } |
---|
| 99 | |
---|
[8780] | 100 | |
---|
| 101 | if(height.y > box->halfLength[1] + 0.0f ) // Above ground |
---|
[8724] | 102 | { |
---|
[8780] | 103 | if(height.y < box->halfLength[1] + 1.3f) // Snap in |
---|
| 104 | { |
---|
| 105 | downspeed = 0; |
---|
| 106 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() - Vector(0.0,height.y - box->halfLength[1] - 0.0f,0.0)); |
---|
| 107 | } else |
---|
| 108 | { |
---|
| 109 | downspeed++; |
---|
| 110 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0,-0.08*downspeed,0.0)); |
---|
| 111 | } |
---|
| 112 | |
---|
[8724] | 113 | } |
---|
[8780] | 114 | else { |
---|
| 115 | if(height.y < box->halfLength[1] + 0.0f /* && height.y > - 55.0f*/) // below ground |
---|
| 116 | { |
---|
| 117 | //if(downspeed <= 0) downspeed =1; |
---|
| 118 | collision->getEntityB()->setAbsCoor(collision->getEntityB()->getAbsCoor() + Vector(0.0, -height.y + box->halfLength[1] + 2.0f/* 0.00001 *//*height.y+3.500005 + 10.0*/,0.0)); |
---|
| 119 | //collision->getEntityB()->setVelocity(Vector(0.0,0.0,0.0)); |
---|
| 120 | downspeed = 0; |
---|
| 121 | } |
---|
[8724] | 122 | |
---|
[8780] | 123 | } |
---|
[8724] | 124 | |
---|
[8780] | 125 | }// if(box!= NULL) |
---|
[8724] | 126 | /* |
---|
[8341] | 127 | PRINTF(0)("Collision with Ground: \n"); |
---|
| 128 | collision->getEntityB()->getAbsCoor().debug(); |
---|
[8724] | 129 | collision->getEntityB()->setVelocity(Vector()); |
---|
| 130 | collision->getEntityB()->setAbsCoor(this->lastPositions[1]); |
---|
[8341] | 131 | |
---|
[8724] | 132 | */ |
---|
[8256] | 133 | } |
---|
[8200] | 134 | |
---|
[8256] | 135 | |
---|
| 136 | |
---|
[8724] | 137 | |
---|
[8256] | 138 | /** |
---|
| 139 | * use this to do some collision offline calculations, only called for bContinuousPoll == true |
---|
| 140 | */ |
---|
| 141 | void CRPhysicsGroundWalk::update(WorldEntity* owner) |
---|
| 142 | { |
---|
[8337] | 143 | for( int i = 9; i > 0; i--) { |
---|
[8336] | 144 | this->lastPositions[i] = this->lastPositions[i-1]; |
---|
[8724] | 145 | // PRINTF(0)("lastPosition[%i]: %f, %f, %f\n", i, lastPositions[i].x, lastPositions[i].y, lastPositions[i].z); |
---|
[8337] | 146 | } |
---|
[8336] | 147 | this->lastPositions[0] = owner->getAbsCoor(); |
---|
[8200] | 148 | } |
---|
| 149 | |
---|
[8256] | 150 | |
---|