| [2303] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  *                    > www.orxonox.net < | 
|---|
 | 4 |  * | 
|---|
 | 5 |  * | 
|---|
 | 6 |  *   License notice: | 
|---|
 | 7 |  * | 
|---|
 | 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 11 |  *   of the License, or (at your option) any later version. | 
|---|
 | 12 |  * | 
|---|
 | 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 16 |  *   GNU General Public License for more details. | 
|---|
 | 17 |  * | 
|---|
 | 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 19 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 21 |  * | 
|---|
 | 22 |  *   Author: | 
|---|
| [2304] | 23 |  *      Reto Grieder | 
|---|
| [2303] | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "CollisionShape.h" | 
|---|
 | 30 |  | 
|---|
| [3196] | 31 | #include <BulletCollision/CollisionShapes/btCollisionShape.h> | 
|---|
| [2303] | 32 |  | 
|---|
 | 33 | #include "core/CoreIncludes.h" | 
|---|
 | 34 | #include "core/XMLPort.h" | 
|---|
| [5735] | 35 | #include "worldentities/WorldEntity.h" | 
|---|
| [2374] | 36 | #include "CompoundCollisionShape.h" | 
|---|
| [2562] | 37 | #include "WorldEntityCollisionShape.h" | 
|---|
| [2374] | 38 |  | 
|---|
| [2303] | 39 | namespace orxonox | 
|---|
 | 40 | { | 
|---|
| [2374] | 41 |     CollisionShape::CollisionShape(BaseObject* creator) | 
|---|
 | 42 |         : BaseObject(creator) | 
|---|
| [2442] | 43 |         , Synchronisable(creator) | 
|---|
| [2303] | 44 |     { | 
|---|
 | 45 |         RegisterObject(CollisionShape); | 
|---|
 | 46 |  | 
|---|
| [2374] | 47 |         this->parent_ = 0; | 
|---|
| [2445] | 48 |         this->parentID_ = OBJECTID_UNKNOWN; | 
|---|
| [2374] | 49 |         this->collisionShape_ = 0; | 
|---|
| [2403] | 50 |         this->position_ = Vector3::ZERO; | 
|---|
 | 51 |         this->orientation_ = Quaternion::IDENTITY; | 
|---|
 | 52 |         this->scale_ = Vector3::UNIT_SCALE; | 
|---|
| [2469] | 53 |  | 
|---|
 | 54 |         this->registerVariables(); | 
|---|
| [2303] | 55 |     } | 
|---|
 | 56 |  | 
|---|
 | 57 |     CollisionShape::~CollisionShape() | 
|---|
 | 58 |     { | 
|---|
| [2514] | 59 |         // Detach from parent | 
|---|
 | 60 |         if (this->isInitialized() && this->parent_) | 
|---|
| [2527] | 61 |             this->parent_->detach(this); | 
|---|
| [2303] | 62 |     } | 
|---|
 | 63 |  | 
|---|
 | 64 |     void CollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 65 |     { | 
|---|
 | 66 |         SUPER(CollisionShape, XMLPort, xmlelement, mode); | 
|---|
| [2374] | 67 |  | 
|---|
 | 68 |         XMLPortParamTemplate(CollisionShape, "position", setPosition, getPosition, xmlelement, mode, const Vector3&); | 
|---|
 | 69 |         XMLPortParamTemplate(CollisionShape, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&); | 
|---|
| [2429] | 70 |         XMLPortParamTemplate(CollisionShape, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&); | 
|---|
 | 71 |         XMLPortParamLoadOnly(CollisionShape, "scale", setScale, xmlelement, mode); | 
|---|
| [2374] | 72 |         XMLPortParamLoadOnly(CollisionShape, "yaw",   yaw,   xmlelement, mode); | 
|---|
 | 73 |         XMLPortParamLoadOnly(CollisionShape, "pitch", pitch, xmlelement, mode); | 
|---|
 | 74 |         XMLPortParamLoadOnly(CollisionShape, "roll",  roll,  xmlelement, mode); | 
|---|
| [2303] | 75 |     } | 
|---|
 | 76 |  | 
|---|
| [2374] | 77 |     void CollisionShape::registerVariables() | 
|---|
| [2303] | 78 |     { | 
|---|
| [3280] | 79 |         registerVariable(this->parentID_, VariableDirection::ToClient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChanged)); | 
|---|
| [2303] | 80 |     } | 
|---|
 | 81 |  | 
|---|
| [2423] | 82 |     void CollisionShape::parentChanged() | 
|---|
| [2303] | 83 |     { | 
|---|
| [2562] | 84 |         Synchronisable* parent = Synchronisable::getSynchronisable(this->parentID_); | 
|---|
 | 85 |         // Parent can either be a WorldEntity or a CompoundCollisionShape. The reason is that the | 
|---|
 | 86 |         // internal collision shape (which is compound) of a WE doesn't get synchronised. | 
|---|
| [3325] | 87 |         CompoundCollisionShape* parentCCS = orxonox_cast<CompoundCollisionShape*>(parent); | 
|---|
| [2562] | 88 |         if (parentCCS) | 
|---|
 | 89 |             parentCCS->attach(this); | 
|---|
 | 90 |         else | 
|---|
 | 91 |         { | 
|---|
| [3325] | 92 |             WorldEntity* parentWE = orxonox_cast<WorldEntity*>(parent); | 
|---|
| [2562] | 93 |             if (parentWE) | 
|---|
 | 94 |                 parentWE->attachCollisionShape(this); | 
|---|
 | 95 |         } | 
|---|
| [2303] | 96 |     } | 
|---|
 | 97 |  | 
|---|
| [2562] | 98 |     bool CollisionShape::notifyBeingAttached(CompoundCollisionShape* newParent) | 
|---|
 | 99 |     { | 
|---|
 | 100 |         if (this->parent_) | 
|---|
 | 101 |             this->parent_->detach(this); | 
|---|
 | 102 |  | 
|---|
 | 103 |         this->parent_ = newParent; | 
|---|
 | 104 |  | 
|---|
| [3325] | 105 |         WorldEntityCollisionShape* parentWECCS = orxonox_cast<WorldEntityCollisionShape*>(newParent); | 
|---|
| [2562] | 106 |         if (parentWECCS) | 
|---|
 | 107 |             this->parentID_ = parentWECCS->getWorldEntityOwner()->getObjectID(); | 
|---|
 | 108 |         else | 
|---|
 | 109 |             this->parentID_ = newParent->getObjectID(); | 
|---|
 | 110 |  | 
|---|
 | 111 |         return true; | 
|---|
 | 112 |     } | 
|---|
 | 113 |  | 
|---|
 | 114 |     void CollisionShape::notifyDetached() | 
|---|
 | 115 |     { | 
|---|
 | 116 |         this->parent_ = 0; | 
|---|
 | 117 |         this->parentID_ = OBJECTID_UNKNOWN; | 
|---|
 | 118 |     } | 
|---|
 | 119 |  | 
|---|
| [2423] | 120 |     void CollisionShape::updateParent() | 
|---|
 | 121 |     { | 
|---|
 | 122 |         if (this->parent_) | 
|---|
| [2527] | 123 |             this->parent_->updateAttachedShape(this); | 
|---|
| [2423] | 124 |     } | 
|---|
 | 125 |  | 
|---|
| [2374] | 126 |     bool CollisionShape::hasTransform() const | 
|---|
| [2303] | 127 |     { | 
|---|
| [3196] | 128 |         return (!this->position_.positionEquals(Vector3(0, 0, 0), 0.001f) || | 
|---|
 | 129 |                 !this->orientation_.equals(Quaternion(1,0,0,0), Degree(0.1f))); | 
|---|
| [2303] | 130 |     } | 
|---|
| [2306] | 131 |  | 
|---|
 | 132 |     void CollisionShape::setScale3D(const Vector3& scale) | 
|---|
 | 133 |     { | 
|---|
| [2433] | 134 |         CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl; | 
|---|
| [2306] | 135 |     } | 
|---|
 | 136 |  | 
|---|
| [2374] | 137 |     void CollisionShape::setScale(float scale) | 
|---|
| [2306] | 138 |     { | 
|---|
| [2433] | 139 |         CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl; | 
|---|
| [2306] | 140 |     } | 
|---|
| [2423] | 141 |  | 
|---|
| [2514] | 142 |     void CollisionShape::updateShape() | 
|---|
 | 143 |     { | 
|---|
 | 144 |         btCollisionShape* oldShape = this->collisionShape_; | 
|---|
 | 145 |         this->collisionShape_ = this->createNewShape(); | 
|---|
 | 146 |         // When we recreate the shape, we have to inform the parent about this to update the shape | 
|---|
 | 147 |         this->updateParent(); | 
|---|
 | 148 |         if (oldShape) | 
|---|
 | 149 |             delete oldShape; | 
|---|
 | 150 |     } | 
|---|
 | 151 |  | 
|---|
| [7401] | 152 |     void CollisionShape::calculateLocalInertia(float mass, btVector3& inertia) const | 
|---|
| [2423] | 153 |     { | 
|---|
 | 154 |         if (this->collisionShape_) | 
|---|
 | 155 |             this->collisionShape_->calculateLocalInertia(mass, inertia); | 
|---|
| [2452] | 156 |         else | 
|---|
 | 157 |             inertia.setValue(0, 0, 0); | 
|---|
| [2423] | 158 |     } | 
|---|
| [2303] | 159 | } | 
|---|