[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 "OrxonoxStableHeaders.h" |
---|
| 30 | #include "CompoundCollisionShape.h" |
---|
| 31 | |
---|
| 32 | #include "BulletCollision/CollisionShapes/btCompoundShape.h" |
---|
| 33 | |
---|
[2423] | 34 | #include "util/Exception.h" |
---|
[2303] | 35 | #include "core/CoreIncludes.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
| 37 | #include "tools/BulletConversions.h" |
---|
[2423] | 38 | #include "objects/worldentities/WorldEntity.h" |
---|
[2303] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateFactory(CompoundCollisionShape); |
---|
| 43 | |
---|
| 44 | CompoundCollisionShape::CompoundCollisionShape(BaseObject* creator) : CollisionShape(creator) |
---|
| 45 | { |
---|
| 46 | RegisterObject(CompoundCollisionShape); |
---|
| 47 | |
---|
| 48 | this->compoundShape_ = new btCompoundShape(); |
---|
[2469] | 49 | this->worldEntityParent_ = 0; |
---|
[2303] | 50 | } |
---|
| 51 | |
---|
| 52 | CompoundCollisionShape::~CompoundCollisionShape() |
---|
| 53 | { |
---|
| 54 | if (this->isInitialized()) |
---|
[2423] | 55 | { |
---|
[2477] | 56 | // Notify children about removal |
---|
| 57 | for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->childShapes_.begin(); |
---|
| 58 | it != this->childShapes_.end(); ++it) |
---|
| 59 | { |
---|
| 60 | it->first->setParent(0, OBJECTID_UNKNOWN); |
---|
| 61 | } |
---|
| 62 | |
---|
[2303] | 63 | delete this->compoundShape_; |
---|
[2423] | 64 | } |
---|
[2303] | 65 | } |
---|
| 66 | |
---|
| 67 | void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 68 | { |
---|
| 69 | SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode); |
---|
[2374] | 70 | // Attached collision shapes |
---|
[2303] | 71 | XMLPortObject(CompoundCollisionShape, CollisionShape, "", addChildShape, getChildShape, xmlelement, mode); |
---|
| 72 | } |
---|
| 73 | |
---|
[2469] | 74 | void CompoundCollisionShape::setWorldEntityParent(WorldEntity* parent) |
---|
| 75 | { |
---|
| 76 | // suppress synchronisation |
---|
| 77 | this->setObjectMode(0x0); |
---|
| 78 | |
---|
| 79 | this->worldEntityParent_ = parent; |
---|
| 80 | } |
---|
| 81 | |
---|
[2423] | 82 | void CompoundCollisionShape::addChildShape(CollisionShape* shape) |
---|
[2374] | 83 | { |
---|
[2423] | 84 | if (!shape || static_cast<CollisionShape*>(this) == shape) |
---|
| 85 | return; |
---|
| 86 | if (this->childShapes_.find(shape) != this->childShapes_.end()) |
---|
| 87 | { |
---|
[2433] | 88 | CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl; |
---|
[2423] | 89 | return; |
---|
| 90 | } |
---|
| 91 | this->childShapes_[shape] = shape->getCollisionShape(); |
---|
| 92 | |
---|
| 93 | if (shape->getCollisionShape()) |
---|
| 94 | { |
---|
| 95 | // Only actually attach if we didn't pick a CompoundCollisionShape with no content |
---|
| 96 | btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition())); |
---|
| 97 | this->compoundShape_->addChildShape(transf, shape->getCollisionShape()); |
---|
| 98 | |
---|
| 99 | this->updatePublicShape(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | // network synchro |
---|
[2469] | 103 | if (this->worldEntityParent_) |
---|
| 104 | { |
---|
| 105 | // This compound collision shape belongs to a WE and doesn't get synchronised |
---|
| 106 | // So set our parent to be the WE |
---|
| 107 | shape->setParent(this, this->worldEntityParent_->getObjectID()); |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | shape->setParent(this, this->getObjectID()); |
---|
[2374] | 111 | } |
---|
| 112 | |
---|
[2423] | 113 | void CompoundCollisionShape::removeChildShape(CollisionShape* shape) |
---|
[2303] | 114 | { |
---|
[2423] | 115 | if (this->childShapes_.find(shape) != this->childShapes_.end()) |
---|
| 116 | { |
---|
[2445] | 117 | shape->setParent(0, OBJECTID_UNKNOWN); |
---|
[2423] | 118 | this->childShapes_.erase(shape); |
---|
| 119 | if (shape->getCollisionShape()) |
---|
| 120 | this->compoundShape_->removeChildShape(shape->getCollisionShape()); |
---|
| 121 | |
---|
| 122 | this->updatePublicShape(); |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | void CompoundCollisionShape::removeAllChildShapes() |
---|
| 127 | { |
---|
| 128 | while (this->childShapes_.size() > 0) |
---|
| 129 | this->removeChildShape(this->childShapes_.begin()->first); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void CompoundCollisionShape::updateChildShape(CollisionShape* shape) |
---|
| 133 | { |
---|
[2403] | 134 | if (!shape) |
---|
[2374] | 135 | return; |
---|
[2423] | 136 | std::map<CollisionShape*, btCollisionShape*>::iterator it = this->childShapes_.find(shape); |
---|
| 137 | if (it == this->childShapes_.end()) |
---|
| 138 | { |
---|
| 139 | CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl; |
---|
| 140 | return; |
---|
| 141 | } |
---|
[2374] | 142 | |
---|
[2423] | 143 | // Remove old btCollisionShape, stored in the children map |
---|
| 144 | if (it->second) |
---|
| 145 | this->compoundShape_->removeChildShape(it->second); |
---|
[2403] | 146 | if (shape->getCollisionShape()) |
---|
[2374] | 147 | { |
---|
[2403] | 148 | // Only actually attach if we didn't pick a CompoundCollisionShape with no content |
---|
| 149 | btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition())); |
---|
| 150 | this->compoundShape_->addChildShape(transf, shape->getCollisionShape()); |
---|
[2423] | 151 | it->second = shape->getCollisionShape(); |
---|
| 152 | } |
---|
[2403] | 153 | |
---|
[2423] | 154 | this->updatePublicShape(); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | void CompoundCollisionShape::updatePublicShape() |
---|
| 158 | { |
---|
| 159 | btCollisionShape* primitive = 0; |
---|
| 160 | bool bPrimitive = true; |
---|
| 161 | bool bEmpty = true; |
---|
| 162 | for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it) |
---|
| 163 | { |
---|
| 164 | if (it->second) |
---|
[2403] | 165 | { |
---|
[2423] | 166 | bEmpty = false; |
---|
[2484] | 167 | if (!it->first->hasTransform() && !bPrimitive) |
---|
[2423] | 168 | primitive = it->second; |
---|
| 169 | else |
---|
| 170 | bPrimitive = false; |
---|
[2403] | 171 | } |
---|
[2374] | 172 | } |
---|
[2423] | 173 | if (bEmpty) |
---|
[2463] | 174 | { |
---|
| 175 | if (this->collisionShape_ == 0) |
---|
| 176 | { |
---|
| 177 | this->collisionShape_ = 0; |
---|
| 178 | return; |
---|
| 179 | } |
---|
[2423] | 180 | this->collisionShape_ = 0; |
---|
[2463] | 181 | } |
---|
[2423] | 182 | else if (bPrimitive) |
---|
| 183 | { |
---|
| 184 | // --> Only one shape to be added, no transform; return it directly |
---|
| 185 | this->collisionShape_ = primitive; |
---|
| 186 | } |
---|
| 187 | else |
---|
| 188 | { |
---|
| 189 | // Make sure we use the compound shape when returning a btCollisionShape |
---|
| 190 | this->collisionShape_ = this->compoundShape_; |
---|
| 191 | } |
---|
| 192 | this->updateParent(); |
---|
| 193 | } |
---|
[2374] | 194 | |
---|
[2423] | 195 | void CompoundCollisionShape::updateParent() |
---|
| 196 | { |
---|
| 197 | if (this->parent_) |
---|
| 198 | this->parent_->updateChildShape(this); |
---|
[2469] | 199 | else if (this->worldEntityParent_) |
---|
| 200 | this->worldEntityParent_->notifyCollisionShapeChanged(); |
---|
[2303] | 201 | } |
---|
| 202 | |
---|
| 203 | CollisionShape* CompoundCollisionShape::getChildShape(unsigned int index) const |
---|
| 204 | { |
---|
[2423] | 205 | unsigned int i = 0; |
---|
| 206 | for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->childShapes_.begin(); it != this->childShapes_.end(); ++it) |
---|
| 207 | { |
---|
| 208 | if (i == index) |
---|
| 209 | return it->first; |
---|
| 210 | ++i; |
---|
| 211 | } |
---|
| 212 | return 0; |
---|
[2303] | 213 | } |
---|
| 214 | } |
---|