/******************************************************************************** * ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * * Copyright (c) 2010-2013 Daniel Chappuis * ********************************************************************************* * * * This software is provided 'as-is', without any express or implied warranty. * * In no event will the authors be held liable for any damages arising from the * * use of this software. * * * * Permission is granted to anyone to use this software for any purpose, * * including commercial applications, and to alter it and redistribute it * * freely, subject to the following restrictions: * * * * 1. The origin of this software must not be misrepresented; you must not claim * * that you wrote the original software. If you use this software in a * * product, an acknowledgment in the product documentation would be * * appreciated but is not required. * * * * 2. Altered source versions must be plainly marked as such, and must not be * * misrepresented as being the original software. * * * * 3. This notice may not be removed or altered from any source distribution. * * * ********************************************************************************/ #ifndef REACTPHYSICS3D_CYLINDER_SHAPE_H #define REACTPHYSICS3D_CYLINDER_SHAPE_H // Libraries #include "CollisionShape.h" #include "../../mathematics/mathematics.h" /// ReactPhysics3D namespace namespace reactphysics3d { // Class CylinderShape /** * This class represents a cylinder collision shape around the Y axis * and centered at the origin. The cylinder is defined by its height * and the radius of its base. The "transform" of the corresponding * rigid body gives an orientation and a position to the cylinder. * This collision shape uses an extra margin distance around it for collision * detection purpose. The default margin is 4cm (if your units are meters, * which is recommended). In case, you want to simulate small objects * (smaller than the margin distance), you might want to reduce the margin by * specifying your own margin distance using the "margin" parameter in the * constructor of the cylinder shape. Otherwise, it is recommended to use the * default margin distance by not using the "margin" parameter in the constructor. */ class CylinderShape : public CollisionShape { private : // -------------------- Attributes -------------------- // /// Radius of the base decimal mRadius; /// Half height of the cone decimal mHalfHeight; // -------------------- Methods -------------------- // /// Private copy-constructor CylinderShape(const CylinderShape& shape); /// Private assignment operator CylinderShape& operator=(const CylinderShape& shape); public : // -------------------- Methods -------------------- // /// Constructor CylinderShape(decimal radius, decimal height, decimal margin = OBJECT_MARGIN); /// Destructor virtual ~CylinderShape(); /// Allocate and return a copy of the object virtual CylinderShape* clone(void* allocatedMemory) const; /// Return the radius decimal getRadius() const; /// Return the height decimal getHeight() const; /// Return the number of bytes used by the collision shape virtual size_t getSizeInBytes() const; /// Return a local support point in a given direction with the object margin virtual Vector3 getLocalSupportPointWithMargin(const Vector3& direction); /// Return a local support point in a given direction without the object margin virtual Vector3 getLocalSupportPointWithoutMargin(const Vector3& direction); /// Return the local bounds of the shape in x, y and z directions virtual void getLocalBounds(Vector3& min, Vector3& max) const; /// Return the local inertia tensor of the collision shape virtual void computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const; /// Test equality between two cylinder shapes virtual bool isEqualTo(const CollisionShape& otherCollisionShape) const; }; /// Allocate and return a copy of the object inline CylinderShape* CylinderShape::clone(void* allocatedMemory) const { return new (allocatedMemory) CylinderShape(*this); } // Return the radius inline decimal CylinderShape::getRadius() const { return mRadius; } // Return the height inline decimal CylinderShape::getHeight() const { return mHalfHeight + mHalfHeight; } // Return the number of bytes used by the collision shape inline size_t CylinderShape::getSizeInBytes() const { return sizeof(CylinderShape); } // Return the local bounds of the shape in x, y and z directions inline void CylinderShape::getLocalBounds(Vector3& min, Vector3& max) const { // Maximum bounds max.x = mRadius + mMargin; max.y = mHalfHeight + mMargin; max.z = max.x; // Minimum bounds min.x = -max.x; min.y = -max.y; min.z = min.x; } // Return the local inertia tensor of the cylinder inline void CylinderShape::computeLocalInertiaTensor(Matrix3x3& tensor, decimal mass) const { decimal height = decimal(2.0) * mHalfHeight; decimal diag = (decimal(1.0) / decimal(12.0)) * mass * (3 * mRadius * mRadius + height * height); tensor.setAllValues(diag, 0.0, 0.0, 0.0, decimal(0.5) * mass * mRadius * mRadius, 0.0, 0.0, 0.0, diag); } // Test equality between two cylinder shapes inline bool CylinderShape::isEqualTo(const CollisionShape& otherCollisionShape) const { const CylinderShape& otherShape = dynamic_cast(otherCollisionShape); return (mRadius == otherShape.mRadius && mHalfHeight == otherShape.mHalfHeight); } } #endif