-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionSystem.cs
More file actions
152 lines (111 loc) · 4.53 KB
/
CollisionSystem.cs
File metadata and controls
152 lines (111 loc) · 4.53 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Drawing;
using System.Windows.Forms;
namespace CollisionSimulation
{
public class CollisionSystem: Graphics
{
//private readonly static double hz = 0.5;
public double currentTime { get; private set; }
public PriorityQueue pq { get; private set; }
public Particle[] particles;// { get; private set; }
//Graphics g = new Graphics();
private Pen p = new Pen(Color.Aqua, 2);
public CollisionSystem (Particle[] particles, int windowWidth, int windowHeight)
{
this.particles = (Particle []) particles.Clone(); //shallow copy, new array references the parameter, does not create an extra copy
this.Height = windowHeight;
this.Width = windowWidth;
}
public void predict(Particle a, double timeLimit)
{
if (a == null) return;
//particle particle collision
for (int i = 0; i < particles.Length; i++)
{
double dt = a.timeToHit(particles[i]);
if(currentTime + dt <= timeLimit)
{
pq.insertEvent(new Event(currentTime + dt, a, particles[i]));
}
}
int sizeArr = pq.getArraySize();
//particle wall collisions
double dtX = a.timeToHitVertWall();
double dtY = a.timeToHitHorizontalWall();
if (currentTime + dtX <= timeLimit)
{
pq.insertEvent(new Event(currentTime + dtX, a, null)); //vert wall
}
if(currentTime + dtY <= timeLimit)
{
pq.insertEvent(new Event(currentTime + dtY, null, a)); //horizontal wall
}
}
//simulates the particles for a given amount of time
public void simulate(double timeLimit)
{
pq = new PriorityQueue();
for (int i = 0; i < particles.Length; i++)
{
predict(particles[i], timeLimit);
}
//main simulation loop
while (pq.getNumItems() != 0)
{
Event impendingEvent = pq.delMin(); //get impending event, discard if invalidated
if (!impendingEvent.isValid(currentTime))
{
continue;
}
System.Console.WriteLine(impendingEvent.time);
Particle a = impendingEvent.getParticleA();
Particle b = impendingEvent.getParticleB();
for (int i = 0; i < particles.Length; i++) // physical collision, so update positions and simulation clock
{
particles[i].move(impendingEvent.time - currentTime);
}
currentTime = impendingEvent.time;
args.Graphics.Clear(Color.Gray);
drawParticles(particles);
//process events
if (a != null && b != null) a.bounceOff(b);
else if (a != null && b == null)
{
a.bounceOffVertWall();
}
else if (b != null && a == null)
{
b.bounceOffHorizontalWall();
}
//no null, null
// update the priority queue with new collisions involving a or b
predict(a, timeLimit);
predict(b, timeLimit);
}
}
public override void Graphics_Paint(object sender, PaintEventArgs e)
{
this.args = e;
e.Graphics.DrawRectangle(p, 0, 0, 500, 500);
simulate(10000);
}
public void drawParticles(Particle[] particles)
{
Invalidate();
for (int i = 0; i < particles.Length; i++)
{
int x = particles[i].centerX - particles[i].radius;
int y = particles[i].centerY - particles[i].radius;
int size = (int)particles[i].radius * 2;
args.Graphics.DrawEllipse(p, x, y, size, size);
}
}
/***************************************************************************
* - a and b both null: redraw event
* - a not null, b null: collision with vertical wall
* - a null, b not null: collision with horizontal wall
* - a and b both not null: binary collision between a and b
*
***************************************************************************/
}
}