eMonolith
Coder
Hello all. My graphics don't step through time like it should. I am trying to write a simple particle sim and I just get a still frame.
C:
#include <X11/Xlib.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h> // Include for usleep
#define WIDTH 1024
#define HEIGHT 1024
#define N 20 // Number of particles
#define RADIUS 512 // Radius of the circular area
#define PARTICLE_RADIUS 5 // Radius of each particle
typedef struct {
double x, y;
double angle;
} Particle;
void initialize_particles(Particle particles[], int n) {
for (int i = 0; i < n; i++) {
particles[i].angle = (double)rand() / RAND_MAX * 2 * M_PI;
particles[i].x = WIDTH / 2 + RADIUS * cos(particles[i].angle);
particles[i].y = HEIGHT / 2 + RADIUS * sin(particles[i].angle);
}
}
void update_particles(Particle particles[], int n) {
for (int i = 0; i < n; i++) {
particles[i].angle += (double)rand() / RAND_MAX * 0.1 - 0.05; // Random angle change
double new_x = WIDTH / 2 + RADIUS * cos(particles[i].angle);
double new_y = HEIGHT / 2 + RADIUS * sin(particles[i].angle);
if (sqrt(pow(new_x - WIDTH / 2, 2) + pow(new_y - HEIGHT / 2, 2)) <= RADIUS - PARTICLE_RADIUS) {
particles[i].x = new_x;
particles[i].y = new_y;
}
for (int j = 0; j < n; j++) {
if (i != j) {
double distance = sqrt(pow(particles[i].x - particles[j].x, 2) + pow(particles[i].y - particles[j].y, 2));
if (distance < 2 * PARTICLE_RADIUS) {
double angle = atan2(particles[j].y - particles[i].y, particles[j].x - particles[i].x);
particles[i].x -= cos(angle) * 0.5;
particles[i].y -= sin(angle) * 0.5;
}
}
}
}
}
int main() {
Display *display;
Window window;
GC gc;
Particle particles[N];
srand(time(NULL));
initialize_particles(particles, N);
display = XOpenDisplay(NULL);
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 0, 0, WIDTH, HEIGHT, 0, 0, 0);
XSelectInput(display, window, ExposureMask | KeyPressMask);
XMapWindow(display, window);
gc = XCreateGC(display, window, 0, NULL);
while (1) {
XEvent event;
while (XPending(display)) {
XNextEvent(display, &event);
if (event.type == KeyPress) break;
}
XClearWindow(display, window);
XSetForeground(display, gc, 0xFF00FF);
for (int i = 0; i < N; i++) {
XDrawArc(display, window, gc, particles[i].x - PARTICLE_RADIUS, particles[i].y - PARTICLE_RADIUS,
2 * PARTICLE_RADIUS, 2 * PARTICLE_RADIUS, 0, 360 * 64);
}
update_particles(particles, N);
usleep(16666); // Sleep for approximately 60 frames per second
}
XFreeGC(display, gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}
Last edited: