-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysics.cs
520 lines (434 loc) · 15.1 KB
/
Physics.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace AI
{
class Physics
{
Env env;
F f;
AI ai;
PointF force_current;
PointF current_direction;
public float frictionPerTick, current_magnitude;
PointF INF_PointF;
Random rn;
//settings
float friction = 0.5f;
float being_feed_radius_coefficient = 0.8f;
public float being_feed_timer_start_s = 1f;
float current_magnitude_sd = 10f;
float current_direction_sd = 2f;
public float essence_movement_force_sd = 700f;
int essence_update_interval = 1;
public Physics(Env _env)
{
env = _env;
f = env.f;
ai = env.ai;
INF_PointF = new PointF(float.PositiveInfinity, float.PositiveInfinity);
rn = new Random((int)DateTime.Now.Ticks);
}
public void DragTo(Being being, Point p)
{
if (being == null)
{
return;
}
//reset everything
being.force = new PointF();
being.velocity = new PointF();
being.p = p;
CorrectDistance();
}
public void DragTo(Being being, Point p, float seconds)
{
if (being == null)
{
return;
}
//reset everything
being.force = new PointF();
being.velocity = new PointF();
PointF dir, dist, newP, vChange;
newP = p;
dir = f.Dir(being.p, p);
dist = dir;
//realise distance
being.p = newP;
//update velocity:
//change in v = d/t
vChange = f.Divide(dist, seconds);
being.velocity = f.Add(being.velocity, vChange);
being.velocity = f.Multiply(being.velocity, friction);
CorrectDistance();
}
public void DragTo(Essence essence, Point p)
{
if (essence == null)
{
return;
}
//reset everything
essence.force = new PointF();
essence.velocity = new PointF();
essence.p = p;
}
public void DragTo(Essence essence, Point p, float seconds)
{
if (essence == null)
{
return;
}
//reset everything
essence.force = new PointF();
essence.velocity = new PointF();
PointF dir, dist, newP, vChange;
newP = p;
dir = f.Dir(essence.p, p);
dist = dir;
//realise distance
essence.p = newP;
//update velocity:
//change in v = d/t
vChange = f.Divide(dist, seconds);
essence.velocity = f.Add(essence.velocity, vChange);
essence.velocity = f.Multiply(essence.velocity, friction);
CorrectDistance();
}
public void Apply()
{
frictionPerTick = 1f - (friction * env.timeInterval);
////ApplyCurrent();
//RandomEssenceMovement();
//RealiseForce();
////CheckBounds();
//CorrectDistance();
//DeleteOldEssence();
}
private void DeleteOldEssence()
{
if (env.counter % essence_update_interval * 10 == 0)
{
int c = ai.essences.Count;
if (c == 0)
{
return;
}
for (int i = 0; i < c; i++)
{
if (ai.essences[i].shell == null)
{
ai.essences[i].age += env.timeInterval;
if (ai.essences[i].age > env.ai.essence_age_max)
{
ai.RemoveEssence(ai.essences[i]);
return;
}
}
}
}
}
private void ApplyCurrent()
{
//update
PointF add;
add = f.NormDist_RandomPoint(new PointF(), current_direction_sd);
current_direction = f.Add(current_direction, add);
current_magnitude = current_magnitude + f.NormDist_RandomX(0, current_magnitude_sd);
force_current = f.Offset(current_direction, current_magnitude);
//apply
int c = ai.beings.Count;
for (int i = 0; i < c; i++)
{
ai.beings[i].force = f.Add(ai.beings[i].force, force_current);
}
c = ai.essences.Count;
for (int i = 0; i < c; i++)
{
ai.essences[i].force = f.Add(ai.essences[i].force, force_current);
}
c = ai.shells.Count;
for (int i = 0; i < c; i++)
{
ai.shells[i].force = f.Add(ai.shells[i].force, force_current);
}
}
public void EatAll(Being being)
{
if (being.timer_feed != 0)
{
return;
}
//check to see if anything is inside beings mouth position
PointF p;
int c, cc, ccc;
bool found;
p = f.Add(being.p, being.mouth_offset);
////beings
//c = ai.beings.Count;
//if (c != 0)
//{
// for (int i = 0; i < c; i++)
// {
// if (ai.beings[i] != being && f.Distance(ai.beings[i].p, p) <= being_feed_radius_coefficient * being.radius)
// {
// Eat(being, ai.beings[i]);
// }
// }
//}
//protein
try
{
IEnumerator<Essence> en = ai.essences.GetEnumerator();
Essence essence;
while (en.MoveNext())
{
essence = en.Current;
if (f.Distance(essence.p, being.p) <= being_feed_radius_coefficient * being.radius)
{
//chack that it is not one of own
cc = being.shells.Count;
found = false;
if (cc != 0)
{
for (int v = 0; v < cc; v++)
{
ccc = being.shells[v].essences.Count;
if (ccc != 0)
{
for (int b = 0; b < ccc; b++)
{
if (essence == being.shells[v].essences[b])
{
found = true;
break;
}
}
}
if (found)
{
break;
}
}
}
if (!found)
{
Eat(being, essence);
}
}
}
en.Dispose();
}
catch { }
}
private void Eat(Being being, Essence essence)
{
env.ai.AddEatenEssence(essence.p);
env.ai.AllocateEssenceToShell(being, essence);
being.feeding = false;
being.timer_feed = being_feed_timer_start_s;
being.unsucessful_feeds_short = 0;
being.unsucessful_feeds_long = 0;
env.vis.PlaySound_Being_Eat();
}
void RandomEssenceMovement()
{
if (env.counter % essence_update_interval == 0)
{
int c = ai.essences.Count;
for (int i = 0; i < c; i++)
{
if (ai.essences[i].shell == null)
{
ai.essences[i].force = f.NormDist_RandomPoint(new PointF(), essence_movement_force_sd);
ai.essences[i].force = f.Multiply(ai.essences[i].force, ai.essences[i].radius);
ai.essences[i].force = f.Multiply(ai.essences[i].force, ai.essences[i].radius);
ai.essences[i].velocity = f.Multiply(ai.essences[i].velocity, frictionPerTick);
}
ai.essences[i].age += env.timeInterval;
}
}
}
void RealiseForce()
{
int c = ai.beings.Count;
for (int i = 0; i < c; i++)
{
RealiseForce(ai.beings[i]);
}
if (env.counter % essence_update_interval == 0)
{
c = ai.essences.Count;
for (int i = 0; i < c; i++)
{
if (ai.essences[i].shell == null)
{
RealiseForce(ai.essences[i]);
}
}
}
c = ai.shells.Count;
for (int i = 0; i < c; i++)
{
RealiseForce(ai.shells[i]);
}
}
public void RealiseForce(Being being)
{
PointF acc, vChange, dist;
acc = f.Divide(being.force, being.mass);
vChange = f.Multiply(acc, env.timeInterval);
//realise velocity
being.velocity = f.Add(being.velocity, vChange);
//apply friction
being.velocity = f.Multiply(being.velocity, frictionPerTick);
//realise distance
dist = f.Multiply(being.velocity, env.timeInterval);
being.p = f.Add(being.p, dist);
//update mouth position
PointF dir;
if (being.velocity != INF_PointF && being.velocity != new PointF())
{
dir = being.velocity;
}
else
{
dir = new PointF(0, -1);
}
being.mouth_offset = f.Offset(dir, being.radius * 1.2f);
}
public void RealiseForce(Essence essence)
{
PointF acc, vChange, dist;
acc = f.Divide(essence.force, essence.mass);
vChange = f.Multiply(acc, env.timeInterval * (float)essence_update_interval);
//realise velocity
essence.velocity = f.Add(essence.velocity, vChange);
//apply friction
for (int i = 0; i < essence_update_interval; i++)
{
essence.velocity = f.Multiply(essence.velocity, frictionPerTick);
}
//realise distance
dist = f.Multiply(essence.velocity, env.timeInterval * (float)essence_update_interval);
essence.p = f.Add(essence.p, dist);
}
public void RealiseForce(Shell shell)
{
PointF acc, vChange, dist;
acc = f.Divide(shell.force, shell.mass);
vChange = f.Multiply(acc, env.timeInterval);
//realise velocity
shell.velocity = f.Add(shell.velocity, vChange);
//apply friction
shell.velocity = f.Multiply(shell.velocity, frictionPerTick);
//realise distance
dist = f.Multiply(shell.velocity, env.timeInterval);
shell.p = f.Add(shell.p, dist);
}
void CorrectDistance()
{
//correct protein distances of beings
int c, cc, ccc;
c = ai.beings.Count;
for (int i = 0; i < c; i++)
{
cc = ai.beings[i].shells.Count;
for (int v = 0; v < cc; v++)
{
CorrectDistance(ai.beings[i].shells[v]);
ccc = ai.beings[i].shells[v].essences.Count;
if (ccc != 0)
{
for (int b = 0; b < ccc; b++)
{
CorrectDistance(ai.beings[i].shells[v].essences[b]);
}
}
}
}
}
public void CorrectDistance(Shell shell)
{
PointF dir, dist, newP, vChange;
int index;
float friction;
float distance_multiplier = 1.7f;
float friction_power_multiplier = 3f;
float friction_index_sensitivity = 0.9f;
index = shell.being.shells.IndexOf(shell);
if (index == 0)
{
dir = f.Dir(shell.being.p, shell.p);
dir = f.Offset(dir, (shell.radius + shell.being.radius) * distance_multiplier);
newP = f.Add(shell.being.p, dir);
}
else
{
Shell prevShell = shell.being.shells[index - 1];
dir = f.Dir(prevShell.p, shell.p);
dir = f.Offset(dir, (shell.radius + prevShell.radius) * distance_multiplier);
newP = f.Add(prevShell.p, dir);
}
dist = f.Diff(shell.p, newP);
//realise distance
shell.p = newP;
friction = (float)Math.Pow(frictionPerTick, friction_power_multiplier * Math.Pow(index + 1, friction_index_sensitivity));
friction = f.NumberBound(friction, 0.5f);
//update velocity:
//change in v = d/t
vChange = f.Divide(dist, env.timeInterval);
shell.velocity = f.Add(shell.velocity, vChange);
shell.velocity = f.Multiply(shell.velocity, friction);
}
public void CorrectDistance(Essence essence)
{
if (essence.shell == null)
{
return;
}
essence.p = essence.shell.p;
}
void CheckBounds()
{
//check positions against bounds
int c = ai.beings.Count;
for (int i = 0; i < c; i++)
{
ai.beings[i].p = CheckBounds(ai.beings[i].p);
}
}
public PointF CheckBounds(PointF p)
{
//PointF topBound, lowBound;
//topBound = env.vis.e.ClipRectangle.Location;
//lowBound = new PointF(topBound.X + env.vis.e.ClipRectangle.Width, topBound.Y + env.vis.e.ClipRectangle.Height);
////lowBound.Y -= 20;
////vertical
////ceiling
//if (p.Y < topBound.Y)
//{
// p.Y = topBound.Y;
//}
////floor
//else if (p.Y > lowBound.Y)
//{
// p.Y = lowBound.Y;
//}
////horizontal
////left
//if (p.X < topBound.X)
//{
// p.X = topBound.X;
//}
////right
//else if (p.X > lowBound.X)
//{
// p.X = lowBound.X;
//}
return p;
}
}
}