diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CollisionShape.cs | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CollisionShape.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CollisionShape.cs new file mode 100644 index 0000000..a9ce3be --- /dev/null +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionShapes/CollisionShape.cs | |||
@@ -0,0 +1,148 @@ | |||
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 | /// CollisionShape provides generic interface for collidable objects | ||
31 | /// </summary> | ||
32 | public abstract class CollisionShape | ||
33 | { | ||
34 | //debugging support | ||
35 | private string _tempDebug; | ||
36 | |||
37 | public abstract string Name { get; } | ||
38 | public string ExtraDebugInfo { get { return _tempDebug; } set { _tempDebug = value; } } | ||
39 | |||
40 | public bool IsPolyhedral | ||
41 | { | ||
42 | get | ||
43 | { | ||
44 | return BroadphaseProxy.IsPolyhedral(ShapeType); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | public bool IsConvex | ||
49 | { | ||
50 | get | ||
51 | { | ||
52 | return BroadphaseProxy.IsConvex(ShapeType); | ||
53 | } | ||
54 | } | ||
55 | public bool IsConcave | ||
56 | { | ||
57 | get | ||
58 | { | ||
59 | return BroadphaseProxy.IsConcave(ShapeType); | ||
60 | } | ||
61 | } | ||
62 | public bool IsCompound | ||
63 | { | ||
64 | get | ||
65 | { | ||
66 | return BroadphaseProxy.IsCompound(ShapeType); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | //isInfinite is used to catch simulation error (aabb check) | ||
71 | public bool IsInfinite | ||
72 | { | ||
73 | get | ||
74 | { | ||
75 | return BroadphaseProxy.IsInfinite(ShapeType); | ||
76 | } | ||
77 | } | ||
78 | |||
79 | public abstract float Margin { get; set; } | ||
80 | public abstract Vector3 LocalScaling { get; set; } | ||
81 | public abstract BroadphaseNativeTypes ShapeType { get; } | ||
82 | |||
83 | |||
84 | public virtual void GetBoundingSphere(out Vector3 center, out float radius) | ||
85 | { | ||
86 | Matrix tr = Matrix.Identity; | ||
87 | Vector3 aabbMin, aabbMax; | ||
88 | |||
89 | GetAabb(tr, out aabbMin, out aabbMax); | ||
90 | |||
91 | radius = (aabbMax - aabbMin).Length() * 0.5f; | ||
92 | center = (aabbMin + aabbMax) * 0.5f; | ||
93 | } | ||
94 | |||
95 | public virtual float GetAngularMotionDisc() | ||
96 | { | ||
97 | Vector3 center; | ||
98 | float disc; | ||
99 | GetBoundingSphere(out center, out disc); | ||
100 | disc += center.Length(); | ||
101 | return disc; | ||
102 | } | ||
103 | |||
104 | //calculateTemporalAabb calculates the enclosing aabb for the moving object over interval [0..timeStep) | ||
105 | //result is conservative | ||
106 | public void CalculateTemporalAabb(Matrix currentTransform, Vector3 linearVelocity, Vector3 angularVelocity, float timeStep, out Vector3 temporalAabbMin, out Vector3 temporalAabbMax) | ||
107 | { | ||
108 | //start with static aabb | ||
109 | GetAabb(currentTransform, out temporalAabbMin, out temporalAabbMax); | ||
110 | |||
111 | float temporalAabbMaxx = temporalAabbMax.X; | ||
112 | float temporalAabbMaxy = temporalAabbMax.Y; | ||
113 | float temporalAabbMaxz = temporalAabbMax.Z; | ||
114 | float temporalAabbMinx = temporalAabbMin.X; | ||
115 | float temporalAabbMiny = temporalAabbMin.Y; | ||
116 | float temporalAabbMinz = temporalAabbMin.Z; | ||
117 | |||
118 | // add linear motion | ||
119 | Vector3 linMotion = linearVelocity * timeStep; | ||
120 | //todo: simd would have a vector max/min operation, instead of per-element access | ||
121 | if (linMotion.X > 0) | ||
122 | temporalAabbMaxx += linMotion.X; | ||
123 | else | ||
124 | temporalAabbMinx += linMotion.X; | ||
125 | if (linMotion.Y > 0) | ||
126 | temporalAabbMaxy += linMotion.Y; | ||
127 | else | ||
128 | temporalAabbMiny += linMotion.Y; | ||
129 | if (linMotion.Z > 0) | ||
130 | temporalAabbMaxz += linMotion.Z; | ||
131 | else | ||
132 | temporalAabbMinz += linMotion.Z; | ||
133 | |||
134 | //add conservative angular motion | ||
135 | float angularMotion = angularVelocity.Length() * GetAngularMotionDisc() * timeStep; | ||
136 | Vector3 angularMotion3d = new Vector3(angularMotion, angularMotion, angularMotion); | ||
137 | temporalAabbMin = new Vector3(temporalAabbMinx, temporalAabbMiny, temporalAabbMinz); | ||
138 | temporalAabbMax = new Vector3(temporalAabbMaxx, temporalAabbMaxy, temporalAabbMaxz); | ||
139 | |||
140 | temporalAabbMin -= angularMotion3d; | ||
141 | temporalAabbMax += angularMotion3d; | ||
142 | } | ||
143 | |||
144 | public abstract void GetAabb(Matrix transform, out Vector3 aabbMin, out Vector3 aabbMax); | ||
145 | |||
146 | public abstract void CalculateLocalInertia(float mass, out Vector3 inertia); | ||
147 | } | ||
148 | } | ||