[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 "CollisionShape.h" |
---|
| 31 | |
---|
| 32 | #include "BulletCollision/CollisionShapes/btCollisionShape.h" |
---|
| 33 | |
---|
| 34 | #include "util/Exception.h" |
---|
| 35 | #include "core/CoreIncludes.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
| 37 | #include "tools/BulletConversions.h" |
---|
| 38 | |
---|
[2562] | 39 | #include "objects/worldentities/WorldEntity.h" |
---|
[2374] | 40 | #include "CompoundCollisionShape.h" |
---|
[2562] | 41 | #include "WorldEntityCollisionShape.h" |
---|
[2374] | 42 | |
---|
[2303] | 43 | namespace orxonox |
---|
| 44 | { |
---|
[2374] | 45 | CollisionShape::CollisionShape(BaseObject* creator) |
---|
| 46 | : BaseObject(creator) |
---|
[2442] | 47 | , Synchronisable(creator) |
---|
[2303] | 48 | { |
---|
| 49 | RegisterObject(CollisionShape); |
---|
| 50 | |
---|
[2374] | 51 | this->parent_ = 0; |
---|
[2445] | 52 | this->parentID_ = OBJECTID_UNKNOWN; |
---|
[2374] | 53 | this->collisionShape_ = 0; |
---|
[2403] | 54 | this->position_ = Vector3::ZERO; |
---|
| 55 | this->orientation_ = Quaternion::IDENTITY; |
---|
| 56 | this->scale_ = Vector3::UNIT_SCALE; |
---|
[2469] | 57 | |
---|
| 58 | this->registerVariables(); |
---|
[2303] | 59 | } |
---|
| 60 | |
---|
| 61 | CollisionShape::~CollisionShape() |
---|
| 62 | { |
---|
[2514] | 63 | // Detach from parent |
---|
| 64 | if (this->isInitialized() && this->parent_) |
---|
[2527] | 65 | this->parent_->detach(this); |
---|
[2303] | 66 | } |
---|
| 67 | |
---|
| 68 | void CollisionShape::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 69 | { |
---|
| 70 | SUPER(CollisionShape, XMLPort, xmlelement, mode); |
---|
[2374] | 71 | |
---|
| 72 | XMLPortParamTemplate(CollisionShape, "position", setPosition, getPosition, xmlelement, mode, const Vector3&); |
---|
| 73 | XMLPortParamTemplate(CollisionShape, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&); |
---|
[2429] | 74 | XMLPortParamTemplate(CollisionShape, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&); |
---|
| 75 | XMLPortParamLoadOnly(CollisionShape, "scale", setScale, xmlelement, mode); |
---|
[2374] | 76 | XMLPortParamLoadOnly(CollisionShape, "yaw", yaw, xmlelement, mode); |
---|
| 77 | XMLPortParamLoadOnly(CollisionShape, "pitch", pitch, xmlelement, mode); |
---|
| 78 | XMLPortParamLoadOnly(CollisionShape, "roll", roll, xmlelement, mode); |
---|
[2303] | 79 | } |
---|
| 80 | |
---|
[2374] | 81 | void CollisionShape::registerVariables() |
---|
[2303] | 82 | { |
---|
[2515] | 83 | registerVariable(this->parentID_, variableDirection::toclient, new NetworkCallback<CollisionShape>(this, &CollisionShape::parentChanged)); |
---|
[2303] | 84 | } |
---|
| 85 | |
---|
[2423] | 86 | void CollisionShape::parentChanged() |
---|
[2303] | 87 | { |
---|
[2562] | 88 | Synchronisable* parent = Synchronisable::getSynchronisable(this->parentID_); |
---|
| 89 | // Parent can either be a WorldEntity or a CompoundCollisionShape. The reason is that the |
---|
| 90 | // internal collision shape (which is compound) of a WE doesn't get synchronised. |
---|
| 91 | CompoundCollisionShape* parentCCS = dynamic_cast<CompoundCollisionShape*>(parent); |
---|
| 92 | if (parentCCS) |
---|
| 93 | parentCCS->attach(this); |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | WorldEntity* parentWE = dynamic_cast<WorldEntity*>(parent); |
---|
| 97 | if (parentWE) |
---|
| 98 | parentWE->attachCollisionShape(this); |
---|
| 99 | } |
---|
[2303] | 100 | } |
---|
| 101 | |
---|
[2562] | 102 | bool CollisionShape::notifyBeingAttached(CompoundCollisionShape* newParent) |
---|
| 103 | { |
---|
| 104 | if (this->parent_) |
---|
| 105 | this->parent_->detach(this); |
---|
| 106 | |
---|
| 107 | this->parent_ = newParent; |
---|
| 108 | |
---|
| 109 | WorldEntityCollisionShape* parentWECCS = dynamic_cast<WorldEntityCollisionShape*>(newParent); |
---|
| 110 | if (parentWECCS) |
---|
| 111 | this->parentID_ = parentWECCS->getWorldEntityOwner()->getObjectID(); |
---|
| 112 | else |
---|
| 113 | this->parentID_ = newParent->getObjectID(); |
---|
| 114 | |
---|
| 115 | return true; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void CollisionShape::notifyDetached() |
---|
| 119 | { |
---|
| 120 | this->parent_ = 0; |
---|
| 121 | this->parentID_ = OBJECTID_UNKNOWN; |
---|
| 122 | } |
---|
| 123 | |
---|
[2423] | 124 | void CollisionShape::updateParent() |
---|
| 125 | { |
---|
| 126 | if (this->parent_) |
---|
[2527] | 127 | this->parent_->updateAttachedShape(this); |
---|
[2423] | 128 | } |
---|
| 129 | |
---|
[2374] | 130 | bool CollisionShape::hasTransform() const |
---|
[2303] | 131 | { |
---|
[2374] | 132 | return (!this->position_.positionEquals(Vector3(0, 0, 0), 0.001) || |
---|
| 133 | !this->orientation_.equals(Quaternion(1,0,0,0), Degree(0.1))); |
---|
[2303] | 134 | } |
---|
[2306] | 135 | |
---|
| 136 | void CollisionShape::setScale3D(const Vector3& scale) |
---|
| 137 | { |
---|
[2433] | 138 | CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl; |
---|
[2306] | 139 | } |
---|
| 140 | |
---|
[2374] | 141 | void CollisionShape::setScale(float scale) |
---|
[2306] | 142 | { |
---|
[2433] | 143 | CCOUT(2) << "Warning: Cannot set the scale of a collision shape: Not yet implemented." << std::endl; |
---|
[2306] | 144 | } |
---|
[2423] | 145 | |
---|
[2514] | 146 | void CollisionShape::updateShape() |
---|
| 147 | { |
---|
| 148 | btCollisionShape* oldShape = this->collisionShape_; |
---|
| 149 | this->collisionShape_ = this->createNewShape(); |
---|
| 150 | // When we recreate the shape, we have to inform the parent about this to update the shape |
---|
| 151 | this->updateParent(); |
---|
| 152 | if (oldShape) |
---|
| 153 | delete oldShape; |
---|
| 154 | } |
---|
| 155 | |
---|
[2452] | 156 | void CollisionShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const |
---|
[2423] | 157 | { |
---|
| 158 | if (this->collisionShape_) |
---|
| 159 | this->collisionShape_->calculateLocalInertia(mass, inertia); |
---|
[2452] | 160 | else |
---|
| 161 | inertia.setValue(0, 0, 0); |
---|
[2423] | 162 | } |
---|
[2303] | 163 | } |
---|