[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 | |
---|
[10013] | 36 | namespace CoRe |
---|
[8200] | 37 | { |
---|
| 38 | |
---|
[10013] | 39 | ObjectListDefinition(CRPhysicsGroundWalk); |
---|
[8200] | 40 | |
---|
| 41 | |
---|
[10013] | 42 | /** |
---|
| 43 | * standard constructor |
---|
| 44 | */ |
---|
| 45 | CRPhysicsGroundWalk::CRPhysicsGroundWalk () |
---|
| 46 | : CollisionReaction() |
---|
| 47 | { |
---|
| 48 | this->registerObject(this, CRPhysicsGroundWalk::_objectList); |
---|
| 49 | } |
---|
[8200] | 50 | |
---|
[8288] | 51 | |
---|
[10013] | 52 | /** |
---|
| 53 | * standard deconstructor |
---|
| 54 | */ |
---|
| 55 | CRPhysicsGroundWalk::~CRPhysicsGroundWalk () |
---|
| 56 | {} |
---|
[8894] | 57 | |
---|
[10013] | 58 | |
---|
| 59 | /** |
---|
| 60 | * caluculates and applys the reaction to a specific collision |
---|
| 61 | * @param collision the collision |
---|
| 62 | */ |
---|
| 63 | void CRPhysicsGroundWalk::reactToCollision(Collision* collision) |
---|
[8894] | 64 | { |
---|
| 65 | |
---|
[10013] | 66 | AABB* box = collision->getEntityA()->getModelAABB(); |
---|
| 67 | WorldEntity* entity = collision->getEntityA(); |
---|
[8894] | 68 | |
---|
[10013] | 69 | if( box == NULL) |
---|
| 70 | { |
---|
| 71 | PRINTF(2)("this model has no aabb box so there is no correct collision reaction implemented. skipping\n"); |
---|
| 72 | return; |
---|
| 73 | } |
---|
[9003] | 74 | |
---|
| 75 | |
---|
[10013] | 76 | float CR_MAX_WALK_HEIGHT = 15.0f; |
---|
[9003] | 77 | |
---|
[10013] | 78 | float height = 0.0f; |
---|
[8894] | 79 | |
---|
| 80 | |
---|
[10013] | 81 | std::vector<CollisionEvent*>::const_iterator it = collision->begin(); |
---|
| 82 | for(; it != collision->end(); it++) |
---|
[9003] | 83 | { |
---|
[8894] | 84 | |
---|
[10013] | 85 | CollisionEvent* ce = (*it); |
---|
| 86 | Vector normal = ce->getGroundNormal(); |
---|
[8894] | 87 | |
---|
[10013] | 88 | // calculate the collision position |
---|
| 89 | Vector collPos = collision->getEntityA()->getAbsCoor() + box->center - ce->getCollisionPosition(); |
---|
[8894] | 90 | |
---|
[10013] | 91 | // test the 3 axis differently |
---|
| 92 | switch( ce->getType()) |
---|
| 93 | { |
---|
| 94 | /* collision in the Y-AXIS */ |
---|
| 95 | case CoRe::CREngine::CR_COLLISION_TYPE_AXIS_Y_NEG: |
---|
| 96 | // calulate the height above ground |
---|
| 97 | height = collPos.len() - box->halfLength[1]; |
---|
[8894] | 98 | |
---|
| 99 | |
---|
[10013] | 100 | // object is beneath the plane (ground) |
---|
| 101 | // if(height >= 0.0f && height <= 0.0001f) break ;// Do nothing |
---|
| 102 | if( height < 0.0f && -height < CR_MAX_WALK_HEIGHT) |
---|
| 103 | { |
---|
| 104 | entity->shiftCoor(Vector(0.0f, -height + 0.00001, 0.0f)); |
---|
| 105 | entity->setOnGround(true); |
---|
| 106 | } |
---|
| 107 | // object is already in the wall |
---|
| 108 | else if( ce->isInWall()) |
---|
| 109 | { |
---|
| 110 | entity->setAbsCoor(entity->getLastAbsCoor()); |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
| 114 | // entity is not on ground |
---|
| 115 | entity->setOnGround(false); |
---|
| 116 | } |
---|
| 117 | break; |
---|
[8200] | 118 | |
---|
[8256] | 119 | |
---|
[10013] | 120 | } |
---|
| 121 | } |
---|
| 122 | //PRINTF(0)("collision distances: x: %f, y: %f, z: %f\n", front, height, side); |
---|
[8256] | 123 | |
---|
[10013] | 124 | } |
---|
[8724] | 125 | |
---|
[8894] | 126 | |
---|
[8200] | 127 | |
---|
[10013] | 128 | } |
---|