aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs')
-rw-r--r--libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs136
1 files changed, 136 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs
new file mode 100644
index 0000000..9a86eaf
--- /dev/null
+++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CylinderShape.cs
@@ -0,0 +1,136 @@
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 /// <summary>
30 /// implements cylinder shape interface
31 /// </summary>
32 public class CylinderShape : BoxShape
33 {
34 public CylinderShape(Vector3 halfExtents)
35 : base(halfExtents)
36 {
37 }
38
39 public override BroadphaseNativeTypes ShapeType
40 {
41 get
42 {
43 return BroadphaseNativeTypes.Cylinder;
44 }
45 }
46
47 public virtual int UpAxis
48 {
49 get
50 {
51 return 1;
52 }
53 }
54
55 public virtual float Radius
56 {
57 get
58 {
59 return HalfExtents.Z;
60 }
61 }
62
63 //debugging
64 public override string Name
65 {
66 get
67 {
68 return "CylinderY";
69 }
70 }
71
72 //getAabb's default implementation is brute force, expected derived classes to implement a fast dedicated version
73 public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax)
74 {
75 GetAabbSlow(t, out aabbMin, out aabbMax);
76 }
77
78 public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec)
79 {
80 return CylinderLocalSupportY(HalfExtents, vec);
81 }
82
83 public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut)
84 {
85 for (int i = 0; i < vectors.Length; i++)
86 {
87 supportVerticesOut[i] = CylinderLocalSupportY(HalfExtents, vectors[i]);
88 }
89 }
90
91 public override Vector3 LocalGetSupportingVertex(Vector3 vec)
92 {
93
94 Vector3 supVertex;
95 supVertex = LocalGetSupportingVertexWithoutMargin(vec);
96
97 if (Margin != 0)
98 {
99 Vector3 vecnorm = vec;
100 if (vecnorm.LengthSquared() < (MathHelper.Epsilon * MathHelper.Epsilon))
101 {
102 vecnorm=new Vector3(-1, -1, -1);
103 }
104 vecnorm = Vector3.Normalize(vecnorm);
105 supVertex += Margin * vecnorm;
106 }
107 return supVertex;
108 }
109
110 private Vector3 CylinderLocalSupportY(Vector3 halfExtents, Vector3 v)
111 {
112 float radius = halfExtents.X;
113 float halfHeight = halfExtents.Y;
114
115 Vector3 tmp = new Vector3();
116 float d;
117
118 float s = (float)Math.Sqrt(v.X * v.X + v.Z * v.Z);
119 if (s != 0)
120 {
121 d = radius / s;
122 tmp.X = v.X * d;
123 tmp.Y = v.Y < 0 ? -halfHeight : halfHeight;
124 tmp.Z = v.Z * d;
125 return tmp;
126 }
127 else
128 {
129 tmp.X = radius;
130 tmp.Y = v.Y < 0 ? -halfHeight : halfHeight;
131 tmp.Z = 0;
132 return tmp;
133 }
134 }
135 }
136}