diff options
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/SphereShape.cs')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/SphereShape.cs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/SphereShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/SphereShape.cs new file mode 100644 index 0000000..c660a6f --- /dev/null +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/SphereShape.cs | |||
@@ -0,0 +1,116 @@ | |||
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 | |||
22 | using System; | ||
23 | using System.Collections.Generic; | ||
24 | using System.Text; | ||
25 | using MonoXnaCompactMaths; | ||
26 | |||
27 | namespace XnaDevRu.BulletX | ||
28 | { | ||
29 | /// <summary> | ||
30 | /// btSphereShape implements an implicit (getSupportingVertex) Sphere | ||
31 | /// </summary> | ||
32 | public class SphereShape : ConvexShape | ||
33 | { | ||
34 | public SphereShape(float radius) | ||
35 | : base() | ||
36 | { | ||
37 | Vector3 temp = ImplicitShapeDimensions; | ||
38 | temp.X = radius; | ||
39 | ImplicitShapeDimensions = temp; | ||
40 | } | ||
41 | |||
42 | public float Radius { get { return ImplicitShapeDimensions.X; } } | ||
43 | |||
44 | public override BroadphaseNativeTypes ShapeType | ||
45 | { | ||
46 | get | ||
47 | { | ||
48 | return BroadphaseNativeTypes.Sphere; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | public override string Name | ||
53 | { | ||
54 | get | ||
55 | { | ||
56 | return "Sphere"; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | public override Vector3 LocalGetSupportingVertexWithoutMargin(Vector3 vec) | ||
61 | { | ||
62 | return new Vector3(); | ||
63 | } | ||
64 | |||
65 | /// <summary> | ||
66 | /// to improve gjk behaviour, use radius+margin as the full margin, so never get into the penetration case | ||
67 | /// this means, non-uniform scaling is not supported anymore | ||
68 | /// </summary> | ||
69 | public override float Margin | ||
70 | { | ||
71 | get | ||
72 | { | ||
73 | return LocalScaling.X * Radius + base.Margin; | ||
74 | } | ||
75 | set | ||
76 | { | ||
77 | base.Margin = value; | ||
78 | } | ||
79 | } | ||
80 | |||
81 | public override void BatchedUnitVectorGetSupportingVertexWithoutMargin(Vector3[] vectors, Vector3[] supportVerticesOut) | ||
82 | { | ||
83 | if (supportVerticesOut != null) | ||
84 | for (int i = 0; i < supportVerticesOut.Length; i++) | ||
85 | supportVerticesOut[i] = new Vector3(); | ||
86 | } | ||
87 | |||
88 | public override void CalculateLocalInertia(float mass, out Vector3 inertia) | ||
89 | { | ||
90 | float elem = 0.4f * mass * Margin * Margin; | ||
91 | inertia = new Vector3(elem, elem, elem); | ||
92 | } | ||
93 | |||
94 | public override Vector3 LocalGetSupportingVertex(Vector3 vec) | ||
95 | { | ||
96 | Vector3 supVertex = LocalGetSupportingVertexWithoutMargin(vec); | ||
97 | |||
98 | Vector3 vecnorm = vec; | ||
99 | if (vecnorm.LengthSquared() < (MathHelper.Epsilon * MathHelper.Epsilon)) | ||
100 | { | ||
101 | vecnorm = new Vector3(-1f, -1f, -1f); | ||
102 | } | ||
103 | vecnorm.Normalize(); | ||
104 | supVertex += Margin * vecnorm; | ||
105 | return supVertex; | ||
106 | } | ||
107 | |||
108 | public override void GetAabb(Matrix t, out Vector3 aabbMin, out Vector3 aabbMax) | ||
109 | { | ||
110 | Vector3 center = t.Translation; | ||
111 | Vector3 extent = new Vector3(Margin, Margin, Margin); | ||
112 | aabbMin = center - extent; | ||
113 | aabbMax = center + extent; | ||
114 | } | ||
115 | } | ||
116 | } | ||