| 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: | 
|---|
| 23 | *      Reto Grieder | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 | @file CollisionShape.h | 
|---|
| 31 | @brief Definition of the CollisionShape class. | 
|---|
| 32 | @ingroup Collisionshapes | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #ifndef _CollisionShape_H__ | 
|---|
| 36 | #define _CollisionShape_H__ | 
|---|
| 37 |  | 
|---|
| 38 | #include "OrxonoxPrereqs.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "util/Math.h" | 
|---|
| 41 | #include "core/BaseObject.h" | 
|---|
| 42 | #include "network/synchronisable/Synchronisable.h" | 
|---|
| 43 |  | 
|---|
| 44 | namespace orxonox | 
|---|
| 45 | { | 
|---|
| 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 | */ | 
|---|
| 57 | class _OrxonoxExport CollisionShape : public BaseObject, public Synchronisable | 
|---|
| 58 | { | 
|---|
| 59 | public: | 
|---|
| 60 | CollisionShape(Context* context); | 
|---|
| 61 | virtual ~CollisionShape(); | 
|---|
| 62 |  | 
|---|
| 63 | virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override; | 
|---|
| 64 |  | 
|---|
| 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 | */ | 
|---|
| 70 | inline void setPosition(const Vector3& position) | 
|---|
| 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 | */ | 
|---|
| 76 | inline const Vector3& getPosition() const | 
|---|
| 77 | { return this->position_; } | 
|---|
| 78 |  | 
|---|
| 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 | */ | 
|---|
| 84 | inline void setOrientation(const Quaternion& orientation) | 
|---|
| 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 | */ | 
|---|
| 90 | inline const Quaternion& getOrientation() const | 
|---|
| 91 | { return this->orientation_; } | 
|---|
| 92 |  | 
|---|
| 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)); } | 
|---|
| 114 |  | 
|---|
| 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 | */ | 
|---|
| 129 | inline const Vector3& getScale3D() const | 
|---|
| 130 | { return this->scale_; } | 
|---|
| 131 |  | 
|---|
| 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; } | 
|---|
| 147 |  | 
|---|
| 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. | 
|---|
| 155 |  | 
|---|
| 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 | */ | 
|---|
| 164 | inline btCollisionShape* getCollisionShape() const | 
|---|
| 165 | { return this->collisionShape_; } | 
|---|
| 166 |  | 
|---|
| 167 | bool hasTransform() const; // Check whether the CollisionShape has been either moved or rotated or both. | 
|---|
| 168 |  | 
|---|
| 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. | 
|---|
| 171 |  | 
|---|
| 172 | protected: | 
|---|
| 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 | */ | 
|---|
| 180 | virtual btCollisionShape* createNewShape() const = 0; | 
|---|
| 181 |  | 
|---|
| 182 | btCollisionShape*       collisionShape_; //!< The bullet collision shape of this CollisionShape. | 
|---|
| 183 | CompoundCollisionShape* parent_; //!< The CompoundCollisionShape this CollisionShape belongs to, nullptr 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. | 
|---|
| 185 |  | 
|---|
| 186 | private: | 
|---|
| 187 | void registerVariables(); | 
|---|
| 188 |  | 
|---|
| 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. | 
|---|
| 193 | }; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | #endif /* _CollisionShape_H__ */ | 
|---|