-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollisionManager.cs
137 lines (118 loc) · 3.85 KB
/
CollisionManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//Kamil Hawdziejuk
//03-06-2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InteractingMeshes.Tests;
namespace InteractingMeshes
{
/// <summary>
/// Collision Manager
/// </summary>
public class CollisionManager
{
#region --- Static fields ---
/// <summary>
/// Tolerance
/// </summary>
public static double Tolerance = 0.01;
/// <summary>
/// List of collided objects
/// </summary>
private static List<GeometricObject> collidedObjects = new List<GeometricObject>();
/// <summary>
/// List of collided objects
/// </summary>
public static List<GeometricObject> CollidedObjects
{
get
{
return collidedObjects;
}
}
/// <summary>
/// List of collision detectos
/// </summary>
private static List<ICollisionDetector> collisionDetectors = new List<ICollisionDetector>();
/// <summary>
/// Clears collided objects
/// </summary>
public static void ClearCollidedObjects()
{
collidedObjects.Clear();
}
/// <summary>
/// Register the detectos
/// </summary>
/// <param name="collisionDetector"></param>
public static void RegisterDetector(ICollisionDetector collisionDetector)
{
collisionDetectors.Add(collisionDetector);
}
#endregion
#region --- Private methods ---
/// <summary>
/// Adds collided object
/// </summary>
/// <param name="obj"></param>
private static void AddCollidedObject(GeometricObject obj)
{
if (!collidedObjects.Contains(obj))
{
collidedObjects.Add(obj);
}
}
#endregion
/// <summary>
/// Main function, that tests if two geometricObjects collide
/// </summary>
/// <param name="_obj1"></param>
/// <param name="_obj2"></param>
/// <returns></returns>
public static bool CollisionTest(GeometricObject _obj1, GeometricObject _obj2)
{
bool isCollision = false;
Options.CollisionStage collisionLevel = Options.CollisionLevel;
foreach (ICollisionDetector detector in collisionDetectors)
{
if (AcceptTest(collisionLevel, detector.CollisionLevel))
{
// CollisionTest test = new CollisionTest(detector.Name, _obj1, _obj2);
//test.Start();
if (detector.DetectCollision(_obj1, _obj2, Tolerance))
{
isCollision = true;
//break;
}
else
{
isCollision = false;
}
//test.Stop(isCollision);
//CollisionTestsManager.AddTest(test);
}
}
if (isCollision)
{
AddCollidedObject(_obj1);
AddCollidedObject(_obj2);
}
return isCollision;
}
/// <summary>
/// Does the level of collision testing has flag for testing this level
/// </summary>
/// <param name="_level"></param>
/// <param name="_stage"></param>
/// <returns></returns>
public static bool AcceptTest(Options.CollisionStage _level, Options.CollisionStage _flag)
{
if ((_level & _flag) == _flag)
{
return true;
}
return false;
}
}
}