[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 | |
---|
[8422] | 29 | /** |
---|
| 30 | @file CollisionShape.h |
---|
| 31 | @brief Definition of the CollisionShape class. |
---|
| 32 | @ingroup Collisionshapes |
---|
| 33 | */ |
---|
| 34 | |
---|
[2303] | 35 | #ifndef _CollisionShape_H__ |
---|
| 36 | #define _CollisionShape_H__ |
---|
| 37 | |
---|
| 38 | #include "OrxonoxPrereqs.h" |
---|
| 39 | |
---|
[2374] | 40 | #include "util/Math.h" |
---|
| 41 | #include "core/BaseObject.h" |
---|
[2442] | 42 | #include "network/synchronisable/Synchronisable.h" |
---|
[2303] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
[8422] | 46 | |
---|
| 47 | /** |
---|
| 48 | @brief |
---|
| 49 | Wrapper for bullet collision shape class btCollisionShape. |
---|
| 50 | |
---|
| 51 | @author |
---|
| 52 | Reto Grieder |
---|
| 53 | |
---|
| 54 | @see btCollisionShape |
---|
| 55 | @ingroup Collisionshapes |
---|
| 56 | */ |
---|
[2442] | 57 | class _OrxonoxExport CollisionShape : public BaseObject, public Synchronisable |
---|
[2303] | 58 | { |
---|
| 59 | public: |
---|
| 60 | CollisionShape(BaseObject* creator); |
---|
| 61 | virtual ~CollisionShape(); |
---|
| 62 | |
---|
| 63 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); |
---|
| 64 | |
---|
[8422] | 65 | /** |
---|
| 66 | @brief Set the position of the CollisionShape. |
---|
| 67 | If the position changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 68 | @param position A vector indicating the new position. |
---|
| 69 | */ |
---|
[2374] | 70 | inline void setPosition(const Vector3& position) |
---|
[8422] | 71 | { if(this->position_ == position) return; this->position_ = position; this->updateParent(); } |
---|
| 72 | /** |
---|
| 73 | @brief Get the position of the CollisionShape. |
---|
| 74 | @return Returns the position of the CollisionShape as a vector. |
---|
| 75 | */ |
---|
[2374] | 76 | inline const Vector3& getPosition() const |
---|
| 77 | { return this->position_; } |
---|
[2303] | 78 | |
---|
[8422] | 79 | /** |
---|
| 80 | @brief Set the orientation of the CollisionShape. |
---|
| 81 | If the orientation changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 82 | @param orientation A quaternion indicating the new orientation. |
---|
| 83 | */ |
---|
[2374] | 84 | inline void setOrientation(const Quaternion& orientation) |
---|
[8422] | 85 | { if(this->orientation_ == orientation) return; this->orientation_ = orientation; this->updateParent(); } |
---|
| 86 | /** |
---|
| 87 | @brief Get the orientation of the CollisionShape. |
---|
| 88 | @return Returns the orientation of the CollisionShape as a quaternion. |
---|
| 89 | */ |
---|
[2374] | 90 | inline const Quaternion& getOrientation() const |
---|
| 91 | { return this->orientation_; } |
---|
[2303] | 92 | |
---|
[8422] | 93 | /** |
---|
| 94 | @brief Rotate the CollisionShape around the y-axis by the specified angle. |
---|
| 95 | If the orientation changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 96 | @param angle The angle by which the CollisionShape is rotated. |
---|
| 97 | */ |
---|
| 98 | void yaw(const Degree& angle) |
---|
| 99 | { this->setOrientation(this->orientation_ * Quaternion(angle, Vector3::UNIT_Y)); } |
---|
| 100 | /** |
---|
| 101 | @brief Rotate the CollisionShape around the x-axis by the specified angle. |
---|
| 102 | If the orientation changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 103 | @param angle The angle by which the CollisionShape is rotated. |
---|
| 104 | */ |
---|
| 105 | void pitch(const Degree& angle) |
---|
| 106 | { this->setOrientation(this->orientation_ * Quaternion(angle, Vector3::UNIT_X)); } |
---|
| 107 | /** |
---|
| 108 | @brief Rotate the CollisionShape around the z-axis by the specified angle. |
---|
| 109 | If the orientation changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 110 | @param angle The angle by which the CollisionShape is rotated. |
---|
| 111 | */ |
---|
| 112 | void roll(const Degree& angle) |
---|
| 113 | { this->setOrientation(this->orientation_ * Quaternion(angle, Vector3::UNIT_Z)); } |
---|
[2374] | 114 | |
---|
[8422] | 115 | /** |
---|
| 116 | @brief Scale the CollisionShape by the input vector. |
---|
| 117 | Since the scale is a vector the CollisionShape can be scaled independently in each direction, allowing for linear distortions. |
---|
| 118 | If the scale changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 119 | Beware, non-uniform scaling (i.e. distortions) might not be supported by all CollisionShapes. |
---|
| 120 | @param scale The scaling vector by which the CollisionShape is scaled. |
---|
| 121 | */ |
---|
| 122 | inline void scale3D(const Vector3& scale) |
---|
| 123 | { this->setScale3D(this->getScale3D()*scale); } |
---|
| 124 | void setScale3D(const Vector3& scale); // Set the scale of the CollisionShape. |
---|
| 125 | /** |
---|
| 126 | @brief Get the scale of the CollisionShape. |
---|
| 127 | @return Returns a vector indicating the scale of the CollisionShape. |
---|
| 128 | */ |
---|
[3301] | 129 | inline const Vector3& getScale3D() const |
---|
[2374] | 130 | { return this->scale_; } |
---|
| 131 | |
---|
[8422] | 132 | /** |
---|
| 133 | @brief (Uniformly) scale the CollisionShape by the input scale. |
---|
| 134 | If the scale changes, this causes the parent CompoundCollisionShape (if there is one) to be updated. |
---|
| 135 | @param scale The value by which the CollisionShape is scaled. |
---|
| 136 | */ |
---|
| 137 | inline void scale(float scale) |
---|
| 138 | { this->setScale3D(this->getScale3D()*scale); } |
---|
| 139 | void setScale(float scale); // Set the (uniform) scale of the CollisionShape. |
---|
| 140 | /** |
---|
| 141 | @brief Get the (uniform) scale of the CollisionShape. |
---|
| 142 | This is only meaningful if the CollisionShape has uniform scaling. |
---|
| 143 | @return Returns the (uniform) scale of the CollisionShape. Returns 0.0f if the scaling is non-uniform. |
---|
| 144 | */ |
---|
| 145 | inline float getScale() |
---|
| 146 | { if(this->hasUniformScaling()) return this->scale_.x; return 0.0f; } |
---|
[2514] | 147 | |
---|
[8422] | 148 | /** |
---|
| 149 | @brief Check whether the CollisionShape is uniformly scaled. |
---|
| 150 | @return Returns true if the CollisionShape is uniformly scaled, false if not. |
---|
| 151 | */ |
---|
| 152 | inline bool hasUniformScaling() |
---|
| 153 | { return this->uniformScale_; } |
---|
| 154 | virtual void changedScale(); // Is called when the scale of the CollisionShape has changed. |
---|
[2423] | 155 | |
---|
[8422] | 156 | void updateShape(); // Updates the shape. |
---|
| 157 | |
---|
| 158 | void calculateLocalInertia(float mass, btVector3& inertia) const; // Calculates the local inertia of the collision shape. |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | @brief Get the bullet collision shape of this CollisionShape. |
---|
| 162 | @return Returns a pointer to the bullet collision shape of this CollisionShape. |
---|
| 163 | */ |
---|
[2423] | 164 | inline btCollisionShape* getCollisionShape() const |
---|
[2374] | 165 | { return this->collisionShape_; } |
---|
| 166 | |
---|
[8422] | 167 | bool hasTransform() const; // Check whether the CollisionShape has been either moved or rotated or both. |
---|
[2374] | 168 | |
---|
[8422] | 169 | bool notifyBeingAttached(CompoundCollisionShape* newParent); // Notifies the CollisionShape of being attached to a CompoundCollisionShape. |
---|
| 170 | void notifyDetached(); // Notifies the CollisionShape of being detached from a CompoundCollisionShape. |
---|
[2374] | 171 | |
---|
[2303] | 172 | protected: |
---|
[8422] | 173 | virtual void updateParent(); // Updates the CompoundCollisionShape the CollisionShape belongs to, after the CollisionShape has changed. |
---|
| 174 | virtual void parentChanged(); // Is called when the parentID of the CollisionShape has changed. |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | @brief Create a new bullet collision shape depending on the internal parameters of the specific CollisionShape. |
---|
| 178 | @return Returns a pointer to the new bullet collision shape. |
---|
| 179 | */ |
---|
[2514] | 180 | virtual btCollisionShape* createNewShape() const = 0; |
---|
[2303] | 181 | |
---|
[8422] | 182 | btCollisionShape* collisionShape_; //!< The bullet collision shape of this CollisionShape. |
---|
| 183 | CompoundCollisionShape* parent_; //!< The CompoundCollisionShape this CollisionShape belongs to, NULL if it doesn't belong to one. |
---|
| 184 | unsigned int parentID_; //!< The objectID of the parent of this CollisionShape, which can either be a CompoundCollisionShape or a WorldEntity. |
---|
[2423] | 185 | |
---|
[2303] | 186 | private: |
---|
[7163] | 187 | void registerVariables(); |
---|
| 188 | |
---|
[8422] | 189 | Vector3 position_; //!< The position of the CollisionShape. |
---|
| 190 | Quaternion orientation_; //!< The orientation of the CollisionShape. |
---|
| 191 | Vector3 scale_; //!< The scale of the CollisionShape. |
---|
| 192 | bool uniformScale_; //!< Whether the scale is uniform. |
---|
[2303] | 193 | }; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | #endif /* _CollisionShape_H__ */ |
---|