-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawBall.cpp
More file actions
484 lines (431 loc) · 17.2 KB
/
Copy pathDrawBall.cpp
File metadata and controls
484 lines (431 loc) · 17.2 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
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
#include "DrawBall.h"
#include <glm/gtc/type_ptr.hpp>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <algorithm>
DrawBall::DrawBall(GLuint vao, int vc, float radius)
: VAO(vao), vertexCount(vc),
position(0.0f), velocity(0.0f), acceleration(0.0f),
scale(radius), gravity(-9.8f),
color(0.93f, 0.16f, 0.16f),
isPredator(false),
isStationary(false),
score(0),
point(0),
currentState(FSMState::SelectTarget),
targetPrey(nullptr),
lastTargetSelectionTime(0.0f),
predatorSpeed(5.0f) {
this->boundingBox.radius = radius;
this->boundingBox.center = position;
UpdateBoundingSphere();
}
DrawBall::~DrawBall() {}
void DrawBall::UpdateBoundingSphere() {
this->boundingBox.SetCenter(position);
}
void DrawBall::Update(float deltaTime, const AABB& roomAABB) {
if (isStationary) {
return;
}
velocity.y += gravity * deltaTime;
position += velocity * deltaTime;
glm::vec3 roomMin = roomAABB.GetMin();
glm::vec3 roomMax = roomAABB.GetMax();
if (position.y - scale < roomMin.y) {
position.y = roomMin.y + scale;
velocity.y *= -1.0f;
}
if (position.x - scale < roomMin.x || position.x + scale > roomMax.x) {
velocity.x *= -1.0f;
position.x = glm::clamp(position.x, roomMin.x + scale, roomMax.x - scale);
}
if (position.z - scale < roomMin.z || position.z + scale > roomMax.z) {
velocity.z *= -1.0f;
position.z = glm::clamp(position.z, roomMin.z + scale, roomMax.z - scale);
}
UpdateBoundingSphere();
}
void DrawBall::Update(float deltaTime, const AABB& roomAABB, const std::vector<DrawBall*>& balls) {
if (isStationary) {
return;
}
if (isPredator) {
glm::vec3 color = GetColor();
bool isGrayPredator = (color.r > 0.4f && color.g > 0.4f && color.b > 0.4f);
if (isGrayPredator) {
UpdateFSM(deltaTime, balls);
} else {
UpdateFuzzyLogic(deltaTime, balls);
}
} else {
glm::vec3 avoidanceForce(0.0f);
float avoidanceRadius = 2.0f;
for (const auto& predator : balls) {
if (predator->IsPredator()) {
glm::vec3 toPredator = predator->GetPosition() - position;
float distance = glm::length(toPredator);
if (distance < avoidanceRadius && distance > 0.001f) {
glm::vec3 avoidDirection = -glm::normalize(toPredator);
avoidDirection.y = 0.0f;
avoidDirection = glm::normalize(avoidDirection);
float avoidStrength = (avoidanceRadius - distance) / avoidanceRadius;
avoidanceForce += avoidDirection * avoidStrength * 3.0f;
}
}
}
float baseSpeed = 0.0f;
if (point == 15) baseSpeed = 4.0f;
else if (point == 10) baseSpeed = 3.0f;
else if (point == 5) baseSpeed = 2.0f;
if (glm::length(avoidanceForce) > 0.001f) {
velocity.x = glm::clamp(velocity.x + avoidanceForce.x * deltaTime, -baseSpeed, baseSpeed);
velocity.z = glm::clamp(velocity.z + avoidanceForce.z * deltaTime, -baseSpeed, baseSpeed);
} else {
velocity.x = glm::clamp(velocity.x, -baseSpeed, baseSpeed);
velocity.z = glm::clamp(velocity.z, -baseSpeed, baseSpeed);
}
}
velocity.y += gravity * deltaTime;
position += velocity * deltaTime;
glm::vec3 roomMin = roomAABB.GetMin();
glm::vec3 roomMax = roomAABB.GetMax();
if (position.y - scale < roomMin.y) {
position.y = roomMin.y + scale;
velocity.y *= -1.0f;
}
if (position.x - scale < roomMin.x || position.x + scale > roomMax.x) {
velocity.x *= -1.0f;
position.x = glm::clamp(position.x, roomMin.x + scale, roomMax.x - scale);
}
if (position.z - scale < roomMin.z || position.z + scale > roomMax.z) {
velocity.z *= -1.0f;
position.z = glm::clamp(position.z, roomMin.z + scale, roomMax.z - scale);
}
UpdateBoundingSphere();
}
// FSM AI Engine for Gray Predator
void DrawBall::UpdateFSM(float deltaTime, const std::vector<DrawBall*>& preys) {
lastTargetSelectionTime += deltaTime;
switch (currentState) {
case FSMState::SelectTarget: {
// Select target every 0.5 seconds or if no target
if (targetPrey == nullptr || lastTargetSelectionTime > 0.5f) {
targetPrey = SelectTargetFSM(preys);
lastTargetSelectionTime = 0.0f;
if (targetPrey != nullptr) {
currentState = FSMState::ChaseTarget;
}
}
break;
}
case FSMState::ChaseTarget: {
if (targetPrey == nullptr) {
// Target is gone (eaten by other predator), return to select
currentState = FSMState::SelectTarget;
velocity.x = 0.0f;
velocity.z = 0.0f;
} else {
// Check if target still exists in prey list
bool targetExists = false;
for (auto prey : preys) {
if (prey == targetPrey) {
targetExists = true;
break;
}
}
if (!targetExists) {
// Target was eaten, return to select immediately
targetPrey = nullptr;
currentState = FSMState::SelectTarget;
lastTargetSelectionTime = 0.5f; // Force immediate selection
velocity.x = 0.0f;
velocity.z = 0.0f;
} else {
// Check if target is too far (invalid)
float distance = glm::length(targetPrey->GetPosition() - position);
if (distance > 8.0f) {
// Target too far, select new target
targetPrey = nullptr;
currentState = FSMState::SelectTarget;
velocity.x = 0.0f;
velocity.z = 0.0f;
} else {
// Chase the target
ChaseTarget(deltaTime);
}
}
}
break;
}
}
}
// FSM Target Selection: Choose highest value prey within shortest distance
DrawBall* DrawBall::SelectTargetFSM(const std::vector<DrawBall*>& preys) {
DrawBall* bestTarget = nullptr;
float bestScore = -1.0f;
for (auto prey : preys) {
if (prey->IsPredator()) continue; // Skip other predators
float distance = glm::length(prey->GetPosition() - position);
if (distance > 10.0f) continue; // Only consider nearby preys
// FSM Logic: Prioritize high value prey with short distance
// Score = Value / Distance (higher is better)
float score = static_cast<float>(prey->GetPoint()) / (distance + 0.1f);
if (score > bestScore) {
bestScore = score;
bestTarget = prey;
}
}
return bestTarget;
}
// Fuzzy Logic AI Engine for Purple Predator
void DrawBall::UpdateFuzzyLogic(float deltaTime, const std::vector<DrawBall*>& preys) {
lastTargetSelectionTime += deltaTime;
// Select target every 1.0 seconds using fuzzy logic
if (targetPrey == nullptr || lastTargetSelectionTime > 1.0f) {
targetPrey = SelectTargetFuzzy(preys);
lastTargetSelectionTime = 0.0f;
}
if (targetPrey != nullptr) {
// Check if target still exists in prey list
bool targetExists = false;
for (auto prey : preys) {
if (prey == targetPrey) {
targetExists = true;
break;
}
}
if (!targetExists) {
// Target was eaten, force immediate reselection
targetPrey = nullptr;
lastTargetSelectionTime = 1.0f; // Force immediate selection
velocity.x = 0.0f;
velocity.z = 0.0f;
} else {
// Check if target is still valid
float distance = glm::length(targetPrey->GetPosition() - position);
if (distance > 8.0f) {
targetPrey = nullptr; // Target too far
velocity.x = 0.0f;
velocity.z = 0.0f;
} else {
ChaseTarget(deltaTime);
}
}
} else {
velocity.x = 0.0f;
velocity.z = 0.0f;
}
}
// Fuzzy Logic Target Selection
DrawBall* DrawBall::SelectTargetFuzzy(const std::vector<DrawBall*>& preys) {
DrawBall* bestTarget = nullptr;
float bestPriority = 0.0f;
for (auto prey : preys) {
if (prey->IsPredator()) continue; // Skip other predators
float distance = glm::length(prey->GetPosition() - position);
if (distance > 7.0f) continue; // Only consider reachable preys
FuzzyInput input;
input.distance = distance;
input.preyValue = prey->GetPoint();
float priority = CalculateFuzzyPriority(input);
if (priority > bestPriority) {
bestPriority = priority;
bestTarget = prey;
}
}
return bestTarget;
}
// Chase Target Implementation
void DrawBall::ChaseTarget(float deltaTime) {
if (targetPrey == nullptr) return;
glm::vec3 direction = targetPrey->GetPosition() - position;
direction.y = 0.0f; // Only move horizontally
if (glm::length(direction) > 0.001f) {
direction = glm::normalize(direction);
velocity.x = direction.x * predatorSpeed;
velocity.z = direction.z * predatorSpeed;
}
}
// Fuzzy Logic Implementation
float DrawBall::CalculateFuzzyPriority(const FuzzyInput& input) {
// Fuzzy Rules:
// 1. If distance is Close and value is High, then priority is VeryHigh
// 2. If distance is Close and value is Medium, then priority is High
// 3. If distance is Medium and value is High, then priority is High
// 4. If distance is Far and value is High, then priority is Medium
// 5. If distance is Close and value is Low, then priority is Medium
// 6. If distance is Medium and value is Medium, then priority is Medium
// 7. If distance is Medium and value is Low, then priority is Low
// 8. If distance is Far and value is Medium, then priority is Low
// 9. If distance is Far and value is Low, then priority is VeryLow
float totalWeight = 0.0f;
float weightedSum = 0.0f;
// Rule 1: Close + High -> VeryHigh
float close = GetDistanceMembership(input.distance, "Close");
float high = GetValueMembership(input.preyValue, "High");
float weight1 = close * high;
if (weight1 > 0.0f) {
totalWeight += weight1;
weightedSum += weight1 * 0.9f; // VeryHigh = 0.9
}
// Rule 2: Close + Medium -> High
float medium_val = GetValueMembership(input.preyValue, "Medium");
float weight2 = close * medium_val;
if (weight2 > 0.0f) {
totalWeight += weight2;
weightedSum += weight2 * 0.7f; // High = 0.7
}
// Rule 3: Medium + High -> High
float medium_dist = GetDistanceMembership(input.distance, "Medium");
float weight3 = medium_dist * high;
if (weight3 > 0.0f) {
totalWeight += weight3;
weightedSum += weight3 * 0.7f; // High = 0.7
}
// Rule 4: Far + High -> Medium
float far = GetDistanceMembership(input.distance, "Far");
float weight4 = far * high;
if (weight4 > 0.0f) {
totalWeight += weight4;
weightedSum += weight4 * 0.5f; // Medium = 0.5
}
// Rule 5: Close + Low -> Medium
float low_val = GetValueMembership(input.preyValue, "Low");
float weight5 = close * low_val;
if (weight5 > 0.0f) {
totalWeight += weight5;
weightedSum += weight5 * 0.5f; // Medium = 0.5
}
// Rule 6: Medium + Medium -> Medium
float weight6 = medium_dist * medium_val;
if (weight6 > 0.0f) {
totalWeight += weight6;
weightedSum += weight6 * 0.5f; // Medium = 0.5
}
// Rule 7: Medium + Low -> Low
float weight7 = medium_dist * low_val;
if (weight7 > 0.0f) {
totalWeight += weight7;
weightedSum += weight7 * 0.3f; // Low = 0.3
}
// Rule 8: Far + Medium -> Low
float weight8 = far * medium_val;
if (weight8 > 0.0f) {
totalWeight += weight8;
weightedSum += weight8 * 0.3f; // Low = 0.3
}
// Rule 9: Far + Low -> VeryLow
float weight9 = far * low_val;
if (weight9 > 0.0f) {
totalWeight += weight9;
weightedSum += weight9 * 0.1f; // VeryLow = 0.1
}
// Defuzzification using weighted average
if (totalWeight > 0.0f) {
return weightedSum / totalWeight;
}
return 0.0f; // No applicable rules
}
// Distance Membership Functions
float DrawBall::GetDistanceMembership(float distance, const std::string& category) {
if (category == "Close") {
// Close: peak at 0, zero at 3
if (distance <= 0.5f) return 1.0f;
if (distance >= 3.0f) return 0.0f;
return (3.0f - distance) / 2.5f;
}
else if (category == "Medium") {
// Medium: peak at 3.5, zero at 1 and 6
if (distance <= 1.0f || distance >= 6.0f) return 0.0f;
if (distance <= 3.5f) return (distance - 1.0f) / 2.5f;
return (6.0f - distance) / 2.5f;
}
else if (category == "Far") {
// Far: peak at 7, zero at 4
if (distance <= 4.0f) return 0.0f;
if (distance >= 7.0f) return 1.0f;
return (distance - 4.0f) / 3.0f;
}
return 0.0f;
}
// Value Membership Functions
float DrawBall::GetValueMembership(int value, const std::string& category) {
if (category == "Low") {
// Low: peak at 5, zero at 8
if (value == 5) return 1.0f;
if (value >= 10) return 0.0f;
if (value < 5) return 1.0f;
return (10.0f - value) / 5.0f;
}
else if (category == "Medium") {
// Medium: peak at 10, zero at 5 and 15
if (value == 10) return 1.0f;
if (value <= 5 || value >= 15) return 0.0f;
if (value < 10) return (value - 5.0f) / 5.0f;
return (15.0f - value) / 5.0f;
}
else if (category == "High") {
// High: peak at 15, zero at 10
if (value == 15) return 1.0f;
if (value <= 10) return 0.0f;
if (value > 15) return 1.0f;
return (value - 10.0f) / 5.0f;
}
return 0.0f;
}
// Priority Membership Functions (for output)
float DrawBall::GetPriorityMembership(float priority, const std::string& category) {
if (category == "VeryLow") {
return (priority <= 0.2f) ? 1.0f : std::max(0.0f, (0.3f - priority) / 0.1f);
}
else if (category == "Low") {
if (priority <= 0.2f || priority >= 0.4f) return 0.0f;
if (priority <= 0.3f) return (priority - 0.2f) / 0.1f;
return (0.4f - priority) / 0.1f;
}
else if (category == "Medium") {
if (priority <= 0.4f || priority >= 0.6f) return 0.0f;
if (priority <= 0.5f) return (priority - 0.4f) / 0.1f;
return (0.6f - priority) / 0.1f;
}
else if (category == "High") {
if (priority <= 0.6f || priority >= 0.8f) return 0.0f;
if (priority <= 0.7f) return (priority - 0.6f) / 0.1f;
return (0.8f - priority) / 0.1f;
}
else if (category == "VeryHigh") {
return (priority >= 0.8f) ? 1.0f : std::max(0.0f, (priority - 0.7f) / 0.1f);
}
return 0.0f;
}
// Reset AI state for predators
void DrawBall::ResetAIState() {
currentState = FSMState::SelectTarget;
targetPrey = nullptr;
lastTargetSelectionTime = 0.0f;
}
void DrawBall::Render(Shader* shader, const glm::mat4& view, const glm::mat4& proj, const glm::vec3& cameraPos) {
glm::mat4 modelMat = glm::mat4(1.0f);
modelMat = glm::translate(modelMat, position);
modelMat = glm::scale(modelMat, glm::vec3(scale));
shader->use();
glUniform1i(glGetUniformLocation(shader->ID, "isbox"), 0);
glUniform1i(glGetUniformLocation(shader->ID, "isRoom"), 0);
glUniformMatrix4fv(glGetUniformLocation(shader->ID, "modelMat"), 1, GL_FALSE, glm::value_ptr(modelMat));
glUniformMatrix4fv(glGetUniformLocation(shader->ID, "viewMat"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader->ID, "projMat"), 1, GL_FALSE, glm::value_ptr(proj));
glUniform3f(glGetUniformLocation(shader->ID, "objColor"), color.x, color.y, color.z);
glUniform3f(glGetUniformLocation(shader->ID, "ambientColor"), 0.3f, 0.3f, 0.3f);
glUniform3f(glGetUniformLocation(shader->ID, "lightPos"), 2.0f, 4.0f, 2.0f);
glUniform3f(glGetUniformLocation(shader->ID, "lightColor"), 0.8f, 0.8f, 0.8f);
glUniform3f(glGetUniformLocation(shader->ID, "lightPos2"), -2.0f, 4.0f, -2.0f);
glUniform3f(glGetUniformLocation(shader->ID, "lightColor2"), 0.6f, 0.6f, 0.6f);
glUniform3f(glGetUniformLocation(shader->ID, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z);
glUniform1i(glGetUniformLocation(shader->ID, "light1Enabled"), light1Enabled);
glUniform1i(glGetUniformLocation(shader->ID, "light2Enabled"), light2Enabled);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, vertexCount);
}