1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15 
16 #ifndef BT_COLLISION_SHAPE_H
17 #define BT_COLLISION_SHAPE_H
18 
19 #include "LinearMath/btTransform.h"
20 #include "LinearMath/btVector3.h"
21 #include "LinearMath/btMatrix3x3.h"
22 #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"  //for the shape types
23 class btSerializer;
24 
25 ///The btCollisionShape class provides an interface for collision shapes that can be shared among btCollisionObjects.
ATTRIBUTE_ALIGNED16(class)26 ATTRIBUTE_ALIGNED16(class)
27 btCollisionShape
28 {
29 protected:
30 	int m_shapeType;
31 	void* m_userPointer;
32 	int m_userIndex;
33 
34 public:
35 	BT_DECLARE_ALIGNED_ALLOCATOR();
36 
37 	btCollisionShape() : m_shapeType(INVALID_SHAPE_PROXYTYPE), m_userPointer(0), m_userIndex(-1)
38 	{
39 	}
40 
41 	virtual ~btCollisionShape()
42 	{
43 	}
44 
45 	///getAabb returns the axis aligned bounding box in the coordinate frame of the given transform t.
46 	virtual void getAabb(const btTransform& t, btVector3& aabbMin, btVector3& aabbMax) const = 0;
47 
48 	virtual void getBoundingSphere(btVector3 & center, btScalar & radius) const;
49 
50 	///getAngularMotionDisc returns the maximum radius needed for Conservative Advancement to handle time-of-impact with rotations.
51 	virtual btScalar getAngularMotionDisc() const;
52 
53 	virtual btScalar getContactBreakingThreshold(btScalar defaultContactThresholdFactor) const;
54 
55 	///calculateTemporalAabb calculates the enclosing aabb for the moving object over interval [0..timeStep)
56 	///result is conservative
57 	void calculateTemporalAabb(const btTransform& curTrans, const btVector3& linvel, const btVector3& angvel, btScalar timeStep, btVector3& temporalAabbMin, btVector3& temporalAabbMax) const;
58 
59 	SIMD_FORCE_INLINE bool isPolyhedral() const
60 	{
61 		return btBroadphaseProxy::isPolyhedral(getShapeType());
62 	}
63 
64 	SIMD_FORCE_INLINE bool isConvex2d() const
65 	{
66 		return btBroadphaseProxy::isConvex2d(getShapeType());
67 	}
68 
69 	SIMD_FORCE_INLINE bool isConvex() const
70 	{
71 		return btBroadphaseProxy::isConvex(getShapeType());
72 	}
73 	SIMD_FORCE_INLINE bool isNonMoving() const
74 	{
75 		return btBroadphaseProxy::isNonMoving(getShapeType());
76 	}
77 	SIMD_FORCE_INLINE bool isConcave() const
78 	{
79 		return btBroadphaseProxy::isConcave(getShapeType());
80 	}
81 	SIMD_FORCE_INLINE bool isCompound() const
82 	{
83 		return btBroadphaseProxy::isCompound(getShapeType());
84 	}
85 
86 	SIMD_FORCE_INLINE bool isSoftBody() const
87 	{
88 		return btBroadphaseProxy::isSoftBody(getShapeType());
89 	}
90 
91 	///isInfinite is used to catch simulation error (aabb check)
92 	SIMD_FORCE_INLINE bool isInfinite() const
93 	{
94 		return btBroadphaseProxy::isInfinite(getShapeType());
95 	}
96 
97 #ifndef __SPU__
98 	virtual void setLocalScaling(const btVector3& scaling) = 0;
99 	virtual const btVector3& getLocalScaling() const = 0;
100 	virtual void calculateLocalInertia(btScalar mass, btVector3 & inertia) const = 0;
101 
102 	//debugging support
103 	virtual const char* getName() const = 0;
104 #endif  //__SPU__
105 
106 	int getShapeType() const
107 	{
108 		return m_shapeType;
109 	}
110 
111 	///the getAnisotropicRollingFrictionDirection can be used in combination with setAnisotropicFriction
112 	///See Bullet/Demos/RollingFrictionDemo for an example
113 	virtual btVector3 getAnisotropicRollingFrictionDirection() const
114 	{
115 		return btVector3(1, 1, 1);
116 	}
117 	virtual void setMargin(btScalar margin) = 0;
118 	virtual btScalar getMargin() const = 0;
119 
120 	///optional user data pointer
121 	void setUserPointer(void* userPtr)
122 	{
123 		m_userPointer = userPtr;
124 	}
125 
126 	void* getUserPointer() const
127 	{
128 		return m_userPointer;
129 	}
130 	void setUserIndex(int index)
131 	{
132 		m_userIndex = index;
133 	}
134 
135 	int getUserIndex() const
136 	{
137 		return m_userIndex;
138 	}
139 
140 	virtual int calculateSerializeBufferSize() const;
141 
142 	///fills the dataBuffer and returns the struct name (and 0 on failure)
143 	virtual const char* serialize(void* dataBuffer, btSerializer* serializer) const;
144 
145 	virtual void serializeSingleShape(btSerializer * serializer) const;
146 };
147 
148 // clang-format off
149 // parser needs * with the name
150 ///do not change those serialization structures, it requires an updated sBulletDNAstr/sBulletDNAstr64
151 struct	btCollisionShapeData
152 {
153 	char	*m_name;
154 	int		m_shapeType;
155 	char	m_padding[4];
156 };
157 // clang-format on
calculateSerializeBufferSize()158 SIMD_FORCE_INLINE int btCollisionShape::calculateSerializeBufferSize() const
159 {
160 	return sizeof(btCollisionShapeData);
161 }
162 
163 #endif  //BT_COLLISION_SHAPE_H
164