🍄 growing thing

This commit is contained in:
Felix Pankratz 2024-12-08 18:11:22 +01:00
parent 4f5cc216dd
commit 7ef3ba9acb

View File

@ -48,6 +48,19 @@ typedef struct {
int y_coords[BLOB_POINTS]; int y_coords[BLOB_POINTS];
} Blob; } Blob;
#define NUM_AGENTS 64
#define AGENT_MOVE_DISTANCE 1
#define AGENT_DROP_AMOUNT 100
typedef struct {
int x_position;
int y_position;
float heading;
} SlimeAgent;
uint16_t attractant[PANEL_WIDTH][PANEL_HEIGHT] = {};
SlimeAgent agents[NUM_AGENTS];
Blob blob; Blob blob;
void setup(){ void setup(){
@ -72,13 +85,23 @@ void setup(){
matrix->setBrightness8(64); matrix->setBrightness8(64);
matrix->fillScreenRGB888(0, 0, 0); matrix->fillScreenRGB888(0, 0, 0);
for(int i = 0; i < BLOB_POINTS; i++) { //for(int i = 0; i < BLOB_POINTS; i++) {
//float angle = (360 / BLOB_POINTS) * (i+1); // //float angle = (360 / BLOB_POINTS) * (i+1);
float angle = ((PI * 2) / BLOB_POINTS) * (i+1); // float angle = ((PI * 2) / BLOB_POINTS) * (i+1);
blob.x_coords[i] = random(-6, 7) + 31 + round(BLOB_RADIUS * sin(angle)); // blob.x_coords[i] = random(-6, 7) + 31 + round(BLOB_RADIUS * sin(angle));
blob.y_coords[i] = random(-2, 7) + 15 + round(BLOB_RADIUS * cos(angle)); // blob.y_coords[i] = random(-2, 7) + 15 + round(BLOB_RADIUS * cos(angle));
//blob.x_coords[i] = random(0, 64); // //blob.x_coords[i] = random(0, 64);
//blob.y_coords[i] = random(0, 32); // //blob.y_coords[i] = random(0, 32);
//}
for(int a = 0; a < NUM_AGENTS; a++) {
agents[a].x_position = random(12, 52);
agents[a].y_position = random(12, 20);
agents[a].heading = (random(0, 100) / 100.0) * (PI*2);
}
for(int x = 0; x<PANEL_WIDTH; x++) {
for(int y = 0; y<PANEL_HEIGHT; y++){
attractant[x][y] = 0;
}
} }
} }
@ -107,6 +130,9 @@ void draw_bezier(int x1, int y1, int x2, int y2, int x3, int y3) {
} }
bool is_in_bounds(int x, int y) {
return (x < PANEL_WIDTH) and (y < PANEL_HEIGHT) and (x >= 0) and (y >= 0);
}
void loop() { void loop() {
matrix ->flipDMABuffer(); matrix ->flipDMABuffer();
@ -125,31 +151,74 @@ void loop() {
matrix->drawFastHLine(0, 7*i + 1, PANEL_WIDTH, matrix->color565(41, 17, 76)); matrix->drawFastHLine(0, 7*i + 1, PANEL_WIDTH, matrix->color565(41, 17, 76));
} }
for(int i = 0; i < BLOB_POINTS; i+=2) { for(int x = 0; x<PANEL_WIDTH; x++) {
int end_offset = i + 2; for(int y = 0; y<PANEL_HEIGHT; y++){
if ( end_offset >= BLOB_POINTS) { matrix->drawPixel(x, y, attractant[x][y]);
end_offset = 0;
} }
draw_bezier(blob.x_coords[i],
blob.y_coords[i],
blob.x_coords[i+1],
blob.y_coords[i+1],
blob.x_coords[end_offset],
blob.y_coords[end_offset]
);
matrix->drawPixel(blob.x_coords[i], blob.y_coords[i], matrix->color565(128, 128, 128));
blob.x_coords[i+1] += random(-1, 2);
if (blob.x_coords[i+1] >= PANEL_WIDTH) {
blob.x_coords[i+1] = PANEL_WIDTH - 1;
} }
if (blob.x_coords[i+1] < 0) {
blob.x_coords[i+1] = 0; for(int a = 0; a < NUM_AGENTS; a++) {
matrix->drawPixel(agents[a].x_position, agents[a].y_position, attractant[agents[a].x_position][agents[a].y_position]);
// motor step
// check if agent can move forward
float target_x = agents[a].x_position + cos(agents[a].heading) * AGENT_MOVE_DISTANCE;
float target_y = agents[a].y_position + sin(agents[a].heading) * AGENT_MOVE_DISTANCE;
int move_x = round(target_x);
int move_y = round(target_y);
if (is_in_bounds(move_x, move_y)) {
agents[a].x_position = move_x;
agents[a].y_position = move_y;
attractant[move_x][move_y] += AGENT_DROP_AMOUNT;
} else {
agents[a].heading += (random(0,2) *2 -1 ) * (random(0, 30) / 100.0) * PI*2;
} }
//blob.y_coords[i+1] += random(-1, 2); // sample step
//matrix->drawPixel(blob.x_coords[i], blob.y_coords[i], matrix->color565(0, 0, 128)); int front_x = round(agents[a].x_position + cos(agents[a].heading) * AGENT_MOVE_DISTANCE);
//matrix->drawPixel(blob.x_coords[i+1], blob.y_coords[i+1], matrix->color565(128, 0, 0)); int front_y = round(agents[a].y_position + sin(agents[a].heading) * AGENT_MOVE_DISTANCE);
//matrix->drawPixel(blob.x_coords[end_offset], blob.y_coords[end_offset], matrix->color565(0, 0, 128)); int left_x = round(agents[a].x_position + cos(agents[a].heading - ((random(0,30) / 100.0) * (PI * 2))));
int left_y = round(agents[a].y_position + cos(agents[a].heading - ((random(0,30) / 100.0) * (PI * 2))));
int right_x = round(agents[a].x_position + cos(agents[a].heading + ((random(0,30) / 100.0) * (PI * 2))));
int right_y = round(agents[a].y_position + cos(agents[a].heading + ((random(0,30) / 100.0) * (PI * 2))));
int front_value = attractant[front_x][front_y];
int left_value = attractant[left_x][left_y];
int right_value = attractant[right_x][right_y];
if (front_value > right_value and front_value > left_value) {
;
} else if (front_value < right_value and front_value < left_value) {
agents[a].heading += (random(0, 2) * 2 - 1) * (random(0, 30) / 100.0) * (PI*2) ;
} else if (left_value > right_value) {
agents[a].heading -= ((random(0,30) / 100.0) * (PI * 2));
} else {
agents[a].heading += ((random(0,30) / 100.0) * (PI * 2));
} }
delay(500); }
//for(int i = 0; i < BLOB_POINTS; i+=2) {
// int end_offset = i + 2;
// if ( end_offset >= BLOB_POINTS) {
// end_offset = 0;
// }
// draw_bezier(blob.x_coords[i],
// blob.y_coords[i],
// blob.x_coords[i+1],
// blob.y_coords[i+1],
// blob.x_coords[end_offset],
// blob.y_coords[end_offset]
// );
// matrix->drawPixel(blob.x_coords[i], blob.y_coords[i], matrix->color565(128, 128, 128));
// blob.x_coords[i+1] += random(-1, 2);
// if (blob.x_coords[i+1] >= PANEL_WIDTH) {
// blob.x_coords[i+1] = PANEL_WIDTH - 1;
// }
// if (blob.x_coords[i+1] < 0) {
// blob.x_coords[i+1] = 0;
// }
// //blob.y_coords[i+1] += random(-1, 2);
// //matrix->drawPixel(blob.x_coords[i], blob.y_coords[i], matrix->color565(0, 0, 128));
// //matrix->drawPixel(blob.x_coords[i+1], blob.y_coords[i+1], matrix->color565(128, 0, 0));
// //matrix->drawPixel(blob.x_coords[end_offset], blob.y_coords[end_offset], matrix->color565(0, 0, 128));
//}
delay(100);
} }