Skip to content

Commit def6512

Browse files
committed
perf(flocking): add better arrows
1 parent ac1365a commit def6512

1 file changed

Lines changed: 33 additions & 32 deletions

File tree

  • courses/artificialintelligence/assignments/flocking

courses/artificialintelligence/assignments/flocking/README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Flocking is a behavior that is observed in birds, fish and other animals that mo
1010

1111
- \( \vec{F} \) means a vector \( F \) that has components. In a 2 dimensional vector it will hold \( F_x \) and \( F_y \). For example, if \( F_x = 1 \) and \( F_y = 3 \), then \( \vec{F} = (1,3) \)
1212
- Simple math operations between vectors are done component-wise. For example, if \( \vec{F} = (1,1) \) and \( \vec{G} = (2,2) \), then \( \vec{F} + \vec{G} = (3,3) \)
13-
- The notation \( \vec{P_{1}P_{2}} \) means the vector that goes from \( P_1 \) to \( P_2 \). It is the same as \( P_2-P_1 \)
13+
- The notation \( \overrightarrow{P_{1}P_{2}} \) means the vector that goes from \( P_1 \) to \( P_2 \). It is the same as \( P_2-P_1 \)
1414
- The modulus notation means the length (magnitude) of the vector. \( |\vec{F}| = \sqrt{F_x^2+F_y^2} \) For example, if \( \vec{F} = (1,1) \), then \( |\vec{F}| = \sqrt{2} \)
1515
- The hat ^ notation means the normalized vector(magnitude is 1) of the vector. \( \hat{F} = \frac{\vec{F}}{|\vec{F}|} \) For example, if \( \vec{F} = (1,1) \), then \( \hat{F} = (\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}) \)
16-
- The hat notation over 2 points means the normalized vector that goes from the first point to the second point. \( \hat{P_1P_2} = \frac{\vec{P_1P_2}}{|\vec{P_1P_2}|} \) For example, if \( P_1 = (0,0) \) and \( P_2 = (1,1) \), then \( \widehat{P_1P_2} = (\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}) \)
16+
- The hat notation over 2 points means the normalized vector that goes from the first point to the second point. \( \widehat{P_1P_2} = \frac{\overrightarrow{P_1P_2}}{|\overrightarrow{P_1P_2}|} \) For example, if \( P_1 = (0,0) \) and \( P_2 = (1,1) \), then \( \widehat{P_1P_2} = (\frac{1}{\sqrt{2}},\frac{1}{\sqrt{2}}) \)
1717
- The sum \( \sum \) notation means the sum of all elements in the list going from `0` to `n-1`. Ex. \( \sum_{i=0}^{n-1} \vec{V_i} = \vec{V_0} + \vec{V_1} + \vec{V_2} + ... + \vec{V_{n-1}} \)
1818

1919
It is your job to implement those 3 behaviors following the ruleset below:
@@ -22,9 +22,9 @@ It is your job to implement those 3 behaviors following the ruleset below:
2222

2323
Apply a force towards the center of mass of the group.
2424

25-
1. The \( n \) neighbors of an agent are all the other agents that are within a certain radius of the agent. It doesnt include the agent itself;
25+
1. The \( n \) neighbors of an agent are all the other agents that are within a certain radius \( r_c \)( `<` operation ) of the agent. It doesn't include the agent itself;
2626
2. Compute the location of the center of mass of the group (\( P_{CM} \));
27-
3. Compute the force that will move the agent towards the center of mass(\( \vec{F_{c}} \)); The farther the agent is from the center of mass, the force increases linearly up to the limit of the cohesion radius \( r_c \).
27+
3. Compute the force that will move the agent towards the center of mass(\( \overrightarrow{F_c} \)); The farther the agent is from the center of mass, the force increases linearly up to the limit of the cohesion radius \( r_c \).
2828

2929
![cohesion](cohesion.png)
3030

@@ -33,15 +33,15 @@ P_{CM} = \frac{\sum_{i=0}^{n-1} P_i}{n}
3333
\]
3434

3535
\[
36-
F_c = \begin{cases}
37-
\frac{ \vec{P_{a}P_{CM}} }{r_c} & \text{if } |\vec{P_{a}P_{CM}}| \leq r_c \\
38-
0 & \text{if } |\vec{P_{a}P_{CM}}| > r_c
36+
\overrightarrow{F_{c}} = \begin{cases}
37+
\frac{ \overrightarrow{P_{agent}P_{CM}} }{r_c} & \text{if } |\overrightarrow{P_{agent}P_{CM}}| \leq r_c \\
38+
0 & \text{if } |\overrightarrow{P_{agent}P_{CM}}| > r_c
3939
\end{cases}
4040
\]
4141

4242
!!! tip
4343

44-
Note that the maximum magnitude of \( F_c \) is 1. Inclusive. This value can be multiplied by a constant \( K_c \) to increase or decrease the cohesion force to looks more appealing.
44+
Note that the maximum magnitude of \( \overrightarrow{F_c} \) is 1. Inclusive. This value can be multiplied by a constant \( K_c \) to increase or decrease the cohesion force to looks more appealing.
4545

4646
??? example "Cohesion Example"
4747

@@ -51,16 +51,16 @@ F_c = \begin{cases}
5151

5252
It will move the agent away from other agents when they get too close.
5353

54-
1. The \( n \) neighbors of an agent are all the other agents that are within the separation radius of the agent;
54+
1. The \( n \) neighbors of an agent are all the other agents that are within the separation radius \( r_s \) of the agent;
5555
2. If the distance to a neighbor is less than the separation radius, then the agent will move away from it inversely proportionally to the distance between them.
56-
3. Accumulate the forces that will move the agent away from each neighbor (\( \vec{F_{s}} \)). And then, clamp the force to a maximum value of \( F_{Smax} \).
56+
3. Accumulate the forces that will move the agent away from each neighbor (\( \overrightarrow{F_{s}} \)). And then, clamp the force to a maximum value of \( F_{Smax} \).
5757

5858
![separation](separation.png)
5959

6060
\[
61-
\vec{F_s} = \sum_{i=0}^{n-1} \begin{cases}
62-
\frac{\widehat{AN_i}}{|\vec{AN_i}|} & \text{if } 0 < |\vec{AN_i}| \leq r_s \\
63-
0 & \text{if } |\vec{AN_i}| = 0 \lor |\vec{AN_i}| > r_s
61+
\overrightarrow{F_s} = \sum_{i=0}^{n-1} \begin{cases}
62+
\frac{\widehat{P_aP_i}}{|\overrightarrow{P_aP_i}|} & \text{if } 0 < |\overrightarrow{P_aP_i}| \leq r_s \\
63+
0 & \text{if } |\overrightarrow{P_aP_i}| = 0 \lor |\overrightarrow{P_aP_i}| > r_s
6464
\end{cases}
6565
\]
6666

@@ -71,9 +71,9 @@ It will move the agent away from other agents when they get too close.
7171
The force will go near infinite when the distance between the agent and the \( n \) neighbor is 0. To avoid this, after accumulating all the influences from every neighbor, the force will be clamped to a maximum magnitude of \( F_{Smax} \).
7272

7373
\[
74-
\vec{F_{s}} = \begin{cases}
75-
\vec{F_s} & \text{if } |\vec{F_s}| \leq F_{Smax} \\
76-
\widehat{F_s} \cdot F_{Smax} & \text{if } |\vec{F_s}| > F_{Smax}
74+
\overrightarrow{F_{s}} = \begin{cases}
75+
\overrightarrow{F_s} & \text{if } |\overrightarrow{F_s}| \leq F_{Smax} \\
76+
\widehat{F_s} \cdot F_{Smax} & \text{if } |\overrightarrow{F_s}| > F_{Smax}
7777
\end{cases}
7878
\]
7979

@@ -90,14 +90,14 @@ The force will go near infinite when the distance between the agent and the \( n
9090

9191
It is the force that will align the velocity of the agent with the average velocity of the group.
9292

93-
1. The \( n \) neighbors of an agent are all the agents that are within the alignment radius of the agent, including itself;
94-
2. Compute the average velocity of the group (\( \vec{V_{avg}} \));
95-
3. Compute the force that will move the agent towards the average velocity (\( \vec{F_{a}} \));
93+
1. The \( n \) neighbors of an agent are all the agents that are within the alignment radius \( r_a \) of the agent, including itself;
94+
2. Compute the average velocity of the group (\( \overrightarrow{V_{avg}} \));
95+
3. Compute the force that will move the agent towards the average velocity (\( \overrightarrow{F_{a}} \));
9696

9797
![alignment](alignment.png)
9898

9999
\[
100-
\vec{V_{avg}} = \frac{\sum_{i=0}^{n-1} \vec{V_i}}{n}
100+
\overrightarrow{V_{avg}} = \frac{\sum_{i=0}^{n-1} \vec{V_i}}{n}
101101
\]
102102

103103
??? example "Alignment Example"
@@ -108,28 +108,28 @@ It is the force that will align the velocity of the agent with the average veloc
108108

109109
The force composition is made by a weighted sum of the influences of those 3 behaviors. This is the way we are going to work, this is not the only way to do it, nor the more correct. It is just a way to do it.
110110

111-
- \( \vec{F} = K_c \cdot \vec{F_c} + K_s \cdot \vec{F_s} + K_a \cdot \vec{F_a} \) `This is a weighted sum!`
112-
- \( \vec{V_{new}} = \vec{V_{cur}} + \vec{F} \cdot \Delta t \) `This is a simplification!`
113-
- \( P_{new} = P_{cur}+\vec{V_{new}} \cdot \Delta t \) `This is an approximation!`
111+
- \( \vec{F} = K_c \cdot \overrightarrow{F_c} + K_s \cdot \overrightarrow{F_s} + K_a \cdot \overrightarrow{F_a} \) `This is a weighted sum!`
112+
- \( \overrightarrow{V_{new}} = \overrightarrow{V_{cur}} + \vec{F} \cdot \Delta t \) `This is a simplification!`
113+
- \( P_{new} = P_{cur}+\overrightarrow{V_{new}} \cdot \Delta t \) `This is an approximation!`
114114

115115
!!! warning
116116

117117
A more precise way for representing the new position would be to use full equations of motion. But given timestep is usually very small and it even squared, it is acceptable to ignore it. But here they are anyway, just dont use them in this assignment:
118118

119-
- \( \vec{V_{new}} = \vec{V_{cur}}+\frac{\vec{F}}{m} \cdot \Delta t \)
120-
- \( P_{new} = P_{cur}+\vec{V_{cur}} \cdot \Delta t + \frac{\vec{F}}{m} \cdot \frac{\Delta t^2}{2} \)
119+
- \( \overrightarrow{V_{new}} = \overrightarrow{V_{cur}}+\frac{\overrightarrow{F}}{m} \cdot \Delta t \)
120+
- \( P_{new} = P_{cur}+\overrightarrow{V_{cur}} \cdot \Delta t + \frac{\vec{F}}{m} \cdot \frac{\Delta t^2}{2} \)
121121

122122
Where:
123123

124-
- \( \vec{F} \) is the force applied to the agent;
125-
- \( \vec{V} \) is the velocity of the agent;
124+
- \( \overrightarrow{F} \) is the force applied to the agent;
125+
- \( \overrightarrow{V} \) is the velocity of the agent;
126126
- \( P \) is the position of the agent;
127127
- \( m \) is the mass of the agent, here it is always 1;
128128
- \( \Delta t \) is the time frame (1/FPS);
129129
- \( cur \) is the current value of the variable;
130130
- \( new \) is the new value of the variable to be used in the next frame.
131131

132-
The \( \vec{V_{new}} \) and \( P_{new} \) are the ones that will be used in the next frame and you will have to print to the console at the end of every single frame.
132+
The \( \overrightarrow{V_{new}} \) and \( P_{new} \) are the ones that will be used in the next frame and you will have to print to the console at the end of every single frame.
133133

134134
!!! note
135135

@@ -202,7 +202,8 @@ The expected output is the position and velocity for each agent after the simula
202202

203203
## Grading
204204

205-
20 points total:
206-
- 5 Points – by following standards;
207-
- 5 Points – properly submitted in Canvas;
208-
- 10 Points – passed on test cases;
205+
10 points total:
206+
207+
- 3 Points – by following standards;
208+
- 2 Points – properly submitted in Canvas;
209+
- 5 Points – passed on test cases;

0 commit comments

Comments
 (0)