Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib2/src/external/bullet/BulletCollision/CollisionShapes/btCylinderShape.cpp @ 8332

Last change on this file since 8332 was 8284, checked in by rgrieder, 14 years ago

Merged revisions 7978 - 8096 from kicklib to kicklib2.

  • Property svn:eol-style set to native
File size: 5.7 KB
RevLine 
[1963]1/*
2Bullet Continuous Collision Detection and Physics Library
[8284]3Copyright (c) 2003-2009 Erwin Coumans  http://bulletphysics.org
[1963]4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
[8284]15
[1963]16#include "btCylinderShape.h"
17
18btCylinderShape::btCylinderShape (const btVector3& halfExtents)
[8284]19:btConvexInternalShape(),
[1963]20m_upAxis(1)
21{
[8284]22        btVector3 margin(getMargin(),getMargin(),getMargin());
23        m_implicitShapeDimensions = (halfExtents * m_localScaling) - margin;
[1963]24        m_shapeType = CYLINDER_SHAPE_PROXYTYPE;
25}
26
27
28btCylinderShapeX::btCylinderShapeX (const btVector3& halfExtents)
29:btCylinderShape(halfExtents)
30{
31        m_upAxis = 0;
[8284]32
[1963]33}
34
35
36btCylinderShapeZ::btCylinderShapeZ (const btVector3& halfExtents)
37:btCylinderShape(halfExtents)
38{
39        m_upAxis = 2;
[8284]40
[1963]41}
42
43void btCylinderShape::getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax) const
44{
[8284]45        btTransformAabb(getHalfExtentsWithoutMargin(),getMargin(),t,aabbMin,aabbMax);
[1963]46}
47
[8284]48void    btCylinderShape::calculateLocalInertia(btScalar mass,btVector3& inertia) const
49{
50        //approximation of box shape, todo: implement cylinder shape inertia before people notice ;-)
51        btVector3 halfExtents = getHalfExtentsWithMargin();
[1963]52
[8284]53        btScalar lx=btScalar(2.)*(halfExtents.x());
54        btScalar ly=btScalar(2.)*(halfExtents.y());
55        btScalar lz=btScalar(2.)*(halfExtents.z());
56
57        inertia.setValue(mass/(btScalar(12.0)) * (ly*ly + lz*lz),
58                                        mass/(btScalar(12.0)) * (lx*lx + lz*lz),
59                                        mass/(btScalar(12.0)) * (lx*lx + ly*ly));
60
61}
62
63
[1963]64SIMD_FORCE_INLINE btVector3 CylinderLocalSupportX(const btVector3& halfExtents,const btVector3& v) 
65{
66const int cylinderUpAxis = 0;
67const int XX = 1;
68const int YY = 0;
69const int ZZ = 2;
70
71        //mapping depends on how cylinder local orientation is
72        // extents of the cylinder is: X,Y is for radius, and Z for height
73
74
75        btScalar radius = halfExtents[XX];
76        btScalar halfHeight = halfExtents[cylinderUpAxis];
77
78
79    btVector3 tmp;
80        btScalar d ;
81
82    btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
83    if (s != btScalar(0.0))
84        {
85        d = radius / s; 
86                tmp[XX] = v[XX] * d;
87                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
88                tmp[ZZ] = v[ZZ] * d;
89                return tmp;
90        }
91    else
92        {
93            tmp[XX] = radius;
94                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
95                tmp[ZZ] = btScalar(0.0);
96                return tmp;
97    }
98
99
100}
101
102
103
104
105
106
107inline  btVector3 CylinderLocalSupportY(const btVector3& halfExtents,const btVector3& v) 
108{
109
110const int cylinderUpAxis = 1;
111const int XX = 0;
112const int YY = 1;
113const int ZZ = 2;
114
115
116        btScalar radius = halfExtents[XX];
117        btScalar halfHeight = halfExtents[cylinderUpAxis];
118
119
120    btVector3 tmp;
121        btScalar d ;
122
123    btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
124    if (s != btScalar(0.0))
125        {
126        d = radius / s; 
127                tmp[XX] = v[XX] * d;
128                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
129                tmp[ZZ] = v[ZZ] * d;
130                return tmp;
131        }
132    else
133        {
134            tmp[XX] = radius;
135                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
136                tmp[ZZ] = btScalar(0.0);
137                return tmp;
138    }
139
140}
141
142inline btVector3 CylinderLocalSupportZ(const btVector3& halfExtents,const btVector3& v) 
143{
144const int cylinderUpAxis = 2;
145const int XX = 0;
146const int YY = 2;
147const int ZZ = 1;
148
149        //mapping depends on how cylinder local orientation is
150        // extents of the cylinder is: X,Y is for radius, and Z for height
151
152
153        btScalar radius = halfExtents[XX];
154        btScalar halfHeight = halfExtents[cylinderUpAxis];
155
156
157    btVector3 tmp;
158        btScalar d ;
159
160    btScalar s = btSqrt(v[XX] * v[XX] + v[ZZ] * v[ZZ]);
161    if (s != btScalar(0.0))
162        {
163        d = radius / s; 
164                tmp[XX] = v[XX] * d;
165                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
166                tmp[ZZ] = v[ZZ] * d;
167                return tmp;
168        }
169    else
170        {
171            tmp[XX] = radius;
172                tmp[YY] = v[YY] < 0.0 ? -halfHeight : halfHeight;
173                tmp[ZZ] = btScalar(0.0);
174                return tmp;
175    }
176
177
178}
179
180btVector3       btCylinderShapeX::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
181{
182        return CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vec);
183}
184
185
186btVector3       btCylinderShapeZ::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
187{
188        return CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vec);
189}
190btVector3       btCylinderShape::localGetSupportingVertexWithoutMargin(const btVector3& vec)const
191{
192        return CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vec);
193}
194
195void    btCylinderShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
196{
197        for (int i=0;i<numVectors;i++)
198        {
199                supportVerticesOut[i] = CylinderLocalSupportY(getHalfExtentsWithoutMargin(),vectors[i]);
200        }
201}
202
203void    btCylinderShapeZ::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
204{
205        for (int i=0;i<numVectors;i++)
206        {
207                supportVerticesOut[i] = CylinderLocalSupportZ(getHalfExtentsWithoutMargin(),vectors[i]);
208        }
209}
210
211
212
213
214void    btCylinderShapeX::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
215{
216        for (int i=0;i<numVectors;i++)
217        {
218                supportVerticesOut[i] = CylinderLocalSupportX(getHalfExtentsWithoutMargin(),vectors[i]);
219        }
220}
221
222
Note: See TracBrowser for help on using the repository browser.