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