[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 | { |
---|
[2514] | 56 | // Delete all children |
---|
[2527] | 57 | for (std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.begin(); |
---|
| 58 | it != this->attachedShapes_.end(); ++it) |
---|
[2477] | 59 | { |
---|
[2514] | 60 | // make sure that the child doesn't want to detach itself --> speedup because of the missing update |
---|
[2477] | 61 | it->first->setParent(0, OBJECTID_UNKNOWN); |
---|
[2514] | 62 | delete it->first; |
---|
[2477] | 63 | } |
---|
| 64 | |
---|
[2303] | 65 | delete this->compoundShape_; |
---|
[2423] | 66 | } |
---|
[2303] | 67 | } |
---|
| 68 | |
---|
| 69 | void CompoundCollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 70 | { |
---|
| 71 | SUPER(CompoundCollisionShape, XMLPort, xmlelement, mode); |
---|
[2374] | 72 | // Attached collision shapes |
---|
[2527] | 73 | XMLPortObject(CompoundCollisionShape, CollisionShape, "", attach, detach, xmlelement, mode); |
---|
[2303] | 74 | } |
---|
| 75 | |
---|
[2469] | 76 | void CompoundCollisionShape::setWorldEntityParent(WorldEntity* parent) |
---|
| 77 | { |
---|
| 78 | // suppress synchronisation |
---|
| 79 | this->setObjectMode(0x0); |
---|
| 80 | |
---|
| 81 | this->worldEntityParent_ = parent; |
---|
| 82 | } |
---|
| 83 | |
---|
[2527] | 84 | void CompoundCollisionShape::attach(CollisionShape* shape) |
---|
[2374] | 85 | { |
---|
[2423] | 86 | if (!shape || static_cast<CollisionShape*>(this) == shape) |
---|
| 87 | return; |
---|
[2527] | 88 | if (this->attachedShapes_.find(shape) != this->attachedShapes_.end()) |
---|
[2423] | 89 | { |
---|
[2433] | 90 | CCOUT(2) << "Warning: Attaching a CollisionShape twice is not yet supported." << std::endl; |
---|
[2423] | 91 | return; |
---|
| 92 | } |
---|
[2527] | 93 | this->attachedShapes_[shape] = shape->getCollisionShape(); |
---|
[2423] | 94 | |
---|
| 95 | if (shape->getCollisionShape()) |
---|
| 96 | { |
---|
| 97 | // Only actually attach if we didn't pick a CompoundCollisionShape with no content |
---|
| 98 | btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition())); |
---|
| 99 | this->compoundShape_->addChildShape(transf, shape->getCollisionShape()); |
---|
| 100 | |
---|
| 101 | this->updatePublicShape(); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | // network synchro |
---|
[2469] | 105 | if (this->worldEntityParent_) |
---|
| 106 | { |
---|
| 107 | // This compound collision shape belongs to a WE and doesn't get synchronised |
---|
| 108 | // So set our parent to be the WE |
---|
| 109 | shape->setParent(this, this->worldEntityParent_->getObjectID()); |
---|
| 110 | } |
---|
| 111 | else |
---|
| 112 | shape->setParent(this, this->getObjectID()); |
---|
[2374] | 113 | } |
---|
| 114 | |
---|
[2527] | 115 | void CompoundCollisionShape::detach(CollisionShape* shape) |
---|
[2303] | 116 | { |
---|
[2527] | 117 | if (this->attachedShapes_.find(shape) != this->attachedShapes_.end()) |
---|
[2423] | 118 | { |
---|
[2445] | 119 | shape->setParent(0, OBJECTID_UNKNOWN); |
---|
[2527] | 120 | this->attachedShapes_.erase(shape); |
---|
[2423] | 121 | if (shape->getCollisionShape()) |
---|
| 122 | this->compoundShape_->removeChildShape(shape->getCollisionShape()); |
---|
| 123 | |
---|
| 124 | this->updatePublicShape(); |
---|
| 125 | } |
---|
[2527] | 126 | else |
---|
| 127 | CCOUT(2) << "Warning: Cannot detach non child collision shape" << std::endl; |
---|
[2423] | 128 | } |
---|
| 129 | |
---|
[2527] | 130 | void CompoundCollisionShape::detachAll() |
---|
[2423] | 131 | { |
---|
[2527] | 132 | while (this->attachedShapes_.size() > 0) |
---|
| 133 | this->detach(this->attachedShapes_.begin()->first); |
---|
[2423] | 134 | } |
---|
| 135 | |
---|
[2527] | 136 | void CompoundCollisionShape::updateAttachedShape(CollisionShape* shape) |
---|
[2423] | 137 | { |
---|
[2403] | 138 | if (!shape) |
---|
[2374] | 139 | return; |
---|
[2527] | 140 | std::map<CollisionShape*, btCollisionShape*>::iterator it = this->attachedShapes_.find(shape); |
---|
| 141 | if (it == this->attachedShapes_.end()) |
---|
[2423] | 142 | { |
---|
| 143 | CCOUT(2) << "Warning: Cannot update child shape: Instance not a child." << std::endl; |
---|
| 144 | return; |
---|
| 145 | } |
---|
[2374] | 146 | |
---|
[2423] | 147 | // Remove old btCollisionShape, stored in the children map |
---|
| 148 | if (it->second) |
---|
| 149 | this->compoundShape_->removeChildShape(it->second); |
---|
[2403] | 150 | if (shape->getCollisionShape()) |
---|
[2374] | 151 | { |
---|
[2403] | 152 | // Only actually attach if we didn't pick a CompoundCollisionShape with no content |
---|
| 153 | btTransform transf(omni_cast<btQuaternion>(shape->getOrientation()), omni_cast<btVector3>(shape->getPosition())); |
---|
| 154 | this->compoundShape_->addChildShape(transf, shape->getCollisionShape()); |
---|
[2423] | 155 | it->second = shape->getCollisionShape(); |
---|
| 156 | } |
---|
[2403] | 157 | |
---|
[2423] | 158 | this->updatePublicShape(); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | void CompoundCollisionShape::updatePublicShape() |
---|
| 162 | { |
---|
| 163 | btCollisionShape* primitive = 0; |
---|
| 164 | bool bPrimitive = true; |
---|
| 165 | bool bEmpty = true; |
---|
[2527] | 166 | for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it) |
---|
[2423] | 167 | { |
---|
| 168 | if (it->second) |
---|
[2403] | 169 | { |
---|
[2423] | 170 | bEmpty = false; |
---|
[2484] | 171 | if (!it->first->hasTransform() && !bPrimitive) |
---|
[2423] | 172 | primitive = it->second; |
---|
| 173 | else |
---|
| 174 | bPrimitive = false; |
---|
[2403] | 175 | } |
---|
[2374] | 176 | } |
---|
[2423] | 177 | if (bEmpty) |
---|
[2463] | 178 | { |
---|
| 179 | if (this->collisionShape_ == 0) |
---|
| 180 | { |
---|
| 181 | this->collisionShape_ = 0; |
---|
| 182 | return; |
---|
| 183 | } |
---|
[2423] | 184 | this->collisionShape_ = 0; |
---|
[2463] | 185 | } |
---|
[2423] | 186 | else if (bPrimitive) |
---|
| 187 | { |
---|
| 188 | // --> Only one shape to be added, no transform; return it directly |
---|
| 189 | this->collisionShape_ = primitive; |
---|
| 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | // Make sure we use the compound shape when returning a btCollisionShape |
---|
| 194 | this->collisionShape_ = this->compoundShape_; |
---|
| 195 | } |
---|
| 196 | this->updateParent(); |
---|
| 197 | } |
---|
[2374] | 198 | |
---|
[2423] | 199 | void CompoundCollisionShape::updateParent() |
---|
| 200 | { |
---|
| 201 | if (this->parent_) |
---|
[2527] | 202 | this->parent_->updateAttachedShape(this); |
---|
[2514] | 203 | if (this->worldEntityParent_) |
---|
[2469] | 204 | this->worldEntityParent_->notifyCollisionShapeChanged(); |
---|
[2303] | 205 | } |
---|
| 206 | |
---|
[2514] | 207 | void CompoundCollisionShape::parentChanged() |
---|
| 208 | { |
---|
| 209 | if (!this->worldEntityParent_) |
---|
| 210 | CollisionShape::parentChanged(); |
---|
| 211 | } |
---|
| 212 | |
---|
[2527] | 213 | CollisionShape* CompoundCollisionShape::getAttachedShape(unsigned int index) const |
---|
[2303] | 214 | { |
---|
[2423] | 215 | unsigned int i = 0; |
---|
[2527] | 216 | for (std::map<CollisionShape*, btCollisionShape*>::const_iterator it = this->attachedShapes_.begin(); it != this->attachedShapes_.end(); ++it) |
---|
[2423] | 217 | { |
---|
| 218 | if (i == index) |
---|
| 219 | return it->first; |
---|
| 220 | ++i; |
---|
| 221 | } |
---|
| 222 | return 0; |
---|
[2303] | 223 | } |
---|
| 224 | } |
---|