Boid velocity and acceleration when clicking on the screen

This commit is contained in:
Joseph Ferano 2024-01-06 18:24:56 +07:00
parent 50deef7c3d
commit 44dc3ccd48
2 changed files with 20 additions and 11 deletions

29
boids.c
View File

@ -4,13 +4,13 @@
#include <stdlib.h>
#include "lib.h"
#define SCREEN_WIDTH 1300
#define SCREEN_HEIGHT 1000
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define TARGET_FPS 60
#define NUM_BOIDS 16
const float max_speed = 200.0f;
const float max_force = 0.1;
const float max_speed = 5.0f;
const float max_force = 0.05;
typedef struct Boid {
Point position;
@ -31,10 +31,11 @@ int main(void) {
boid->position = (Vector2){rand_x, rand_y};
int rand_vx = GetRandomValue(0, 100);
int rand_vy = GetRandomValue(0, 100);
boid->velocity = (Vector2){rand_vx * 0.01f, rand_vy * 0.01f};
int rand_ax = GetRandomValue(0, 100);
int rand_ay = GetRandomValue(0, 100);
boid->velocity = (Vector2){rand_ax * 0.01f - 0.5f, rand_ay * 0.01f - 0.5f};
boid->velocity = (Vector2){rand_vx * 0.01f - 0.5f, rand_vy * 0.01f - 0.5f};
boid->velocity = Vector2Scale(boid->velocity, 10.0f);
// int rand_ax = GetRandomValue(0, 100);
// int rand_ay = GetRandomValue(0, 100);
// boid->velocity = (Vector2){rand_ax * 0.01f - 0.5f, rand_ay * 0.01f - 0.5f};
}
PointOption target_pos = {0};
@ -51,6 +52,14 @@ int main(void) {
for (int i = 0; i < NUM_BOIDS; i++) {
Boid *boid = &boids[i];
if (target_pos.tag == SOME) {
Vector2 desired = Vector2Subtract(target_pos.some.point, boid->position);
desired = Vector2Normalize(desired);
desired = Vector2Scale(desired, max_speed);
Vector2 steer = Vector2Subtract(desired, boid->velocity);
steer = Vector2ClampValue(steer, 0.0f, max_force);
boid->acceleration = Vector2Add(boid->acceleration, steer);
}
boid->velocity = Vector2Add(boid->velocity, boid->acceleration);
boid->velocity = Vector2ClampValue(boid->velocity, 0.0f, max_speed);
boid->position = Vector2Add(boid->position, boid->velocity);
@ -66,8 +75,8 @@ int main(void) {
// DrawTriangleLines(Vector2 v1, Vector2 v2, Vector2 v3, GREEN);
for (int i = 0; i < NUM_BOIDS; i++) {
Boid *boid = &boids[i];
DrawCircle(boid->position.x, boid->position.y, 35, BLACK);
DrawCircle(boid->position.x, boid->position.y, 25, GREEN);
DrawCircle(boid->position.x, boid->position.y, 27, BLACK);
DrawCircle(boid->position.x, boid->position.y, 20, GREEN);
}
}
EndDrawing();

View File

@ -8,5 +8,5 @@
** <2024-01-06 Sat>
:LOGBOOK:
CLOCK: [2024-01-06 Sat 11:00]
CLOCK: [2024-01-06 Sat 11:00]--[2024-01-06 Sat 13:10] => 2:10
:END: