Boids/C: Difference between revisions

Content added Content deleted
(boids + opengl)
 
m (tweaks)
Line 84: Line 84:
typedef struct {
typedef struct {
vec position, heading, newheading;
vec position, heading, newheading;
flt speed;
} boid_t;
} boid_t;


Line 284: Line 285:
int i;
int i;
for (i = 0; i < N; i++)
for (i = 0; i < N; i++)
vmuladd_to(&boids[i].position, &boids[i].heading, msec * MOVE_SPEED);
vmuladd_to(&boids[i].position, &boids[i].heading, msec * boids[i].speed);


vec average = {{0,0,0}};
vec average = {{0,0,0}};
Line 365: Line 366:


glTranslatef(b->position.x[0], b->position.x[1], b->position.x[2]);
glTranslatef(b->position.x[0], b->position.x[1], b->position.x[2]);

flt yaw = atan2(b->heading.x[1], b->heading.x[0]) / PI * 180 - 90;
float *x = b->heading.x;
flt yaw = atan2(x[1], x[0]) / PI * 180 - 90;
glRotatef(yaw, 0, 0, 1);
glRotatef(yaw, 0, 0, 1);

flt rxy = sqrt(x[0] * x[0] + x[1] * x[1]);
flt pitch = atan2(x[2], rxy) / PI * 180;
glRotatef(pitch, 1, 0, 0);


glBegin(GL_TRIANGLES);
glBegin(GL_TRIANGLES);
Line 533: Line 540:
flt z = (((flt)rand() / RAND_MAX) + .5) * IDEAL_HEIGHT + ground_height(x, y);
flt z = (((flt)rand() / RAND_MAX) + .5) * IDEAL_HEIGHT + ground_height(x, y);
boids[i].position = (vec){{x, y, z}};
boids[i].position = (vec){{x, y, z}};
boids[i].speed = (0.98 + 0.04 * rand() / RAND_MAX) * MOVE_SPEED;
}
}