-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
1901 lines (1658 loc) · 74.8 KB
/
Game.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using System.Windows.Forms;
using System.IO;
//using SharpDX.XAudio2;
//using SharpDX.Multimedia;
//using SharpDX.IO;
//to be added through nuget:
//SharpDX 4.2.0 (Alexandre Mutel)
//SharpDX.Xaudio2 4.2.0(Alexandre Mutel)
namespace SphereDivision
{
/*
Features to add:
quad faces
face colors for different tri types
sounds based on face area, alternative to line length
Auto-show? (automatically randomly change settings)
dividing a hypersphere(4d, 5d?)
*/
class Game : GameWindow
{
/// New Vars
///
// up to 12 critters can exist.
// each critter can have up to 16384 points
// each point has a position and velocity
int numCritters = 4;
const int maxCritters = 23200;
const int maxPoints = 1601;
Vector2[,] bPos = new Vector2[maxCritters, maxPoints];
float[,] zPos = new float[maxCritters, maxPoints];
Vector2[] bVel = new Vector2[maxCritters];
float[,] circleSize = new float[maxCritters, maxPoints]; //only for drawing circles, tracks size to avoid later recalculation
//Vector3[,] bHSL = new Vector3[maxCritters, maxPoints]; //expressed in HSL, needs to be looked up when newly calculated
Vector3[,] bColor = new Vector3[maxCritters, maxPoints]; //this is the actual draw color, expressed in RGB
int[] currentHue = new int[maxCritters]; //desired hue to reach
int[] hueTarget = new int[maxCritters]; //desired hue to reach
int[] hueSpeed = new int[maxCritters]; //how fast to go toward this hue
int[] hueTimer = new int[maxCritters]; //how fast to go toward this hue
float maxVel = 2f;
float minVel = 1f;
int connectMode = 0;
//0 = individual bugs (lines)
//1 = Individual squares
//2 = Individual circles
//3 = mystify style (each 4 critters form a loop)
//brightness cycle mode, size cycle mode
float lineWidth = 4f;
bool edgesOn = false;
int trailLength = 6; //number from 1 to maxpoints, how many trailing points are drawn and tracked
int trailSkip = 1;
public int polygonLength = 4; //for mystify and bowtie drawing styles
//for tracking trail positions, instead of copying the entire trail each frame
//we just move a pointer along.
int trailStart = 1;
int trailEnd = 2;
int trailPrev = 0;
Vector3[] lookupHSL = new Vector3[768]; //Hue is looked up, Saturation and Lightness (if <1) are calculated
// gives full saturation of indexed hue 0-767
float mouseGravStrength = 0.1f;
int MouseGrav = 0;// Mouse cursor acts as an attractor.
//0= off
//1= directional (distance doesn't matter)
//2= linear falloff
//3= Squared Falloff
//color cycling:
//totally random: each critter goes from one random hue to another
// Synched Random: each critter is its own different hue and timing
// Grayscale: stays on white/back
// Rainbow: cycles in order
int rainbowMode = 3;
int rainbowPosition = 0; //current rainbow color to lookup in hue chart
int rainbowSpeed = 5; //how fast the colors cycle
bool brightnessMode = true;
float[] brightnessValue = new float[maxCritters];
float[] brightnessDir = new float[maxCritters];
float brightnessCycleSpeed = 0.1f;
bool brightnessSynch = false; //if true, all brightness is set to the same value all the time
bool thicknessMode = true;
float[,] thicknessValue = new float[maxCritters, maxPoints];
float[] thicknessDir = new float[maxCritters];
float thicknessCycleSpeed = 01f;
bool thicknessSynch = false;
bool attractorMode = false;
// gravpoints have their own separate physics params and routines
bool gravMode = true;
bool showGravPoints = true; //draw them visibly
int numGravPoints = 4;
const int maxGravPoints = 16;
Vector2[] gPos = new Vector2[maxGravPoints];
Vector2[] gVel = new Vector2[maxGravPoints];
float gravPointStrength = 0.05f;
int gravFalloff = 1; //0 = Directional, 1 = 1/dist, 2 = 1/dist^2
float maxGravSpeed = 0.5f;
float minGravSpeed = 0.05f;
float cycleTime = 1000f;
bool SoundReaction = false;
string soundFile = ""; //sound file to play and react to
/// <summary>
/// OLD VARS
/// </summary>
float viewScale = 1f;
bool showVerts = true;
float zWall;
bool wireFrame = true;
bool showBacks = true;
bool showHelp = false;
float lineThickness = 4f;
float binocSplit = 500f;
bool rotateNow = true; //rotates visualization if true
float spinAngle = 0f; //updated each tick to rotate visualization
float spinAngleY = 0f;
bool fixedTop = false; //when true, point zero is fixed to the top of the sphere
int lockedVerts = 0; // if > 0 then any verts up to this index get locked in position, this is for attempting to subdivide
int findFaces = 1; //when 1 tries to find triangular faces, 2 shows flower view, 0 no faces rendered
bool binocular = false; //when true, renders crosseyed 3d view
bool haltGrav = false; //when true, stops trying to move points around until next reset
bool inputMode = false;
string inputVal;
bool exitQuestion = false;
bool hideMessage = false;
// my vars
Random random = new Random();
long lastTime, curTime, deltaTime; //milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
int tempTicks; //number of ticks per loop
int fps; //for counting how many frames are rendered each second
int fpsCount; //to be displayed, updated once per second
long fpsTime;
long tickCount = 0;
Vector2 lastMousePos = new Vector2();
bool lastButtonState = false; //previous mouse button state so we can tell when it's first pressed
int lastWheel;
KeyboardState keyboard, lastkeyboard;
bool[] keysHeld = new bool[5]; //set to true when this key is to be activated by timer
int[] keyTimers = new int[5]; //tracks how long since last keypress
enum keysUsed
{
Right, Left, PageUp, PageDown, Enter
}
//sound stuff
// XAudio2 xaudio;
// WaveFormat waveFormat;
// AudioBuffer buffer;
// SoundStream soundstream;
// SourceVoice[] sourceVoice;
int soundOn = 0;
int currentVoice = 0;
int voiceLoop = 0;
public Game()
: base(1920, 1080, new OpenTK.Graphics.GraphicsMode(32, 24, 0, 4))
{
}
Form1 settingsForm;
/// <summary>
/// writes current game settings to the visible form for users to see
/// </summary>
void sendSettings()
{
}
/// <summary>
/// gets settings from the visible form and writes to internal values
/// </summary>
void readSettings()
{
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
settingsForm = new Form1();
settingsForm.Show();
sendSettings();
//for 4d testing:
//binocular = false;
//findFaces = 0;
//lineLevel = 0;
//this.Cursor = OpenTK.MouseCursor.Cross;
//initAudio();
//initProgram();
// hide max,min and close button at top right of Window
//FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
// fill the screen
initHSL();
initcirclePoints();
initCritters();
this.WindowBorder = WindowBorder.Hidden;
Bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
Title = "Flying Bugs";
GL.ClearColor(System.Drawing.Color.CornflowerBlue);
GL.PointSize(5f);
}
Vector2[] circlePoints = new Vector2[64];
void initcirclePoints()
{
int pNum = 0;
double factor = Math.PI * 2d / 64d;
for (int i = 0; i < 64; i++)
{
circlePoints[pNum].X = (float)Math.Sin((double)i * factor);
circlePoints[pNum].Y = (float)-Math.Cos((double)i * factor);
pNum++;
}
}
void initHSL()
{
for (int i = 0; i < 128; i++)
{
lookupHSL[i].X = 1.0f;
lookupHSL[i].Y = (float)i / 128f;
lookupHSL[i].Z = 0.0f;
}
for (int i = 128; i < 256; i++)
{
lookupHSL[i].X = 1f - ((float)(i - 128) / 128f);
lookupHSL[i].Y = 1.0f;
lookupHSL[i].Z = 0.0f;
}
for (int i = 256; i < 384; i++)
{
lookupHSL[i].X = 0.0f;
lookupHSL[i].Y = 1.0f;
lookupHSL[i].Z = (float)(i - 256) / 128f;
}
for (int i = 384; i < 512; i++)
{
lookupHSL[i].X = 0.0f;
lookupHSL[i].Y = 1f - ((float)(i - 384) / 128f);
lookupHSL[i].Z = 1.0f;
}
for (int i = 512; i < 640; i++)
{
lookupHSL[i].X = (float)(i - 512) / 128f;
lookupHSL[i].Y = 0f;
lookupHSL[i].Z = 1f;
}
for (int i = 640; i < 768; i++)
{
lookupHSL[i].X = 1f;
lookupHSL[i].Y = 0f;
lookupHSL[i].Z = 1f - ((float)(i - 640) / 128f);
}
}
//void initAudio()
//{
// xaudio = new XAudio2();
// MasteringVoice masteringsound = new MasteringVoice(xaudio);
// NativeFileStream nativefilestream = new NativeFileStream(@"ding.wav", NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);
// soundstream = new SoundStream(nativefilestream);
// waveFormat = soundstream.Format;
// buffer = new AudioBuffer
// {
// Stream = soundstream.ToDataStream(),
// AudioBytes = (int)soundstream.Length,
// Flags = BufferFlags.EndOfStream
// };
// sourceVoice = new SourceVoice[32];
// for (int i = 0; i < sourceVoice.Length; i++)
// {
// sourceVoice[i] = new SourceVoice(xaudio, waveFormat, VoiceFlags.None, 4f);
// }
//}
void BounceOffEdges()
{
}
void LimitVelocity(int critter)
{
float vel = (float)Math.Sqrt((bVel[critter].X * bVel[critter].X) + (bVel[critter].Y + bVel[critter].Y));
if (vel > maxVel)
{
bVel[critter].X = (bVel[critter].X / vel) * maxVel;
bVel[critter].Y = (bVel[critter].Y / vel) * maxVel;
}
if (vel < minVel)
{
bVel[critter].X = (bVel[critter].X / vel) * minVel;
bVel[critter].Y = (bVel[critter].Y / vel) * minVel;
}
}
void getSettingsFromForm()
{
if (settingsForm.requestExit) Exit();
numCritters = settingsForm.numCritters;
polygonLength = settingsForm.polygonLength;
connectMode = settingsForm.connectMode;
edgesOn = settingsForm.edgesOn;
trailLength = settingsForm.trailLength;
rainbowSpeed = settingsForm.rainbowSpeed;
rainbowMode = settingsForm.rainbowMode;
brightnessMode = settingsForm.brightnessMode;
brightnessCycleSpeed = settingsForm.brightnessCycleSpeed;
lineWidth = settingsForm.lineWidth;
trailSkip = settingsForm.trailSkip;
gravMode = settingsForm.gravMode;
showGravPoints = settingsForm.showGravPoints;
numGravPoints = settingsForm.numGravPoints;
gravPointStrength = settingsForm.gravPointStrength;
gravFalloff = settingsForm.gravFalloff;
thicknessMode = settingsForm.thicknessMode;
thicknessCycleSpeed = settingsForm.thicknessCycleSpeed;
thicknessSynch = settingsForm.thicknessSynch;
mouseGravStrength = settingsForm.mouseGravStrength;
MouseGrav = settingsForm.MouseGrav;
if (settingsForm.newCycleTime)
{
settingsForm.newCycleTime = false; //reset each time it's changed
cycleTime = (float)settingsForm.cycleTime;
}
if (settingsForm.restartNow)
{
settingsForm.restartNow = false;
initCritters();
}
}
private void makeNewSettings()
{
if (settingsForm.cycleTime == 0) return; //don't change settings if user hasn't selected either or if timer is set to zero
//generate random settings or choose a random preset
int choice = 0;
if (settingsForm.usePresets) choice = 1;
if (settingsForm.useRandom) choice += 2;
switch (choice)
{
case 0: //no checkboxes selected
if (settingsForm.reExplode) initCritters();
break;
case 1: //usepresets only
settingsForm.chooseRandomConfig();
break;
case 2: //use random only
settingsForm.createRandomSettings();
break;
case 3: //use either/or
if (random.NextDouble() > 0.5) settingsForm.chooseRandomConfig();
else settingsForm.createRandomSettings();
break;
}
cycleTime = (float)settingsForm.cycleTime;
}
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
getSettingsFromForm();
//if (soundOn>0) playSounds();
curTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
deltaTime = curTime - lastTime;
fps++;
fpsTime += deltaTime;
if (fpsTime >= 1000)
{
fpsTime -= 1000;
fpsCount = fps;
fps = 0;
}
lastTime = curTime;
settingsForm.showFPS(fpsCount, deltaTime);
if (settingsForm.paused) return;
cycleTime -= deltaTime;
settingsForm.updateClock((int)cycleTime);
if (cycleTime <= 0f)
{
cycleTime = 0f;
makeNewSettings();
}
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
//set up the viewport's drawing scale so that everything shows up
//get the window's aspect ratio
float targetAspectRatio = (float)this.Width / this.Height; // 1.777
GL.ClearColor(System.Drawing.Color.Black);
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
float maxDepth = 1000f;
if (maxDepth < 1000) maxDepth = 1000f;
GL.Ortho(-1000 * targetAspectRatio, 1000 * targetAspectRatio, 1000, -1000, maxDepth, -maxDepth);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.LineSmooth);
GL.Enable(EnableCap.Blend);
//GL.Enable(EnableCap.AlphaTest);
//GL.LineStipple(1, 0x0101);
float screenLeft = -1000f * targetAspectRatio;
float screenRight = 1000f * targetAspectRatio;
float screenTop = -1000f;
float screenBot = 1000f;
//Advance trailing points and start pointer
trailPrev = trailStart;
trailStart--;
if (trailStart < 1) trailStart += 1600;
trailEnd = trailStart + trailLength;// + 1;
if (trailEnd > 1601) trailEnd -= 1601;
//apply Mouse Gravity (if enabled)
if (MouseGrav > 0)
{
//convert mouse screen coords to GL coordinates
//Console.WriteLine(lastMousePos.X + "," + lastMousePos.Y);
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Lines);
GL.Color3(0.5f, 0.5f, 0.5f);
GL.Vertex2(lastMousePos.X - 8, lastMousePos.Y);
GL.Vertex2(lastMousePos.X + 8, lastMousePos.Y);
GL.End();
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Lines);
GL.Vertex2(lastMousePos.X, lastMousePos.Y - 8);
GL.Vertex2(lastMousePos.X, lastMousePos.Y + 8);
GL.End();
for (int c = 0; c < numCritters; c++)
{
//get vector pointing from point to mouse
Vector2 g = lastMousePos - bPos[c, trailPrev];
float dist = g.Length;
Vector2 direction = g.Normalized();
if (dist > 0.1f)
{
switch (MouseGrav)
{
case 1: //directional only
bVel[c] += (direction * mouseGravStrength);
break;
case 2:
//dist = g.Length;
bVel[c] += (direction * mouseGravStrength / dist);
break;
case 3:
dist = dist * dist;
bVel[c] += (direction * mouseGravStrength / dist);
break;
}
}
}
}
//apply Points Gravity (if enabled)
if (gravMode)
{
for (int g = 0; g < numGravPoints; g++)
{
//Move them forward by their velocity
gPos[g] += gVel[g] * deltaTime;
//keep them on-screen
if (gPos[g].X < screenLeft)
{
gPos[g].X = screenLeft + (screenLeft - gPos[g].X);
gVel[g].X = minGravSpeed + (float)random.NextDouble() * maxGravSpeed;
}
else if (gPos[g].X > screenRight)
{
gPos[g].X = screenRight - (gPos[g].X - screenRight);
gVel[g].X = -minGravSpeed -(float)random.NextDouble() * maxGravSpeed;
}
if (gPos[g].Y < screenTop)
{
gPos[g].Y = screenTop + (screenTop - gPos[g].Y);
gVel[g].Y = minGravSpeed + (float)random.NextDouble() * maxGravSpeed;
}
else if (gPos[g].Y > screenBot)
{
gPos[g].Y = screenBot - (gPos[g].Y - screenBot);
gVel[g].Y = -minGravSpeed -(float)random.NextDouble() * maxGravSpeed;
}
}
if (showGravPoints)
{
for (int g = 0; g < numGravPoints; g++)
{
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Lines);
GL.Color3(0.5f, 0.5f, 0.5f);
GL.Vertex2(gPos[g].X - 6, gPos[g].Y);
GL.Vertex2(gPos[g].X + 6, gPos[g].Y);
GL.End();
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.Lines);
GL.Vertex2(gPos[g].X, gPos[g].Y - 6);
GL.Vertex2(gPos[g].X, gPos[g].Y + 6);
GL.End();
}
}
for (int c = 0; c < numCritters; c++)
{
for (int g = 0; g < numGravPoints; g++)
{
//get vector pointing from point to gravwell
Vector2 gravVector = gPos[g] - bPos[c, trailPrev];
float dist = gravVector.Length;
Vector2 direction = gravVector.Normalized();
if (dist > 0.1f)
{
switch (gravFalloff)
{
case 0:
bVel[c] += (direction * gravPointStrength * deltaTime);
break;
case 1:
bVel[c] += (direction * gravPointStrength * deltaTime / dist);
break;
case 2:
dist = dist * dist;
bVel[c] += (direction * gravPointStrength * deltaTime / dist);
break;
case 3:
bVel[c] -= (direction * gravPointStrength * deltaTime / dist);
break;
case 4:
bVel[c] -= (direction * gravPointStrength * deltaTime);
break;
}
}
}
LimitVelocity(c); //don't let gravpoints fling them too fast!
}
}
if (attractorMode) // do cool chaotic attractor mode with lorenz system
{
float sigma = 10f;
float rho = 28f;
float beta = 8f / 3f;
float time = 0.025f;
for (int c = 0; c < numCritters; c++)
{
float xt = 0, yt = 0, zt = 0;
//normalize/scale to a number less than 10:
//Vector3 totalSize = new Vector3(bPos[c, 0].X, bPos[c, 0].Y, zPos[c, 0]);
//float len = totalSize.Length;
//if (len > 1)
//{
// Console.WriteLine("length " + len + "exceeds limit");
// totalSize.Normalize();
// totalSize *= 1f;
// bPos[c, 0].X = totalSize.X;
// bPos[c, 0].Y = totalSize.Y;
// zPos[c, 0] = totalSize.Z;
//}
float xVel = sigma * (bPos[c, trailStart].Y - bPos[c, trailStart].X);
float yVel = (bPos[c, trailStart].X * (rho - zPos[c, trailStart])) - bPos[c, trailStart].Y;
float zVel = (bPos[c, trailStart].X * bPos[c, trailStart].Y) - (beta * zPos[c, trailStart]);
bPos[c, trailStart].X += xVel * time;
bPos[c, trailStart].Y += yVel * time;
zPos[c, trailStart] += zVel * time;
////rescale to fit on screen
//if (bPos[c, 0].X > largest) largest = bPos[c, 0].X;
//if (bPos[c, 0].Y > largest) largest = bPos[c, 0].Y;
//float scale = screenBot / largest;
//bPos[c, 0].X *= scale;
//bPos[c, 0].Y *= scale;
//zPos[c, 0] *= scale;
//Console.WriteLine(bPos[c, 0].X + ", " + bPos[c, 0].Y + ", " + zPos[c, 0]);
if (rainbowMode == 3)
{
bColor[c, trailStart] = lookupHSL[rainbowPosition] * brightnessValue[c];
}
else
{
bColor[c, trailStart] = new Vector3(1f, 1f, 1f) * brightnessValue[c];
}
}
}
else //Regular Newtonian Physics for particle movement
{
if (edgesOn)
{
//update point locations and velocities
for (int c = 0; c < numCritters; c++)
{
bPos[c, trailStart] = bPos[c, trailPrev] + bVel[c] * deltaTime;
//Edge Detection
if (bPos[c, trailStart].X < screenLeft)
{
bPos[c, trailStart].X = screenLeft + (screenLeft - bPos[c, trailStart].X);
bVel[c].X = (float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
else if (bPos[c, trailStart].X > screenRight)
{
bPos[c, trailStart].X = screenRight - (bPos[c, trailStart].X - screenRight);
bVel[c].X = -(float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
if (bPos[c, trailStart].Y < screenTop)
{
bPos[c, trailStart].Y = screenTop + (screenTop - bPos[c, trailStart].Y);
bVel[c].Y = (float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
else if (bPos[c, trailStart].Y > screenBot)
{
bPos[c, trailStart].Y = screenBot - (bPos[c, trailStart].Y - screenBot);
bVel[c].Y = -(float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
}
}
else //if edges are off, we just let things fly to double the screensize instead
{
float lLeft = screenLeft * 2f;
float lRight = screenRight * 2f;
float lTop = screenTop * 2f;
float lBottom = screenBot * 2f;
//update point locations and velocities
for (int c = 0; c < numCritters; c++)
{
bPos[c, trailStart] = bPos[c, trailPrev] + bVel[c] * deltaTime;
//Edge Detection
if (bPos[c, trailStart].X < lLeft)
{
bPos[c, trailStart].X = lLeft + (lLeft - bPos[c, trailStart].X);
bVel[c].X = (float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
else if (bPos[c, trailStart].X > lRight)
{
bPos[c, trailStart].X = lRight - (bPos[c, trailStart].X - lRight);
bVel[c].X = -(float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
if (bPos[c, trailStart].Y < lTop)
{
bPos[c, trailStart].Y = lTop + (lTop - bPos[c, trailStart].Y);
bVel[c].Y = (float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
else if (bPos[c, trailStart].Y > lBottom)
{
bPos[c, trailStart].Y = lBottom - (bPos[c, trailStart].Y - lBottom);
bVel[c].Y = -(float)random.NextDouble() * maxVel;
LimitVelocity(c);
}
}
}
}
//deal with colors and hues:
if (rainbowMode == 0) //random hues
{
for (int c = 0; c < numCritters; c++)
{
hueTimer[c] -= 1;
if (hueTimer[c] <= 0)
{
//time to pick a new hue and set up speed/direction
hueTarget[c] = random.Next(0, 768);
int dir;
if (hueTarget[c] > currentHue[c]) dir = 1; else dir = -1;
if (Math.Abs(hueTarget[c] - currentHue[c]) > 384)
{
dir *= -1;
}
hueSpeed[c] = random.Next(1, 100) * dir;
hueTimer[c] = random.Next(1, 10) * 60;
}
if (hueSpeed[c] != 0)
{
currentHue[c] += hueSpeed[c];
if (currentHue[c] < 0) currentHue[c] += 768;
else if (currentHue[c] > 767) currentHue[c] -= 768;
if (Math.Abs(currentHue[c] - hueTarget[c]) <= Math.Abs(hueSpeed[c]))
{
currentHue[c] = hueTarget[c];
hueSpeed[c] = 0;
}
}
bColor[c, trailStart] = lookupHSL[currentHue[c]] * brightnessValue[c];
}
}
else if (rainbowMode == 1) //synched random hues
{ //same as above except we only need to do this for a single critter.
hueTimer[0] -= 1;
if (hueTimer[0] <= 0)
{
//time to pick a new hue and set up speed/direction
hueTarget[0] = random.Next(0, 768);
int dir;
if (hueTarget[0] > currentHue[0]) dir = 1; else dir = -1;
if (Math.Abs(hueTarget[0] - currentHue[0]) > 384)
{
dir *= -1;
}
hueSpeed[0] = random.Next(1, 100) * dir;
hueTimer[0] = random.Next(1, 10) * 60;
}
if (hueSpeed[0] != 0)
{
currentHue[0] += hueSpeed[0];
if (currentHue[0] < 0) currentHue[0] += 768;
else if (currentHue[0] > 767) currentHue[0] -= 768;
if (Math.Abs(currentHue[0] - hueTarget[0]) <= Math.Abs(hueSpeed[0]))
{
currentHue[0] = hueTarget[0];
hueSpeed[0] = 0;
}
}
for (int c = 0; c < numCritters; c++)
{
bColor[c, trailStart] = lookupHSL[currentHue[0]] * brightnessValue[c];
}
}
else if (rainbowMode == 2) //grayscale
{
for (int c = 0; c < numCritters; c++)
{
bColor[c, trailStart] = new Vector3(1f, 1f, 1f) * brightnessValue[c];
}
}
else// (rainbowMode == 3)
{
for (int c = 0; c < numCritters; c++)
{
bColor[c, trailStart] = lookupHSL[rainbowPosition] * brightnessValue[c];
}
}
Vector3 rainbowColor;
rainbowColor = lookupHSL[rainbowPosition];
rainbowPosition += rainbowSpeed;
if (rainbowPosition > 767) rainbowPosition = 0;
if (brightnessMode)
{
if (brightnessSynch)
{
brightnessValue[0] += brightnessDir[0];
if (brightnessValue[0] > 1f)
{
brightnessValue[0] = 1f - (brightnessValue[0] - 1f);
brightnessDir[0] = -brightnessCycleSpeed;
if (brightnessValue[0] < 0f) brightnessValue[0] = 1f;
}
else if (brightnessValue[0] < 0f)
{
brightnessDir[0] = brightnessCycleSpeed;
brightnessValue[0] = -brightnessValue[0];
if (brightnessValue[0] > 1f) brightnessValue[0] = 0f;
}
for (int c = 0; c < numCritters; c++)
{
brightnessValue[c] = brightnessValue[0];
}
}
else
{
// to cycle brightness values
for (int c = 0; c < numCritters; c++)
{
//Console.WriteLine(brightnessValue[c].ToString() + " " + brightnessDir[c].ToString());
brightnessValue[c] += brightnessDir[c];
if (brightnessValue[c] > 1f)
{
brightnessValue[c] = 1f - (brightnessValue[c] - 1f);
brightnessDir[c] = -brightnessCycleSpeed;
if (brightnessValue[c] < 0f) brightnessValue[c] = 1f;
}
else if (brightnessValue[c] < 0f)
{
brightnessDir[c] = brightnessCycleSpeed;
brightnessValue[c] = -brightnessValue[c];
if (brightnessValue[c] > 1f) brightnessValue[c] = 0f;
}
}
}
}
else
{
for (int c = 0; c < numCritters; c++)
{
brightnessValue[c] = 1f;
}
}
if (thicknessMode)
{
if (thicknessSynch)
{
thicknessValue[0, 0] += thicknessDir[0];
if (thicknessValue[0, 0] > 10f)
{
thicknessValue[0, 0] = 10f - (thicknessValue[0, 0] - 10f);
thicknessDir[0] = -thicknessCycleSpeed;
if (thicknessValue[0, 0] < 0f) thicknessValue[0, 0] = 10f;
}
else if (thicknessValue[0, 0] < 0f)
{
thicknessDir[0] = thicknessCycleSpeed;
thicknessValue[0, 0] = -thicknessValue[0, 0];
if (thicknessValue[0, 0] > 10f) thicknessValue[0, 0] = 0f;
}
float t = thicknessValue[0, 0];
for (int c = 0; c < numCritters; c++)
{
thicknessValue[c, trailStart] = t;
}
}
else
{
// to cycle thickness values
for (int c = 0; c < numCritters; c++)
{
//Console.WriteLine(thicknessValue[c].ToString() + " " + thicknessDir[c].ToString());
thicknessValue[c, 0] += thicknessDir[c];
if (thicknessValue[c, 0] > 10f)
{
thicknessValue[c, 0] = 10f - (thicknessValue[c, 0] - 10f);
thicknessDir[c] = -thicknessCycleSpeed;
if (thicknessValue[c, 0] < 0f) thicknessValue[c, 0] = 10f;
}
else if (thicknessValue[c, 0] < 0f)
{
thicknessDir[c] = thicknessCycleSpeed;
thicknessValue[c, 0] = -thicknessValue[c, 0];
if (thicknessValue[c, 0] > 10f) thicknessValue[c, 0] = 0f;
}
thicknessValue[c, trailStart] = thicknessValue[c, 0];
}
}
}
else
{
for (int c = 0; c < numCritters; c++)
{
thicknessValue[c, trailStart] = lineWidth;
}
}
//if (thicknessMode || connectMode > 0)
//{
// //advance each trailing point
// for (int c = 0; c < numCritters; c++)
// {
// for (int p = trailLength; p > 0; p--)
// {
// bPos[c, p] = bPos[c, p - 1];
// bColor[c, p] = bColor[c, p - 1];
// thicknessValue[c, p] = thicknessValue[c, p - 1];
// }
// }
//}
//else
//{
// //advance each trailing point
// for (int c = 0; c < numCritters; c++)
// {
// for (int p = trailLength; p > 0; p--)
// {
// bPos[c, p] = bPos[c, p - 1];
// bColor[c, p] = bColor[c, p - 1];
// }
// }
//}
if (connectMode == 0)
{
//Console.WriteLine(bPos[0, trailStart].X.ToString() + ", " + bPos[0, trailStart].Y.ToString());
if (trailEnd > trailStart)
{
// draw each point's position
for (int c = 0; c < numCritters; c++)
{
GL.LineWidth(thicknessValue[c, trailStart]);
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.LineStrip);
for (int p = trailStart; p < trailEnd; p += trailSkip)
{
GL.Color3(bColor[c, p]);
GL.Vertex2(bPos[c, p]);
}
GL.End();
}
}
else//otherwise we need to wrap around
{
for (int c = 0; c < numCritters; c++)
{
GL.LineWidth(thicknessValue[c, trailStart]);
GL.Begin(OpenTK.Graphics.OpenGL.PrimitiveType.LineStrip);
for (int p = trailStart; p < 1601; p += trailSkip)
{
GL.Color3(bColor[c, p]);
GL.Vertex2(bPos[c, p]);
}
//now that we've passed the threshold, we need to wrap around accounting for trailskip size
int wrap = trailSkip - ((maxPoints - trailStart) % trailSkip);