1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
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 | #ifndef OBB_TRIANGLE_MINKOWSKI_H |
---|
17 | #define OBB_TRIANGLE_MINKOWSKI_H |
---|
18 | |
---|
19 | #include "btConvexShape.h" |
---|
20 | #include "btBoxShape.h" |
---|
21 | |
---|
22 | class btTriangleShape : public btPolyhedralConvexShape |
---|
23 | { |
---|
24 | |
---|
25 | |
---|
26 | public: |
---|
27 | |
---|
28 | btVector3 m_vertices1[3]; |
---|
29 | |
---|
30 | virtual int getNumVertices() const |
---|
31 | { |
---|
32 | return 3; |
---|
33 | } |
---|
34 | |
---|
35 | const btVector3& getVertexPtr(int index) const |
---|
36 | { |
---|
37 | return m_vertices1[index]; |
---|
38 | } |
---|
39 | virtual void getVertex(int index,btVector3& vert) const |
---|
40 | { |
---|
41 | vert = m_vertices1[index]; |
---|
42 | } |
---|
43 | |
---|
44 | virtual int getNumEdges() const |
---|
45 | { |
---|
46 | return 3; |
---|
47 | } |
---|
48 | |
---|
49 | virtual void getEdge(int i,btVector3& pa,btVector3& pb) const |
---|
50 | { |
---|
51 | getVertex(i,pa); |
---|
52 | getVertex((i+1)%3,pb); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | virtual void getAabb(const btTransform& t,btVector3& aabbMin,btVector3& aabbMax)const |
---|
57 | { |
---|
58 | // btAssert(0); |
---|
59 | getAabbSlow(t,aabbMin,aabbMax); |
---|
60 | } |
---|
61 | |
---|
62 | btVector3 localGetSupportingVertexWithoutMargin(const btVector3& dir)const |
---|
63 | { |
---|
64 | btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2])); |
---|
65 | return m_vertices1[dots.maxAxis()]; |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | virtual void batchedUnitVectorGetSupportingVertexWithoutMargin(const btVector3* vectors,btVector3* supportVerticesOut,int numVectors) const |
---|
70 | { |
---|
71 | for (int i=0;i<numVectors;i++) |
---|
72 | { |
---|
73 | const btVector3& dir = vectors[i]; |
---|
74 | btVector3 dots(dir.dot(m_vertices1[0]), dir.dot(m_vertices1[1]), dir.dot(m_vertices1[2])); |
---|
75 | supportVerticesOut[i] = m_vertices1[dots.maxAxis()]; |
---|
76 | } |
---|
77 | |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | btTriangleShape(const btVector3& p0,const btVector3& p1,const btVector3& p2) : btPolyhedralConvexShape () |
---|
83 | { |
---|
84 | m_shapeType = TRIANGLE_SHAPE_PROXYTYPE; |
---|
85 | m_vertices1[0] = p0; |
---|
86 | m_vertices1[1] = p1; |
---|
87 | m_vertices1[2] = p2; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | virtual void getPlane(btVector3& planeNormal,btVector3& planeSupport,int i) const |
---|
92 | { |
---|
93 | getPlaneEquation(i,planeNormal,planeSupport); |
---|
94 | } |
---|
95 | |
---|
96 | virtual int getNumPlanes() const |
---|
97 | { |
---|
98 | return 1; |
---|
99 | } |
---|
100 | |
---|
101 | void calcNormal(btVector3& normal) const |
---|
102 | { |
---|
103 | normal = (m_vertices1[1]-m_vertices1[0]).cross(m_vertices1[2]-m_vertices1[0]); |
---|
104 | normal.normalize(); |
---|
105 | } |
---|
106 | |
---|
107 | virtual void getPlaneEquation(int i, btVector3& planeNormal,btVector3& planeSupport) const |
---|
108 | { |
---|
109 | (void)i; |
---|
110 | calcNormal(planeNormal); |
---|
111 | planeSupport = m_vertices1[0]; |
---|
112 | } |
---|
113 | |
---|
114 | virtual void calculateLocalInertia(btScalar mass,btVector3& inertia) const |
---|
115 | { |
---|
116 | (void)mass; |
---|
117 | btAssert(0); |
---|
118 | inertia.setValue(btScalar(0.),btScalar(0.),btScalar(0.)); |
---|
119 | } |
---|
120 | |
---|
121 | virtual bool isInside(const btVector3& pt,btScalar tolerance) const |
---|
122 | { |
---|
123 | btVector3 normal; |
---|
124 | calcNormal(normal); |
---|
125 | //distance to plane |
---|
126 | btScalar dist = pt.dot(normal); |
---|
127 | btScalar planeconst = m_vertices1[0].dot(normal); |
---|
128 | dist -= planeconst; |
---|
129 | if (dist >= -tolerance && dist <= tolerance) |
---|
130 | { |
---|
131 | //inside check on edge-planes |
---|
132 | int i; |
---|
133 | for (i=0;i<3;i++) |
---|
134 | { |
---|
135 | btVector3 pa,pb; |
---|
136 | getEdge(i,pa,pb); |
---|
137 | btVector3 edge = pb-pa; |
---|
138 | btVector3 edgeNormal = edge.cross(normal); |
---|
139 | edgeNormal.normalize(); |
---|
140 | btScalar dist = pt.dot( edgeNormal); |
---|
141 | btScalar edgeConst = pa.dot(edgeNormal); |
---|
142 | dist -= edgeConst; |
---|
143 | if (dist < -tolerance) |
---|
144 | return false; |
---|
145 | } |
---|
146 | |
---|
147 | return true; |
---|
148 | } |
---|
149 | |
---|
150 | return false; |
---|
151 | } |
---|
152 | //debugging |
---|
153 | virtual const char* getName()const |
---|
154 | { |
---|
155 | return "Triangle"; |
---|
156 | } |
---|
157 | |
---|
158 | virtual int getNumPreferredPenetrationDirections() const |
---|
159 | { |
---|
160 | return 2; |
---|
161 | } |
---|
162 | |
---|
163 | virtual void getPreferredPenetrationDirection(int index, btVector3& penetrationVector) const |
---|
164 | { |
---|
165 | calcNormal(penetrationVector); |
---|
166 | if (index) |
---|
167 | penetrationVector *= btScalar(-1.); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | }; |
---|
172 | |
---|
173 | #endif //OBB_TRIANGLE_MINKOWSKI_H |
---|
174 | |
---|