此篇为学习《代码本色》笔记
自治智能体简介
建立流场模型:
将Processing窗口当作一个网格,每个单元格都有一个向量指向特定方向,这样的模型就是流场。
使用noise模拟一个较平滑3D随机流场,其中,流场内向量随着时间发生变化。
void update() {
float xoff = 0;
for (int i = 0; i < cols; i++) {
float yoff = 0;
for (int j = 0; j < rows; j++) {
float theta = map(noise(xoff,yoff,zoff),0,1,0,TWO_PI);
field[i][j] = PVector.fromAngle(theta);
yoff += 0.1;
}
xoff += 0.1;
}
// 每帧改变随机
zoff += 0.01;
}
按下鼠标,流场变化(通过改变伪随机的种子参数)。
void mousePressed() {
noiseSeed((int)random(10000));
}
练习完整代码:
// Using this variable to decide whether to draw all the stuff
boolean debug = true;
// Flowfield object
FlowField flowfield;
// An ArrayList of vehicles
ArrayList<Vehicle> vehicles;
void setup() {
size(640, 360);
// Make a new flow field wi