summaryrefslogtreecommitdiffstats
path: root/balls.c
blob: 5fd61f452edc19a6dc5f86ec1f4c292c93a8fb6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "balls.h"

#include <stdio.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>

#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))

#define G 0.25

enum {
	BG = DWhite,
	WALLS = DBlack,

	PAD = 0,
	WIDTH = 800,
	HEIGHT=600,

	KEY_QUIT = 'q' ,

	DEFAULT_NBALLS = 3,

	VMAX = 3,
	MASS = 10,

	FPS = 30,
	NS_PER_SEC = 1000000000,
	FRAME_TIME = NS_PER_SEC / FPS,
	TICK_BUFSIZE = 4,

	POS_BUFSIZE = 4,
};

typedef struct {
	Ball b;

	int color;

	Channel *frametick;

	Channel **in; /* <-chan Ball */
	Channel **out; /* chan<- Ball */
	int nothers; /* number of other balls */
} BallArg;

int ballcolors[] = { DRed, DGreen, DBlue };
Rectangle bounds = {{PAD, PAD}, {PAD+WIDTH, PAD+HEIGHT}};

int init(char *label, Mousectl **mctl, Keyboardctl **kctl);
void spawnball(void);
void drawbg(Image *walls, Image *bg);
void spawnballs(int n);
Channel **allocchans(int nchans, int elsize, int nel);
void mcopycolskip(Channel *vec[], Channel **matrix[], int n, int col, int skip);
void vcopyskip(Channel *dst[], Channel *src[], int n, int skip);
void nooverlapcircles(Point centers[], int n);
Point randptinrect(Rectangle r);
int randint(int lo, int hi);
void ball(void *arg);
Image *alloccircle(int fg, int bg);
void drawcircle(Image *m, Point pos);
int clamp(int v, int lo, int hi);
int min(int a, int b);
int max(int a, int b);
void broadcast(Point p, Channel *cs[], int n);
void frametick(void *arg);

void
threadmain(int argc, char *argv[]) {
	int nballs;
	Image *bg, *walls;
	Mousectl *mctl;
	Keyboardctl *kctl;
	int resize[2];
	Rune key;

	nballs = DEFAULT_NBALLS;
	if (argc > 1) {
		if (sscanf(argv[1], "%d", &nballs) != 1 || nballs < 1) {
			printf("usage: balls [number of balls]\n");
			threadexitsall(0);
		}
	}
	printf("nballs: %ud\n", nballs);

	if (init(argv[0], &mctl, &kctl))
		sysfatal("%s: %r", argv[0]);

	printf("screen: (%d,%d) (%d,%d)\n", screen->r.min.x, screen->r.min.y,
		screen->r.max.x, screen->r.max.y);
	printf("bounds: (%d,%d) (%d,%d)\n", bounds.min.x, bounds.min.y,
		bounds.max.x, bounds.max.y);

	walls = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, WALLS);
	bg = allocimage(display, bounds, RGBA32, 0, BG);
	if (bg == nil || walls == nil)
		sysfatal("failed to allocate images");
	
	drawbg(walls, bg);

	spawnballs(nballs);

	enum { RESIZE = 0, KEYBD = 1 };
	Alt alts[3] = {
		{mctl->resizec, &resize, CHANRCV},
		{kctl->c, &key, CHANRCV},
		{nil, nil, CHANEND},
	};
	for (;;) {
		switch (alt(alts)) {
		case RESIZE:
			if (getwindow(display, Refnone) < 0)
				sysfatal("%s: %r", argv[0]);
			drawbg(walls, bg);
			break;
		case KEYBD:
			if (key == KEY_QUIT)
				threadexitsall(0);
			break;
		case -1: /* interrupted */
			break;
		}
	}
}

int
init(char *label, Mousectl **mctl, Keyboardctl **kctl) {
	if (initdraw(nil, nil, label) < 0)
		return 1;
	if ((*mctl = initmouse(nil, screen)) == nil)
		return 1;
	if ((*kctl = initkeyboard(nil)) == nil)
		return 1;
	return 0;
}

void
drawbg(Image *walls, Image *bg) {
	draw(screen, screen->r, walls, nil, ZP);
	draw(screen, screen->r, bg, nil, ZP);
	flushimage(display, Refnone);
}

void
spawnballs(int n) {
	Channel **ticks;
	Point *ps;
	int i, j;
	BallArg *arg;
	Channel ***cs;

	if ((ticks = allocchans(n, sizeof(int), TICK_BUFSIZE)) == nil)
		sysfatal("failed to allocate frame ticker channels");

	threadcreate(frametick, ticks, mainstacksize);

	if ((ps = malloc(n*sizeof(Point))) == nil)
		sysfatal("failed to allocate position array");
	nooverlapcircles(ps, n);

	if ((cs = malloc(n*sizeof(Channel **))) == nil)
		sysfatal("failed to allocate channel matrix");
	for (i = 0; i < n; i++) {
		if ((cs[i] = malloc(n*sizeof(Channel *))) == nil)
			sysfatal("failed to allocate row of channel matrix");
		for (j = 0; j < n; j++) {
			if (j == i)
				continue;
			if ((cs[i][j] = chancreate(sizeof(Ball), POS_BUFSIZE)) == nil)
				sysfatal("failed to create channel");
		}
	}

	for (i = 0; i < n; i++) {
		if ((arg = malloc(sizeof(BallArg))) == nil)
			sysfatal("failed to allocate ball");

		arg->b.p = ps[i];
		arg->b.v = V((double) randint(-VMAX, VMAX+1), (double) randint(-VMAX, VMAX+1));
		arg->b.m = MASS;

		arg->color = ballcolors[randint(0, NELEMS(ballcolors))];

		arg->frametick = ticks[i];

		if ((arg->in = malloc((n-1)*sizeof(Channel *))) == nil)
			sysfatal("failed to allocate array of incoming channels");
		mcopycolskip(arg->in, cs, n, i, i);

		if ((arg->out = malloc((n-1)*sizeof(Channel *))) == nil)
			sysfatal("failed to allocate array of outgoing channels");
		vcopyskip(arg->out, cs[i], n, i);

		arg->nothers = n-1;

		threadcreate(ball, arg, mainstacksize);
	}

	free(ps);
	for (i = 0; i < n; i++)
		free(cs[i]);
	free(cs);
}

Channel **
allocchans(int nchans, int elsize, int nel) {
	Channel **cs;
	int i;

	if (nchans < 0)
		return nil;

	if ((cs = malloc((nchans+1)*sizeof(Channel *))) == nil)
		return nil;
	for (i = 0; i < nchans; i++) {
		if ((cs[i] = chancreate(elsize, nel)) == nil) {
			while (i-- > 0)
				chanfree(cs[i]);
			free(cs);
			return nil;
		}
	}
	cs[nchans] = nil;
	return cs;
}

/* copy column col of nxn matrix, except for element skip, into n-1 length vec */
void
mcopycolskip(Channel *vec[], Channel **matrix[], int n, int col, int skip) {
	int i;

	for (i = 0; i < skip; i++)
		vec[i] = matrix[i][col];
	for (i = skip+1; i < n; i++)
		vec[i-1] = matrix[i][col];
}

/* copy each element in n-length src, except for element skip, into n-1 length dst */
void
vcopyskip(Channel *dst[], Channel *src[], int n,  int skip) {
	int i;

	for (i = 0; i < skip; i++)
		dst[i] = src[i];
	for (i = skip+1; i < n; i++)
		dst[i-1] = src[i];
}

void
nooverlapcircles(Point centers[], int n) {
	int i, j;

	srand(time(0));
	for (i = 0; i < n; i++) {
		centers[i] = randptinrect(insetrect(bounds, RADIUS));
		for (j = 0; j < i; j++)
			if (iscollision(centers[j], centers[i]))
				break;
		if (j < i) { /* overlapping */
			i--;
			continue;
		}
	}
}

Point
randptinrect(Rectangle r) {
	return Pt(randint(r.min.x, r.max.x), randint(r.min.y, r.max.y));
}

int
randint(int lo, int hi) {
	return (rand() % (hi-lo)) + lo;
}

void
ball(void *arg) {
	BallArg *barg;
	Point p, oldp;
	Vec v;
	int m;
	Image *fill, *erase;
	int i;
	Ball other;
	Point midpoint;
	Vec d, n;
	double magnitude;
	int t;

	barg = (BallArg *) arg;
	p = oldp = barg->b.p;
	v = barg->b.v;
	m = barg->b.m;

	fill = alloccircle(barg->color, DTransparent);
	erase = alloccircle(BG, DTransparent);
	if (fill == nil ||erase == nil)
		sysfatal("failed to allocate image");

	for (;;) {
		v.y += G;

		p = ptaddv(p, v);

		printf("(%d,%d) %f %f\n", p.x, p.y, v.x, v.y);

		broadcast(p, barg->out, barg->nothers);

		/* check for ball collision */
		for (i = 0; i < barg->nothers; i++) {
			recv(barg->in[i], &other);
			if (iscollision(p, other.p)) {
				midpoint = divpt(addpt(p, other.p), 2);
				d = Vpt(other.p, p);
				p = ptaddv(midpoint, vmuls(unitnorm(d), RADIUS));

				printf("collision (%d,%d), (%d,%d)\n", p.x, p.y, other.p.x, other.p.y);

				printf("oldv: (%2.2f,%2.2f\n", v.x, v.y);

				n = unitnorm(Vpt(other.p, p));
				magnitude = 2*(vdot(v, n) - vdot(other.v, n)) / (m + other.m);

				printf("n: (%2.2f,%2.2f), magnitude: %2.2f\n", n.x, n.y, magnitude);

				v = vsub(v, vmuls(n, magnitude * m));

				printf("newv: (%2.2f,%2.2f)\n", v.x, v.y);
			}
		}		

		/* check for wall collision */
		if (p.x < bounds.min.x+RADIUS || p.x > bounds.max.x-RADIUS) {
			p.x = clamp(p.x, bounds.min.x+RADIUS, bounds.max.x-RADIUS);
			printf("clamped to %d\n", p.x);
			v.x = -v.x;
		}
		if (p.y < bounds.min.y+RADIUS || p.y > bounds.max.y-RADIUS) {
			p.y = clamp(p.y, bounds.min.y+RADIUS, bounds.max.y-RADIUS);
			v.y = -v.y;
		}		
		recv(barg->frametick, &t);
		drawcircle(erase, oldp);
		drawcircle(fill, p);
		oldp = p;

	}	
}

Image *
alloccircle(int fg, int bg) {
	Image *m, *fill
;
	m = allocimage(display, Rect(0, 0, 2*RADIUS, 2*RADIUS), RGBA32, 0, bg);
	if (m == nil)
		return nil;

	fill = allocimage(display, Rect(0, 0, 1, 1), RGBA32, 1, fg);
	if (fill == nil) {
		free(m);
		return nil;
	}

	fillellipse(m, Pt(RADIUS, RADIUS), RADIUS, RADIUS, fill, ZP);
	freeimage(fill);
	return m;
}

void
drawcircle(Image *m, Point pos) {
	draw(screen, rectaddpt(bounds, subpt(pos, Pt(RADIUS, RADIUS))), m, nil, ZP);
}

int
clamp(int v, int lo, int hi) {
	return min(hi, max(v, lo));
}

int
min(int a, int b) {
	return (a < b) ? a : b;
}

int
max(int a, int b) {
	return (a > b) ? a : b;
}

void
broadcast(Point p, Channel *cs[], int n) {
	while (n-- > 0)
		send(cs[n], &p);
}

void
frametick(void *arg) {
	Channel **cs, **c;
	vlong t1, t2;
	int s;

	cs = (Channel **) arg;

	t1 = t2 = nsec();
	for (;;) {
		while (t2 - t1 < FRAME_TIME)
			t2 = nsec();
		t1 = t2;
		for (c = cs; *c != nil; c++)
			send(*c, &s);
		flushimage(display, Refnone);
	}
}