[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / BulletCollision / CollisionShapes / btCapsuleShape.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 "btCapsuleShape.h"
17
18 #include "LinearMath/btQuaternion.h"
19
20 btCapsuleShape::btCapsuleShape(btScalar radius, btScalar height) : btConvexInternalShape()
21 {
22         m_collisionMargin = radius;
23         m_shapeType = CAPSULE_SHAPE_PROXYTYPE;
24         m_upAxis = 1;
25         m_implicitShapeDimensions.setValue(radius, 0.5f * height, radius);
26 }
27
28 btVector3 btCapsuleShape::localGetSupportingVertexWithoutMargin(const btVector3& vec0) const
29 {
30         btVector3 supVec(0, 0, 0);
31
32         btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
33
34         btVector3 vec = vec0;
35         btScalar lenSqr = vec.length2();
36         if (lenSqr < btScalar(0.0001))
37         {
38                 vec.setValue(1, 0, 0);
39         }
40         else
41         {
42                 btScalar rlen = btScalar(1.) / btSqrt(lenSqr);
43                 vec *= rlen;
44         }
45
46         btVector3 vtx;
47         btScalar newDot;
48
49         {
50                 btVector3 pos(0, 0, 0);
51                 pos[getUpAxis()] = getHalfHeight();
52
53                 vtx = pos;
54                 newDot = vec.dot(vtx);
55                 if (newDot > maxDot)
56                 {
57                         maxDot = newDot;
58                         supVec = vtx;
59                 }
60         }
61         {
62                 btVector3 pos(0, 0, 0);
63                 pos[getUpAxis()] = -getHalfHeight();
64
65                 vtx = pos;
66                 newDot = vec.dot(vtx);
67                 if (newDot > maxDot)
68                 {
69                         maxDot = newDot;
70                         supVec = vtx;
71                 }
72         }
73
74         return supVec;
75 }
76
77 void btCapsuleShape::batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors, btVector3* supportVerticesOut, int numVectors) const
78 {
79         for (int j = 0; j < numVectors; j++)
80         {
81                 btScalar maxDot(btScalar(-BT_LARGE_FLOAT));
82                 const btVector3& vec = vectors[j];
83
84                 btVector3 vtx;
85                 btScalar newDot;
86                 {
87                         btVector3 pos(0, 0, 0);
88                         pos[getUpAxis()] = getHalfHeight();
89                         vtx = pos;
90                         newDot = vec.dot(vtx);
91                         if (newDot > maxDot)
92                         {
93                                 maxDot = newDot;
94                                 supportVerticesOut[j] = vtx;
95                         }
96                 }
97                 {
98                         btVector3 pos(0, 0, 0);
99                         pos[getUpAxis()] = -getHalfHeight();
100                         vtx = pos;
101                         newDot = vec.dot(vtx);
102                         if (newDot > maxDot)
103                         {
104                                 maxDot = newDot;
105                                 supportVerticesOut[j] = vtx;
106                         }
107                 }
108         }
109 }
110
111 void btCapsuleShape::calculateLocalInertia(btScalar mass, btVector3& inertia) const
112 {
113         //as an approximation, take the inertia of the box that bounds the spheres
114
115         btTransform ident;
116         ident.setIdentity();
117
118         btScalar radius = getRadius();
119
120         btVector3 halfExtents(radius, radius, radius);
121         halfExtents[getUpAxis()] += getHalfHeight();
122
123         btScalar lx = btScalar(2.) * (halfExtents[0]);
124         btScalar ly = btScalar(2.) * (halfExtents[1]);
125         btScalar lz = btScalar(2.) * (halfExtents[2]);
126         const btScalar x2 = lx * lx;
127         const btScalar y2 = ly * ly;
128         const btScalar z2 = lz * lz;
129         const btScalar scaledmass = mass * btScalar(.08333333);
130
131         inertia[0] = scaledmass * (y2 + z2);
132         inertia[1] = scaledmass * (x2 + z2);
133         inertia[2] = scaledmass * (x2 + y2);
134 }
135
136 btCapsuleShapeX::btCapsuleShapeX(btScalar radius, btScalar height)
137 {
138         m_collisionMargin = radius;
139         m_upAxis = 0;
140         m_implicitShapeDimensions.setValue(0.5f * height, radius, radius);
141 }
142
143 btCapsuleShapeZ::btCapsuleShapeZ(btScalar radius, btScalar height)
144 {
145         m_collisionMargin = radius;
146         m_upAxis = 2;
147         m_implicitShapeDimensions.setValue(radius, radius, 0.5f * height);
148 }