diff options
Diffstat (limited to 'libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/SphereSphereCollisionAlgorithm.cs')
-rw-r--r-- | libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/SphereSphereCollisionAlgorithm.cs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/SphereSphereCollisionAlgorithm.cs b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/SphereSphereCollisionAlgorithm.cs new file mode 100644 index 0000000..7a76d24 --- /dev/null +++ b/libraries/ModifiedBulletX/ModifiedBulletX/Collision/CollisionDispatch/SphereSphereCollisionAlgorithm.cs | |||
@@ -0,0 +1,104 @@ | |||
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 | public class SphereSphereCollisionAlgorithm : CollisionAlgorithm | ||
30 | { | ||
31 | private bool _ownManifold; | ||
32 | private PersistentManifold _manifold; | ||
33 | |||
34 | public SphereSphereCollisionAlgorithm(PersistentManifold manifold, CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) | ||
35 | : base(collisionAlgorithmConstructionInfo) | ||
36 | { | ||
37 | _ownManifold = false; | ||
38 | _manifold = manifold; | ||
39 | |||
40 | if (_manifold == null) | ||
41 | { | ||
42 | _manifold = Dispatcher.GetNewManifold(bodyA, bodyB); | ||
43 | _ownManifold = true; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | public SphereSphereCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo) | ||
48 | : base(collisionAlgorithmConstructionInfo) { } | ||
49 | |||
50 | ~SphereSphereCollisionAlgorithm() | ||
51 | { | ||
52 | if (_ownManifold) | ||
53 | { | ||
54 | if (_manifold != null) | ||
55 | Dispatcher.ReleaseManifold(_manifold); | ||
56 | } | ||
57 | } | ||
58 | |||
59 | public override void ProcessCollision(CollisionObject bodyA, CollisionObject bodyB, DispatcherInfo dispatchInfo, ManifoldResult resultOut) | ||
60 | { | ||
61 | if (_manifold == null) | ||
62 | return; | ||
63 | |||
64 | SphereShape sphereA = bodyA.CollisionShape as SphereShape; | ||
65 | SphereShape sphereB = bodyB.CollisionShape as SphereShape; | ||
66 | |||
67 | Vector3 diff = bodyA.WorldTransform.Translation - bodyB.WorldTransform.Translation; | ||
68 | float len = diff.Length(); | ||
69 | float radiusA = sphereA.Radius; | ||
70 | float radiusB = sphereB.Radius; | ||
71 | |||
72 | //if distance positive, don't generate a new contact | ||
73 | if (len > (radiusA + radiusB)) | ||
74 | return; | ||
75 | |||
76 | //distance (negative means penetration) | ||
77 | float dist = len - (radiusA + radiusB); | ||
78 | |||
79 | Vector3 normalOnSurfaceB = diff / len; | ||
80 | //point on A (worldspace) | ||
81 | Vector3 posA = bodyA.WorldTransform.Translation - radiusA * normalOnSurfaceB; | ||
82 | //point on B (worldspace) | ||
83 | Vector3 posB = bodyB.WorldTransform.Translation + radiusB * normalOnSurfaceB; | ||
84 | |||
85 | // report a contact. internally this will be kept persistent, and contact reduction is done | ||
86 | resultOut.SetPersistentManifold(_manifold); | ||
87 | resultOut.AddContactPoint(normalOnSurfaceB, posB, dist); | ||
88 | } | ||
89 | |||
90 | public override float CalculateTimeOfImpact(CollisionObject bodyA, CollisionObject bodyB, DispatcherInfo dispatchInfo, ManifoldResult resultOut) | ||
91 | { | ||
92 | //not yet | ||
93 | return 1f; | ||
94 | } | ||
95 | |||
96 | public class CreateFunc : CollisionAlgorithmCreateFunction | ||
97 | { | ||
98 | public override CollisionAlgorithm CreateCollisionAlgorithm(CollisionAlgorithmConstructionInfo collisionAlgorithmConstructionInfo, CollisionObject bodyA, CollisionObject bodyB) | ||
99 | { | ||
100 | return new SphereSphereCollisionAlgorithm(null, collisionAlgorithmConstructionInfo, bodyA, bodyB); | ||
101 | } | ||
102 | } | ||
103 | } | ||
104 | } | ||