-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
311 lines (248 loc) · 12.3 KB
/
Program.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
using SFML.Graphics;
using SFML.System;
using SFML.Window;
using static bouncyballs.util;
namespace bouncyballs {
static class Program {
static void Main(string[] args) {
bouncyballs bb = new bouncyballs();
bb.run();
}
}
public class bouncyballs {
#region "Properties"
Vector2f viewPos;
Vector2f screenSize;
float delta = 0;
double RunTime = 0; // time program has been running
DateTime lastTime = DateTime.Now;
float timeScale = 1f;
float gravityMulti = 9.81f;
Random rand = new Random();
Vector2i mousePos;
bool grabbedWindow = false;
Vector2i grabbedOffset;
Vector2i lastMousePos;
Font arial;
RenderWindow window;
View view;
List<body>bodies = new List<body>();
float timestep = 1f / 100f;
float maxSpeed = 1000f;
#endregion
#region "Methods"
public bouncyballs() {
arial = new Font("arial.ttf");
screenSize = new Vector2f(800, 600);
window = new RenderWindow(new VideoMode((uint)screenSize.X, (uint)screenSize.Y),
"Bouncy Balls!",
Styles.Close | Styles.Titlebar);
window.Closed += window_CloseWindow;
window.Resized += window_ResizedWindow;
window.KeyPressed += window_KeyPressed;
window.KeyReleased += window_KeyReleased;
window.MouseButtonPressed += window_MouseButtonPressed;
window.MouseButtonReleased += window_MouseButtonReleased;
window.MouseMoved += window_MouseMoved;
viewPos = screenSize / 2;
view = new View(viewPos, screenSize);
window.SetView(view);
window.SetKeyRepeatEnabled(false);
}
public void run() {
Color grey = new Color(100, 100, 100);
float borderThickness = 10f;
float edgeBounciness = 1f;
// edge of window is just a fixed rectangle
rectbody topEdge = new rectbody(new Vector2f(screenSize.X, borderThickness),
new Vector2f(screenSize.X / 2f, borderThickness / 2f),
grey);
bodies.Add(topEdge);
rectbody bottomEdge = new rectbody(new Vector2f(screenSize.X, borderThickness),
new Vector2f(screenSize.X / 2f, screenSize.Y - borderThickness / 2f),
grey);
bodies.Add(bottomEdge);
rectbody leftEdge = new rectbody(new Vector2f(borderThickness, screenSize.Y),
new Vector2f(borderThickness / 2f, screenSize.Y / 2f),
grey);
bodies.Add(leftEdge);
rectbody rightEdge = new rectbody(new Vector2f(borderThickness, screenSize.Y),
new Vector2f(screenSize.X - borderThickness / 2f, screenSize.Y / 2f),
grey);
bodies.Add(rightEdge);
// bucket hit box
rectbody bucketBottom = new rectbody(new Vector2f(100, 20),
screenSize / 2f,
grey);
bodies.Add(bucketBottom);
rectbody bucketLeft = new rectbody(new Vector2f(20, 100),
bucketBottom.Position + new Vector2f(-40, -60),
grey);
bodies.Add(bucketLeft);
rectbody bucketRight = new rectbody(new Vector2f(20, 100),
bucketBottom.Position + new Vector2f(40, -60),
grey);
bodies.Add(bucketRight);
foreach(body b in bodies) {
b.isStatic = true;
b.Bounciness = edgeBounciness;
}
int numBalls = 10;
for(int i = 0; i < numBalls; i++) {
float radius = 10f + (float)rand.NextDouble() * 10f;
float mass = randfloat(100, 300);
Vector2f pos = randvec2(radius + borderThickness,
screenSize.X - radius - borderThickness,
radius + borderThickness,
screenSize.Y - radius - borderThickness);
Color fillColour = hsvtocol(360f / numBalls * i, 1, 1);
circlebody B = new circlebody(radius, pos, fillColour, mass);
B.Bounciness = 0.95f; // bouncy balls are 95% efficient in collisions
bodies.Add(B);
}
bodies.Reverse();
// main loop
while (window.IsOpen) {
if ((float)(DateTime.Now - lastTime).TotalSeconds < timestep) {
continue;
}
mousePos = Mouse.GetPosition();
//delta = (float)(DateTime.Now - lastTime).TotalMilliseconds / 1000;
delta = timestep;
lastTime = DateTime.Now;
RunTime += delta;
window.Clear();
// get window velocity if its being moved
Vector2f windowVel = new Vector2f();
if (grabbedWindow) {
float mouseMoveMulti = 750f;
windowVel += (Vector2f)(Mouse.GetPosition() - lastMousePos) * delta * mouseMoveMulti * timeScale;
}
lastMousePos = Mouse.GetPosition();
foreach(body b in bodies) {
// calculate collisions
for (int i = 0; i < bodies.Count; i++) {
body c = bodies[i];
if (b == c) { continue; }
// // TRYING TO PREVENT TUNNELLING START
// // check for time of impact to try
// // alleviate tunneling
// Vector2f cbdir = normalise(c.Position - b.Position);
// Vector2f veldir = normalise(b.Velocity);
// Vector2f nextPos = b.Position + b.Velocity * delta * timeScale;
// // if the body is on the opposite side of the other
// // body then we have gone too far!
// Vector2f curPosToOtherBody = normalise(c.Position - b.Position);
// Vector2f nextPosToOtherBody = normalise(c.Position - nextPos);
// if (dot(curPosToOtherBody, nextPosToOtherBody) < 0) {
// string reportIssue = "Tunnelling has potentially occurred!";
// // if the body is moving towards the other body
// // then we will check for potential tunnelling
// Vector2f bcnorm = normalise(c.Position - b.Position);
// Vector2f velnorm = normalise(b.Velocity * delta * timeScale);
// float normdotnorm = dot(bcnorm, velnorm);
// if (normdotnorm > 0) {
// float dist = distance(c.Position, b.Position);
// float velmag = magnitude(b.Velocity * delta * timeScale);
// float timeOfImpact = dist / velmag / normdotnorm;
// if (timeOfImpact < 1) {
// reportIssue += " (2)";
// b.SetVelocity(new Vector2f());
// b.isStatic = true;
// b.OutlineColour = Color.White;
// }
// }
// Console.WriteLine(reportIssue);
// }
// // TRYING TO PREVENT TUNNELLING END
b.collide(c);
}
// get other velocities
if (b.GetType() == typeof(circlebody)) {
circlebody cb = (circlebody)b;
cb.SetVelocity(cb.Velocity + windowVel);
//gravity
cb.SetYVelocity(cb.Velocity.Y + cb.Mass * gravityMulti * delta * timeScale);
}
if (magnitude(b.Velocity) > maxSpeed) {
b.SetVelocity(normalise(b.Velocity) * maxSpeed);
}
b.update(delta * timeScale);
b.draw(window);
}
window.DispatchEvents();
window.Display();
} // end while(window.IsOpen)
}
#endregion
#region "Events"
void window_CloseWindow(object? sender, System.EventArgs? e) {
if (sender == null) { return; }
((RenderWindow)sender).Close();
}
void window_ResizedWindow(object? sender, System.EventArgs? e) {
if (sender == null) { return; }
if (e == null) { return; }
SizeEventArgs sizeE = (SizeEventArgs)e;
screenSize = new Vector2f(sizeE.Width, sizeE.Height);
view.Reset(new FloatRect(0, 0, sizeE.Width, sizeE.Height));
window.SetView(view);
//Console.WriteLine("Resized window!");
}
void window_MouseButtonPressed(object? sender, System.EventArgs e) {
if (sender == null) { return; }
if (e == null) { return; }
if (e.GetType() == typeof(MouseButtonEventArgs)) {
MouseButtonEventArgs mouseButton = (MouseButtonEventArgs)e;
//Console.WriteLine(String.Format("Button {0} pressed at ({1}, {2})", mouseButton.Button.ToString(), mouseButton.X.ToString(), mouseButton.Y.ToString()));
if (mouseButton.Button == Mouse.Button.Left) {
grabbedOffset = (Vector2i)window.Position - Mouse.GetPosition();
grabbedWindow = true;
}
}
}
void window_MouseButtonReleased(object? sender, System.EventArgs e) {
if (sender == null) { return; }
if (e == null) { return; }
if (e.GetType() == typeof(MouseButtonEventArgs)) {
MouseButtonEventArgs mouseButton = (MouseButtonEventArgs)e;
//Console.WriteLine(String.Format("Button {0} released at ({1}, {2})", mouseButton.Button.ToString(), mouseButton.X.ToString(), mouseButton.Y.ToString()));
if (mouseButton.Button == Mouse.Button.Left) {
grabbedWindow = false;
}
}
}
void window_MouseMoved(object? sender, System.EventArgs e) {
if (sender == null) { return; }
if (e == null) { return; }
if (e.GetType() == typeof(MouseMoveEventArgs)) {
if (grabbedWindow) {
window.Position = Mouse.GetPosition() + grabbedOffset;
}
}
}
void window_KeyPressed(object? sender, System.EventArgs e) {
if (sender == null) { return; }
if (e == null) { return; }
if (e.GetType() == typeof(KeyEventArgs)) {
KeyEventArgs key = (KeyEventArgs)e;
//Console.WriteLine(String.Format("Key {0} was pressed", key.Code.ToString()));
if (key.Code == Keyboard.Key.Escape) {
window.Close();
}
if (key.Alt && key.Code == Keyboard.Key.F4) {
window.Close();
}
}
}
void window_KeyReleased(object? sender, System.EventArgs e) {
if (sender == null) { return; }
if (e == null) { return; }
if (e.GetType() == typeof(KeyEventArgs)) {
KeyEventArgs key = (KeyEventArgs)e;
//Console.WriteLine(String.Format("Key {0} was released", key.Code.ToString()));
}
}
#endregion
}
}