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