== SoC Bullet - Bullet Upgrade to 2.76 ==
[blender.git] / extern / bullet2 / BulletCollision / CollisionShapes / btConeShape.cpp
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 #include "btConeShape.h"
17
18
19
20 btConeShape::btConeShape (btScalar radius,btScalar height): btConvexInternalShape (),
21 m_radius (radius),
22 m_height(height)
23 {
24         m_shapeType = CONE_SHAPE_PROXYTYPE;
25         setConeUpIndex(1);
26         btVector3 halfExtents;
27         m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height));
28 }
29
30 btConeShapeZ::btConeShapeZ (btScalar radius,btScalar height):
31 btConeShape(radius,height)
32 {
33         setConeUpIndex(2);
34 }
35
36 btConeShapeX::btConeShapeX (btScalar radius,btScalar height):
37 btConeShape(radius,height)
38 {
39         setConeUpIndex(0);
40 }
41
42 ///choose upAxis index
43 void    btConeShape::setConeUpIndex(int upIndex)
44 {
45         switch (upIndex)
46         {
47         case 0:
48                         m_coneIndices[0] = 1;
49                         m_coneIndices[1] = 0;
50                         m_coneIndices[2] = 2;
51                 break;
52         case 1:
53                         m_coneIndices[0] = 0;
54                         m_coneIndices[1] = 1;
55                         m_coneIndices[2] = 2;
56                 break;
57         case 2:
58                         m_coneIndices[0] = 0;
59                         m_coneIndices[1] = 2;
60                         m_coneIndices[2] = 1;
61                 break;
62         default:
63                 btAssert(0);
64         };
65 }
66
67 btVector3 btConeShape::coneLocalSupport(const btVector3& v) const
68 {
69         
70         btScalar halfHeight = m_height * btScalar(0.5);
71
72  if (v[m_coneIndices[1]] > v.length() * m_sinAngle)
73  {
74         btVector3 tmp;
75
76         tmp[m_coneIndices[0]] = btScalar(0.);
77         tmp[m_coneIndices[1]] = halfHeight;
78         tmp[m_coneIndices[2]] = btScalar(0.);
79         return tmp;
80  }
81   else {
82     btScalar s = btSqrt(v[m_coneIndices[0]] * v[m_coneIndices[0]] + v[m_coneIndices[2]] * v[m_coneIndices[2]]);
83     if (s > SIMD_EPSILON) {
84       btScalar d = m_radius / s;
85           btVector3 tmp;
86           tmp[m_coneIndices[0]] = v[m_coneIndices[0]] * d;
87           tmp[m_coneIndices[1]] = -halfHeight;
88           tmp[m_coneIndices[2]] = v[m_coneIndices[2]] * d;
89           return tmp;
90     }
91     else  {
92                 btVector3 tmp;
93                 tmp[m_coneIndices[0]] = btScalar(0.);
94                 tmp[m_coneIndices[1]] = -halfHeight;
95                 tmp[m_coneIndices[2]] = btScalar(0.);
96                 return tmp;
97         }
98   }
99
100 }
101
102 btVector3       btConeShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
103 {
104                 return coneLocalSupport(vec);
105 }
106
107 void    btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
108 {
109         for (int i=0;i<numVectors;i++)
110         {
111                 const btVector3& vec = vectors[i];
112                 supportVerticesOut[i] = coneLocalSupport(vec);
113         }
114 }
115
116
117 btVector3       btConeShape::localGetSupportingVertex(const btVector3& vec)  const
118 {
119         btVector3 supVertex = coneLocalSupport(vec);
120         if ( getMargin()!=btScalar(0.) )
121         {
122                 btVector3 vecnorm = vec;
123                 if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
124                 {
125                         vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
126                 } 
127                 vecnorm.normalize();
128                 supVertex+= getMargin() * vecnorm;
129         }
130         return supVertex;
131 }
132
133