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