Bullet Collision Detection & Physics Library: btConeShape.cpp Source File

Bullet Collision Detection & Physics Library

btConeShape.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
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*/
15
16#include "btConeShape.h"
17
18btConeShape::btConeShape(btScalar radius, btScalar height) : btConvexInternalShape(),
19 m_radius(radius),
20 m_height(height)
21{
22 m_shapeType = CONE_SHAPE_PROXYTYPE;
23 setConeUpIndex(1);
24 btVector3 halfExtents;
25 m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height));
26}
27
28btConeShapeZ::btConeShapeZ(btScalar radius, btScalar height) : btConeShape(radius, height)
29{
30 setConeUpIndex(2);
31}
32
33btConeShapeX::btConeShapeX(btScalar radius, btScalar height) : btConeShape(radius, height)
34{
35 setConeUpIndex(0);
36}
37
39 void btConeShape::setConeUpIndex(int upIndex)
40{
41 switch (upIndex)
42 {
43 case 0:
44 m_coneIndices[0] = 1;
45 m_coneIndices[1] = 0;
46 m_coneIndices[2] = 2;
47 break;
48 case 1:
49 m_coneIndices[0] = 0;
50 m_coneIndices[1] = 1;
51 m_coneIndices[2] = 2;
52 break;
53 case 2:
54 m_coneIndices[0] = 0;
55 m_coneIndices[1] = 2;
56 m_coneIndices[2] = 1;
57 break;
58 default:
59 btAssert(0);
60 };
61
62 m_implicitShapeDimensions[m_coneIndices[0]] = m_radius;
63 m_implicitShapeDimensions[m_coneIndices[1]] = m_height;
64 m_implicitShapeDimensions[m_coneIndices[2]] = m_radius;
65}
66
67 btVector3 btConeShape::coneLocalSupport(const btVector3& v) const
68{
69 btScalar halfHeight = m_height * btScalar(0.5);
70
71 if (v[m_coneIndices[1]] > v.length() * m_sinAngle)
72 {
73 btVector3 tmp;
74
75 tmp[m_coneIndices[0]] = btScalar(0.);
76 tmp[m_coneIndices[1]] = halfHeight;
77 tmp[m_coneIndices[2]] = btScalar(0.);
78 return tmp;
79 }
80 else
81 {
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 {
85 btScalar d = m_radius / s;
86 btVector3 tmp;
87 tmp[m_coneIndices[0]] = v[m_coneIndices[0]] * d;
88 tmp[m_coneIndices[1]] = -halfHeight;
89 tmp[m_coneIndices[2]] = v[m_coneIndices[2]] * d;
90 return tmp;
91 }
92 else
93 {
94 btVector3 tmp;
95 tmp[m_coneIndices[0]] = btScalar(0.);
96 tmp[m_coneIndices[1]] = -halfHeight;
97 tmp[m_coneIndices[2]] = btScalar(0.);
98 return tmp;
99 }
100 }
101}
102
103 btVector3 btConeShape::localGetSupportingVertexWithoutMargin(const btVector3& vec) const
104{
105 return coneLocalSupport(vec);
106}
107
108 void btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
109{
110 for (int i = 0; i < numVectors; i++)
111 {
112 const btVector3& vec = vectors[i];
113 supportVerticesOut[i] = coneLocalSupport(vec);
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 void btConeShape::setLocalScaling(const btVector3& scaling)
134{
135 int axis = m_coneIndices[1];
136 int r1 = m_coneIndices[0];
137 int r2 = m_coneIndices[2];
138 m_height *= scaling[axis] / m_localScaling[axis];
139 m_radius *= (scaling[r1] / m_localScaling[r1] + scaling[r2] / m_localScaling[r2]) / 2;
140 m_sinAngle = (m_radius / btSqrt(m_radius * m_radius + m_height * m_height));
141 btConvexInternalShape::setLocalScaling(scaling);
142}
CONE_SHAPE_PROXYTYPE
@ CONE_SHAPE_PROXYTYPE
Definition btBroadphaseProxy.h:42
btConeShape.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition btScalar.h:314
btSqrt
btScalar btSqrt(btScalar y)
Definition btScalar.h:466
SIMD_EPSILON
#define SIMD_EPSILON
Definition btScalar.h:543
btAssert
#define btAssert(x)
Definition btScalar.h:153
btCollisionShape::m_shapeType
int m_shapeType
Definition btCollisionShape.h:30
btConeShapeX::btConeShapeX
btConeShapeX(btScalar radius, btScalar height)
Definition btConeShape.cpp:33
btConeShapeZ::btConeShapeZ
btConeShapeZ(btScalar radius, btScalar height)
Definition btConeShape.cpp:28
btConeShape::localGetSupportingVertexWithoutMargin
virtual btVector3 localGetSupportingVertexWithoutMargin(const btVector3 &vec) const
Definition btConeShape.cpp:103
btConeShape::btConeShape
btConeShape(btScalar radius, btScalar height)
Definition btConeShape.cpp:18
btConeShape::m_height
btScalar m_height
Definition btConeShape.h:29
btConeShape::m_radius
btScalar m_radius
Definition btConeShape.h:28
btConeShape::m_sinAngle
btScalar m_sinAngle
Definition btConeShape.h:27
btConeShape::batchedUnitVectorGetSupportingVertexWithoutMargin
virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3 *vectors, btVector3 *supportVerticesOut, int numVectors) const
Definition btConeShape.cpp:108
btConeShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition btConeShape.cpp:133
btConeShape::m_coneIndices
int m_coneIndices[3]
Definition btConeShape.h:30
btConeShape::coneLocalSupport
btVector3 coneLocalSupport(const btVector3 &v) const
Definition btConeShape.cpp:67
btConeShape::setConeUpIndex
void setConeUpIndex(int upIndex)
choose upAxis index
Definition btConeShape.cpp:39
btConeShape::localGetSupportingVertex
virtual btVector3 localGetSupportingVertex(const btVector3 &vec) const
Definition btConeShape.cpp:117
btConvexInternalShape::m_localScaling
btVector3 m_localScaling
Definition btConvexInternalShape.h:33
btConvexInternalShape::m_implicitShapeDimensions
btVector3 m_implicitShapeDimensions
Definition btConvexInternalShape.h:35
btConvexInternalShape::btConvexInternalShape
btConvexInternalShape()
Definition btConvexInternalShape.cpp:18
btConvexInternalShape::setLocalScaling
virtual void setLocalScaling(const btVector3 &scaling)
Definition btConvexInternalShape.cpp:24
btConvexInternalShape::getMargin
virtual btScalar getMargin() const
Definition btConvexInternalShape.h:106
btVector3
btVector3 can be used to represent 3D points and vectors.
Definition btVector3.h:82
btVector3::length
btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
btVector3::setValue
void setValue(const btScalar &_x, const btScalar &_y, const btScalar &_z)
Definition btVector3.h:640
btVector3::length2
btScalar length2() const
Return the length of the vector squared.
Definition btVector3.h:251
btVector3::normalize
btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303