-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNA_Boid.cpp
192 lines (144 loc) · 4.29 KB
/
NA_Boid.cpp
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
#include <iostream>
using std::cout;
#include "globals.h"
#include "cRenderClass.h"
#include <vector>
using std::vector;
#include "NA_Boid.h"
#include "NA_MathsLib.h"
#include "NA_Matrix.h"
NA_Boid::NA_Boid()
{
}
void NA_Boid::update()
{
extern vector<NA_Boid> boidList;
extern NA_MathsLib na_maths;
extern cRenderClass graphics;
//TODO: find nearby boids and only consider them
//alignment - align self to average heading
//calc sum velocity
NA_Vector sumVelocity;
for (int i = 0; i < BOID_MAX; i++)
{
sumVelocity.x += boidList[i].currentVelocity.x;
sumVelocity.y += boidList[i].currentVelocity.y;
}
//convert to average
sumVelocity.x = sumVelocity.x / (BOID_MAX);
sumVelocity.y = sumVelocity.y / (BOID_MAX);
//cout << "average vel: X: " << sumVelocity.x << " Y:" << sumVelocity.y << "\n";
newVelocity = sumVelocity;
//cohesion - move towards average position
//calc sum position
NA_Vector sumPosition;
for (int i = 0; i < BOID_MAX; i++)
{
sumPosition.x += boidList[i].position.x;
sumPosition.y += boidList[i].position.y;
}
//convert to average
sumPosition.x = sumPosition.x / (BOID_MAX);
sumPosition.y = sumPosition.y / (BOID_MAX);
//TODO: if i'm close already maybe i should go slower
NA_Vector temp = NA_Vector::twoPointsIntoVector(position, sumPosition); //modify velocity to head towards the average position
temp.scale(BOID_COHESION_WEIGHTING);
newVelocity.x += temp.x;
newVelocity.y += temp.y;
//separation
for (int i = 0; i < BOID_MAX; i++)
{
if (&boidList[i] != this) //ignore self
{
NA_Vector d = NA_Vector::twoPointsIntoVector(boidList[i].position, position);
if (d.length() < BOID_RESPECT_DIST)
{
newVelocity = d; //TODO: what if near multiple boids?
}
}
}
if (graphics.mouseIsScary)
{
NA_Vector d = NA_Vector::twoPointsIntoVector(graphics.mousePos, position);
//cout << "mouse dist: " << d.length() << "\n";
if (d.length() < BOID_MOUSE_FEAR)
{
//cout << "AHHH A MOUSE!!!\n";
newVelocity = d;
}
}
}
void NA_Boid::postUpdate()
{
//enforce rotation limit
if (newVelocity.clockwiseAngle(currentVelocity) > BOID_ROTATE_MAX && currentVelocity.clockwiseAngle(newVelocity) > BOID_ROTATE_MAX)
{
if (newVelocity.clockwiseAngle(currentVelocity) < currentVelocity.clockwiseAngle(newVelocity))//clockwise or counterclockwise?
{
NA_Matrix r = NA_Matrix(NA_Matrix::types::rotateZ, BOID_ROTATE_MAX);
newVelocity = r.matrixXvector(newVelocity);
}
else
{
NA_Matrix r = NA_Matrix(NA_Matrix::types::rotateZ, -BOID_ROTATE_MAX);
newVelocity = r.matrixXvector(newVelocity);
}
}
//boids should not break the speed limit
if (newVelocity.length() > BOID_SPEED_MAX)
newVelocity.normalise();
newVelocity.scale(BOID_SPEED_MAX);
//if (newVelocity.length() > BOID_SPEED_MAX) cout << "speed limit is poorly enforced\n";
currentVelocity = newVelocity; //TODO: Acceleration limit?
newVelocity = NA_Vector();//prepare vector for next update
//move
position.x += currentVelocity.x;
position.y += currentVelocity.y;
//screen wrap
if (position.x < 0)
position.x += SCREEN_WIDTH;
if (position.x > SCREEN_WIDTH)
position.x -= SCREEN_WIDTH;
if (position.y < 0)
position.y += SCREEN_HEIGHT;
if (position.y > SCREEN_HEIGHT)
position.y -= SCREEN_HEIGHT;
}
/*NA_Vector NA_Boid::getVelocity()
{
NA_Vector temp(this->currentVelocity);
return temp;
}
NA_Vector NA_Boid::getposition()
{
NA_Vector temp(this->position);
return temp;
}*/
void NA_Boid::draw()
{
extern cRenderClass graphics;
short int bodyColourR, bodyColourG, bodyColourB;
bodyColourR = 1;
bodyColourG = 1;
bodyColourB = 1;
if (DEBUG_HIGHLIGHT_FIRST_BOID)
{
extern vector<NA_Boid> boidList;
if (this == &boidList[0])
{
bodyColourR = 0;
bodyColourG = 1;
bodyColourB = 0;
}
}
//draw body
graphics.setColour(bodyColourR, bodyColourG, bodyColourB);
graphics.setPointSize(3);
graphics.drawPixel(position.x,position.y);
//draw 'nose'
graphics.setColour(1, 0, 0);
graphics.setPointSize(1);
NA_Vector noseOffset = currentVelocity;
noseOffset.normalise();
graphics.drawPixel(position.x+(noseOffset.x*5.0f), position.y+(noseOffset.y*5.0f));
}