Bullet Collision Detection & Physics Library
btConeShape.cpp
Go to the documentation of this file.
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 
21 m_radius (radius),
22 m_height(height)
23 {
25  setConeUpIndex(1);
26  btVector3 halfExtents;
28 }
29 
31 btConeShape(radius,height)
32 {
33  setConeUpIndex(2);
34 }
35 
37 btConeShape(radius,height)
38 {
39  setConeUpIndex(0);
40 }
41 
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 
67  m_implicitShapeDimensions[m_coneIndices[1]] = m_height;
68  m_implicitShapeDimensions[m_coneIndices[2]] = m_radius;
69 }
70 
72 {
73 
74  btScalar halfHeight = m_height * btScalar(0.5);
75 
76  if (v[m_coneIndices[1]] > v.length() * m_sinAngle)
77  {
78  btVector3 tmp;
79 
80  tmp[m_coneIndices[0]] = btScalar(0.);
81  tmp[m_coneIndices[1]] = halfHeight;
82  tmp[m_coneIndices[2]] = btScalar(0.);
83  return tmp;
84  }
85  else {
86  btScalar s = btSqrt(v[m_coneIndices[0]] * v[m_coneIndices[0]] + v[m_coneIndices[2]] * v[m_coneIndices[2]]);
87  if (s > SIMD_EPSILON) {
88  btScalar d = m_radius / s;
89  btVector3 tmp;
90  tmp[m_coneIndices[0]] = v[m_coneIndices[0]] * d;
91  tmp[m_coneIndices[1]] = -halfHeight;
92  tmp[m_coneIndices[2]] = v[m_coneIndices[2]] * d;
93  return tmp;
94  }
95  else {
96  btVector3 tmp;
97  tmp[m_coneIndices[0]] = btScalar(0.);
98  tmp[m_coneIndices[1]] = -halfHeight;
99  tmp[m_coneIndices[2]] = btScalar(0.);
100  return tmp;
101  }
102  }
103 
104 }
105 
107 {
108  return coneLocalSupport(vec);
109 }
110 
111 void btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const
112 {
113  for (int i=0;i<numVectors;i++)
114  {
115  const btVector3& vec = vectors[i];
116  supportVerticesOut[i] = coneLocalSupport(vec);
117  }
118 }
119 
120 
122 {
123  btVector3 supVertex = coneLocalSupport(vec);
124  if ( getMargin()!=btScalar(0.) )
125  {
126  btVector3 vecnorm = vec;
127  if (vecnorm .length2() < (SIMD_EPSILON*SIMD_EPSILON))
128  {
129  vecnorm.setValue(btScalar(-1.),btScalar(-1.),btScalar(-1.));
130  }
131  vecnorm.normalize();
132  supVertex+= getMargin() * vecnorm;
133  }
134  return supVertex;
135 }
136 
137 
139 {
140  int axis = m_coneIndices[1];
141  int r1 = m_coneIndices[0];
142  int r2 = m_coneIndices[2];
143  m_height *= scaling[axis] / m_localScaling[axis];
144  m_radius *= (scaling[r1] / m_localScaling[r1] + scaling[r2] / m_localScaling[r2]) / 2;
147 }
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
#define SIMD_EPSILON
Definition: btScalar.h:521
btConeShape(btScalar radius, btScalar height)
Definition: btConeShape.cpp:20
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition: btVector3.h:652
The btConvexInternalShape is an internal base class, shared by most convex shape implementations.
int m_coneIndices[3]
Definition: btConeShape.h:30
btScalar btSqrt(btScalar y)
Definition: btScalar.h:444
#define btAssert(x)
Definition: btScalar.h:131
btScalar m_height
Definition: btConeShape.h:29
void setConeUpIndex(int upIndex)
choose upAxis index
Definition: btConeShape.cpp:43
virtual btScalar getMargin() const
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition: btVector3.h:309
btVector3 coneLocalSupport(const btVector3 &v) const
Definition: btConeShape.cpp:71
btScalar m_radius
Definition: btConeShape.h:28
The btConeShape implements a cone shape primitive, centered around the origin and aligned with the Y ...
Definition: btConeShape.h:23
btScalar length() const
Return the length of the vector.
Definition: btVector3.h:263
btConeShapeX(btScalar radius, btScalar height)
Definition: btConeShape.cpp:36
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:83
virtual void setLocalScaling(const btVector3 &scaling)
virtual void setLocalScaling(const btVector3 &scaling)
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
btConeShapeZ(btScalar radius, btScalar height)
Definition: btConeShape.cpp:30
btScalar m_sinAngle
Definition: btConeShape.h:27
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292