aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs')
-rw-r--r--libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs187
1 files changed, 187 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs
new file mode 100644
index 0000000..59ffad0
--- /dev/null
+++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/TriangleShape.cs
@@ -0,0 +1,187 @@
1/*
2 Bullet for XNA Copyright (c) 2003-2007 Vsevolod Klementjev http://www.codeplex.com/xnadevru
3 Bullet original C++ version Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using MonoXnaCompactMaths;
26
27namespace XnaDevRu.BulletX
28{
29 public class TriangleShape : PolyhedralConvexShape
30 {
31 private Vector3[] _vertices = new Vector3[3];
32
33 public TriangleShape(Vector3 pointA, Vector3 pointB, Vector3 pointC)
34 {
35 _vertices[0] = pointA;
36 _vertices[1] = pointB;
37 _vertices[2] = pointC;
38 }
39
40 public override int PreferredPenetrationDirectionsCount
41 {
42 get
43 {
44 return 2;
45 }
46 }
47
48 public Vector3[] Vertices
49 {
50 get
51 {
52 return _vertices;
53 }
54 }
55
56 public override int VertexCount
57 {
58 get
59 {
60 return 3;
61 }
62 }
63
64 public override int EdgeCount
65 {
66 get
67 {
68 return 3;
69 }
70 }
71
72 public override int PlaneCount
73 {
74 get
75 {
76 return 1;
77 }
78 }
79
80 public override BroadphaseNativeTypes ShapeType
81 {
82 get
83 {
84 return BroadphaseNativeTypes.Triangle;
85 }
86 }
87
88 public override string Name
89 {
90 get
91 {
92 return "Triangle";
93 }
94 }
95
96 public override void GetPreferredPenetrationDirection(int index, out Vector3 penetrationVector)
97 {
98 CalculateNormal(out penetrationVector);
99 if (index != 0)
100 penetrationVector *= -1f;
101 }
102
103 public virtual void GetPlaneEquation(int i, out Vector3 planeNormal, out Vector3 planeSupport)
104 {
105 CalculateNormal(out planeNormal);
106 planeSupport = _vertices[0];
107 }
108
109 public void CalculateNormal(out Vector3 normal)
110 {
111 normal = Vector3.Normalize(Vector3.Cross(_vertices[1] - _vertices[0], _vertices[2] - _vertices[0]));
112 }
113
114 public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec)
115 {
116 Vector3 dots = new Vector3(Vector3.Dot(vec, _vertices[0]), Vector3.Dot(vec, _vertices[1]), Vector3.Dot(vec, _vertices[2]));
117 return _vertices[MathHelper.MaxAxis(dots)];
118 }
119
120 public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut)
121 {
122 for (int i = 0; i < vectors.Length; i++)
123 {
124 Vector3 dir = vectors[i];
125 Vector3 dots = new Vector3(Vector3.Dot(dir, _vertices[0]), Vector3.Dot(dir, _vertices[1]), Vector3.Dot(dir, _vertices[2]));
126 supportVerticesOut[i] = _vertices[MathHelper.MaxAxis(dots)];
127 }
128 }
129
130 public override void CalculateLocalInertia(float mass, out Vector3 inertia)
131 {
132 inertia = new Vector3();
133 BulletDebug.Assert(false);
134 }
135
136 public override void GetEdge(int i, out Vector3 pa, out Vector3 pb)
137 {
138 GetVertex(i, out pa);
139 GetVertex((i + 1) % 3, out pb);
140 }
141
142 public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax)
143 {
144 GetAabbSlow(t, out aabbMin, out aabbMax);
145 }
146
147 public override void GetVertex(int i, out Vector3 vtx)
148 {
149 vtx = _vertices[i];
150 }
151
152 public override void GetPlane(out Vector3 planeNormal, out Vector3 planeSupport, int i)
153 {
154 GetPlaneEquation(i, out planeNormal, out planeSupport);
155 }
156
157 public override bool IsInside(Vector3 pt, float tolerance)
158 {
159 Vector3 normal;
160 CalculateNormal(out normal);
161 //distance to plane
162 float dist = Vector3.Dot(pt, normal);
163 float planeconst = Vector3.Dot(_vertices[0], normal);
164 dist -= planeconst;
165 if (dist >= -tolerance && dist <= tolerance)
166 {
167 //inside check on edge-planes
168 int i;
169 for (i = 0; i < 3; i++)
170 {
171 Vector3 pa, pb;
172 GetEdge(i, out pa, out pb);
173 Vector3 edge = pb - pa;
174 Vector3 edgeNormal = Vector3.Cross(edge, normal);
175 edgeNormal = Vector3.Normalize(edgeNormal);
176 float distance = Vector3.Dot(pt, edgeNormal);
177 float edgeConst = Vector3.Dot(pa, edgeNormal);
178 distance -= edgeConst;
179 if (distance < -tolerance)
180 return false;
181 }
182 return true;
183 }
184 return false;
185 }
186 }
187}